How do I use a TimeSeriesSplit with a GridSearchCV object to tune a model in scikit-learn? How do I use a TimeSeriesSplit with a GridSearchCV object to tune a model in scikit-learn? python python

How do I use a TimeSeriesSplit with a GridSearchCV object to tune a model in scikit-learn?


It turns out the problem was I was using GridSearchCV from sklearn.grid_search, which is deprecated. Importing GridSearchCV from sklearn.model_selection resolved the problem:

import xgboost as xgbfrom sklearn.model_selection import TimeSeriesSplit, GridSearchCVimport numpy as npX = np.array([[4, 5, 6, 1, 0, 2], [3.1, 3.5, 1.0, 2.1, 8.3, 1.1]]).Ty = np.array([1, 6, 7, 1, 2, 3])model = xgb.XGBRegressor()param_search = {'max_depth' : [3, 5]}tscv = TimeSeriesSplit(n_splits=2)gsearch = GridSearchCV(estimator=model, cv=tscv,                        param_grid=param_search)gsearch.fit(X, y)

gives:

GridSearchCV(cv=<generator object TimeSeriesSplit.split at 0x11ab4abf8>,       error_score='raise',       estimator=XGBRegressor(base_score=0.5, colsample_bylevel=1, colsample_bytree=1, gamma=0,       learning_rate=0.1, max_delta_step=0, max_depth=3,       min_child_weight=1, missing=None, n_estimators=100, nthread=-1,       objective='reg:linear', reg_alpha=0, reg_lambda=1,       scale_pos_weight=1, seed=0, silent=True, subsample=1),       fit_params=None, iid=True, n_jobs=1,       param_grid={'max_depth': [3, 5]}, pre_dispatch='2*n_jobs',       refit=True, return_train_score=True, scoring=None, verbose=0)