TSCGridSearchCV
TSCGridSearchCV
- class TSCGridSearchCV(estimator, param_grid, scoring=None, n_jobs='deprecated', refit=True, cv=None, verbose=0, pre_dispatch='deprecated', error_score=nan, return_train_score=False, tune_by_variable=False, greater_is_better='auto', backend=None, backend_params=None)[source]
Exhaustive search over specified parameter values for a classifier.
Optimizes hyper-parameters of
estimatorby exhaustive grid search, usingsktimenative backtesting viaclassification.model_evaluation.evaluate.In
fit, each parameter combination inparam_gridis backtested on the data passed, using the cross-validation schemecvand the metricscoring. All candidates are evaluated on identical folds.The parameter combination with the best mean test score is set as
best_params_, and a clone ofestimatorwith those parameters is set asbest_estimator_. Ifrefitis not False,best_estimator_is fitted to the entire data, andpredictandpredict-like methods of the tuner call the respective method ofbest_estimator_.- Parameters:
- estimatorsktime classifier, BaseClassifier instance or interface compatible
The classifier to tune, must implement the sktime classifier interface.
- param_griddict or list of dictionaries
Dictionary with parameters names (
str) as keys and lists of parameter settings to try as values, or a list of such dictionaries, in which case the grids spanned by each dictionary in the list are explored. This enables searching over any sequence of parameter settings.- scoringNone, str, callable, sklearn scorer, or list or dict of these
Metric or metrics to evaluate the cross-validated model with.
a callable must have signature
(y_true, y_pred) -> float, e.g.,accuracy_scorefromsklearn.metrics. Its value is reported as isa string must name a scikit-learn scorer, e.g.,
"accuracy". Values are reported with the sign convention of the scorer, so values of"neg_log_loss"are negativea list or dict selects multiple metrics. The first is used to rank candidates, unless
refitnames another. Dict keys are used as the metric names incv_results_if None, defaults to
accuracy_score
- n_jobsint, optional, default=”deprecated”
Number of jobs to run in parallel over the parameter candidates.
Deprecated, and will be removed in sktime 1.3.0. If passed, the value is written to
backend_params, andbackenddefaults to"loky", so behaviour is unchanged. To retain the behaviour after removal, passbackend="loky"andbackend_params={"n_jobs": ...}instead.- refitbool, str, or callable, default=True
Refit
best_estimator_using the best found parameters on the whole dataset. If False,predictandpredict-like methods raise, and the tuner can be used only to tune hyper-parameters, e.g., as a parameter estimator viaget_fitted_params.For multi-metric evaluation, this can be a
strnaming the metric to select the best parameters by.Where there are considerations other than the best score in choosing the best parameters,
refitcan be a callable, which is applied tocv_results_and returns the selectedbest_index_. In that casebest_score_is not available.The refitted estimator is available at the
best_estimator_attribute, and permits callingpredictdirectly on the tuner.- cvint, cross-validation generator, iterable of splits, or None, default=None
Determines the cross-validation splitting strategy. Possible inputs for cv are:
None, to use the default 5-fold cross-validation,
integer, to specify the number of folds in a
(Stratified)KFold,a cross-validation splitter with a
splitmethod,an iterable yielding (train, test) splits as arrays of indices.
For integer and None inputs,
StratifiedKFoldis used ifyis binary or multiclass, andKFoldotherwise. Both are instantiated withshuffle=False, so the splits are the same across calls.Splits are computed once, before the search, so that all parameter candidates are evaluated on the same folds.
- verboseint, default=0
Controls the verbosity. If positive, the number of fits is printed.
- pre_dispatchint or str, optional, default=”deprecated”
Number of jobs dispatched during parallel execution, a
joblibparameter.Deprecated, and will be removed in sktime 1.3.0. If passed, the value is written to
backend_params, andbackenddefaults to"loky", so behaviour is unchanged. To retain the behaviour after removal, passbackend="loky"andbackend_params={"pre_dispatch": ...}instead.- error_score‘raise’ or numeric, default=np.nan
Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised. This parameter does not affect the refit step, which will always raise the error.
- return_train_scorebool, default=False
Whether to include training scores in
cv_results_.Deprecated, and will be removed in sktime 1.3.0. Train scores are not computed by the native grid search, so the value passed is ignored and
cv_results_contains test scores only. Passing True raises a warning.- tune_by_variablebool, optional (default=False)
Whether to tune parameter by each time series variable separately, in case of multivariate data passed to the tuning estimator. Only applies if time series passed are strictly multivariate. If True, clones of the estimator will be fit to each variable separately, and are available in fields of the classifiers_ attribute. Has the same effect as applying ColumnEnsembleClassifier wrapper to self. If False, the same best parameter is selected for all variables.
- greater_is_better“auto”, bool, optional, default=”auto”
Whether higher values of the reported metric are better, used to rank the parameter candidates.
“auto” determines the direction from the metric. Scikit-learn scorers are higher-is-better by their sign convention. For metric callables, the direction is inferred from the metric, e.g.,
accuracy_scoreis higher-is-better, andlog_lossis lower-is-betterTrue or False set the direction explicitly, for all metrics
- backendstr, optional, default=None
Parallelization backend for the search over parameter candidates.
None: executes loop sequentially, simple list comprehension
“loky”, “multiprocessing” and “threading”: uses
joblib.Parallelloops“joblib”: custom and 3rd party
joblibbackends, e.g.,spark“dask”: uses
dask, requiresdaskpackage in environment“ray”: uses
ray, requiresraypackage in environment
Recommendation: use “dask” or “loky” for parallel grid search. “threading” is unlikely to see speed ups due to the GIL.
- backend_paramsdict, optional
Additional parameters passed to the backend as config, directly passed to
utils.parallel.parallelize. Valid keys depend onbackend, see there for details.
- Attributes:
- cv_results_dict of str to numpy (masked) ndarray
A dict with keys as column headers and values as columns, that can be imported into a pandas
DataFrame. Containsparams, oneparam_<name>column per searched parameter, fit and score timings, and per metric the per-fold scoressplit<i>_test_<name>, their meanmean_test_<name>, standard deviationstd_test_<name>, and rankrank_test_<name>, 1 being the best.For a single metric,
<name>isscore, e.g.,mean_test_score. For multiple metrics,<name>is the name of the respective metric.- best_estimator_estimator
Clone of
estimatorwith the best found parameters set, i.e., the parameters which gave the best mean test score on the held out data. Fitted to the entire data ifrefitis not False, otherwise unfitted. See therefitparameter for more information on allowed values.- best_score_float
Mean cross-validated score of
best_estimator_. Not available ifrefitis a callable.- best_params_dict
Parameter setting that gave the best results on the hold out data.
- best_index_int
The index in the
cv_results_arrays which corresponds to the best candidate parameter setting.The dict at
cv_results_["params"][best_index_]gives the parameter setting for the best model, i.e., is identical withbest_params_.- scorer_callable or dict of callable
Metric used on the held out data to choose the best parameters. For multi-metric evaluation, a dict of metric name to metric.
- n_splits_int
The number of cross-validation splits (folds/iterations).
- refit_time_float
Seconds used for refitting the best model on the whole dataset. This is present only if
refitis not False.- multimetric_bool
Whether multiple metrics were passed in
scoring.- classes_ndarray of shape (n_classes,)
The class labels seen in
fit.
See also
ParameterGridGenerates all the combinations of a hyperparameter grid.
sktime.classification.model_evaluation.evaluateBacktesting used internally.
sklearn.metrics.make_scorerMake a scorer from a performance metric or loss function.
Examples
>>> from sklearn.metrics import accuracy_score >>> from sktime.classification.dummy import DummyClassifier >>> from sktime.classification.model_selection import TSCGridSearchCV >>> from sktime.datasets import load_unit_test >>> >>> X, y = load_unit_test(split="train") >>> tuned = TSCGridSearchCV( ... DummyClassifier(), ... param_grid={"strategy": ["most_frequent", "prior"]}, ... scoring=accuracy_score, ... cv=2, ... ) >>> tuned = tuned.fit(X, y) >>> y_pred = tuned.predict(X) >>> best_params = tuned.best_params_
Methods
check_is_fitted([method_name])Check if the estimator has been fitted.
clone()Obtain a clone of the object with same hyper-parameters and config.
clone_tags(estimator[, tag_names])Clone tags from another object as dynamic override.
create_test_instance([parameter_set])Construct an instance of the class, using first test parameter set.
create_test_instances_and_names([parameter_set])Create list of all test instances and a list of names for them.
fit(X, y)Fit time series classifier to training data.
fit_predict(X, y[, cv, change_state])Fit and predict labels for sequences in X.
fit_predict_proba(X, y[, cv, change_state])Fit and predict labels probabilities for sequences in X.
get_class_tag(tag_name[, tag_value_default])Get class tag value from class, with tag level inheritance from parents.
get_class_tags()Get class tags from class, with tag level inheritance from parent classes.
get_config()Get config flags for self.
get_fitted_params([deep])Get fitted parameters.
get_param_defaults()Get object's parameter defaults.
get_param_names([sort])Get object's parameter names.
get_params([deep])Get a dict of parameters values for this object.
get_tag(tag_name[, tag_value_default, ...])Get tag value from instance, with tag level inheritance and overrides.
get_tags()Get tags from instance, with tag level inheritance and overrides.
get_test_params([parameter_set])Return testing parameter settings for the estimator.
is_composite()Check if the object is composed of other BaseObjects.
load_from_path(serial)Load object from file location.
load_from_serial(serial)Load object from serialized memory container.
predict(X)Predicts labels for sequences in X.
predict_proba(X)Predicts labels probabilities for sequences in X.
reset()Reset the object to a clean post-init state.
save([path, serialization_format])Save serialized self to bytes-like object or to (.zip) file.
score(X, y)Scores predicted labels against ground truth labels on X.
set_config(**config_dict)Set config flags to given values.
set_params(**params)Set the parameters of this object.
set_random_state([random_state, deep, ...])Set random_state pseudo-random seed parameters for self.
set_tags(**tag_dict)Set instance level tag overrides to given values.

