TSRGridSearchCV
Exhaustive search over specified parameter values for a regressor.
Optimizes hyper-parameters of estimator by exhaustive grid search, using sktime native backtesting via regression.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 of the tuner calls predict of best_estimator_.
Quickstart
from sktime.regression.model_selection import TSRGridSearchCV
estimator = TSRGridSearchCV(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 regressor, BaseRegressor instance or interface compatible
- The regressor to tune, must implement the sktime regressor 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.,r2_scorefromsklearn.metrics. Its value is reported as isa string must name a scikit-learn scorer, e.g.,
"r2". Values are reported with the sign convention of the scorer, so values of"neg_mean_squared_error"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
r2_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,predictraises, 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
KFold,a cross-validation splitter with a
splitmethod,an iterable yielding (train, test) splits as arrays of indices.
For integer and None inputs,
KFoldis used, 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 regressors_ attribute. Has the same effect as applying ColumnEnsembleRegressor 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.,
r2_scoreis higher-is-better, andmean_squared_erroris 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.
Examples
>>> from sktime.datasets import load_unit_test
>>> from sktime.regression.dummy import DummyRegressor
>>> from sktime.regression.model_selection import TSRGridSearchCV
>>>
>>> X, y = load_unit_test (split = "train")
>>> tuned = TSRGridSearchCV (
... DummyRegressor (),
... param_grid = { "strategy": ["mean", "median" ]},
... cv = 2,
... )
>>> tuned = tuned. fit (X, y. astype ("float"))
>>> y_pred = tuned. predict (X)
>>> best_params = tuned. best_params_