Back to models
Classifier

TSCGridSearchCV

Exhaustive search over specified parameter values for a classifier.

Optimizes hyper-parameters of estimator by exhaustive grid search, using sktime native backtesting via classification.model_evaluation.evaluate.

In fit, each parameter combination in param_grid is backtested on the data passed, using the cross-validation scheme cv and the metric scoring. All candidates are evaluated on identical folds.

The parameter combination with the best mean test score is set as best_params_, and a clone of estimator with those parameters is set as best_estimator_. If refit is not False, best_estimator_ is fitted to the entire data, and predict and predict-like methods of the tuner call the respective method of best_estimator_.

Quickstart

python
from sktime.classification.model_selection import TSCGridSearchCV

estimator = 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)

Parameters(14)

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_score from sklearn.metrics. Its value is reported as is

  • a 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 negative

  • a list or dict selects multiple metrics. The first is used to rank candidates, unless refit names another. Dict keys are used as the metric names in cv_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, and backend defaults to "loky", so behaviour is unchanged. To retain the behaviour after removal, pass backend="loky" and backend_params={"n_jobs": ...} instead.

refitbool, str, or callable, default=True

Refit best_estimator_ using the best found parameters on the whole dataset. If False, predict and predict-like methods raise, and the tuner can be used only to tune hyper-parameters, e.g., as a parameter estimator via get_fitted_params.

For multi-metric evaluation, this can be a str naming the metric to select the best parameters by.

Where there are considerations other than the best score in choosing the best parameters, refit can be a callable, which is applied to cv_results_ and returns the selected best_index_. In that case best_score_ is not available.

The refitted estimator is available at the best_estimator_ attribute, and permits calling predict directly 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 split method,

  • an iterable yielding (train, test) splits as arrays of indices.

For integer and None inputs, StratifiedKFold is used if y is binary or multiclass, and KFold otherwise. Both are instantiated with shuffle=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 joblib parameter.

Deprecated, and will be removed in sktime 1.3.0. If passed, the value is written to backend_params, and backend defaults to "loky", so behaviour is unchanged. To retain the behaviour after removal, pass backend="loky" and backend_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_score is higher-is-better, and log_loss is lower-is-better

  • True 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.Parallel loops

  • “joblib”: custom and 3rd party joblib backends, e.g., spark

  • “dask”: uses dask, requires dask package in environment

  • “ray”: uses ray, requires ray package 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 on backend, see there for details.

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_