如何使用带有 GridSearchCV 对象的 TimeSeriesSplit 来调整 scikit-learn 中的模型? [英] How do I use a TimeSeriesSplit with a GridSearchCV object to tune a model in scikit-learn?

查看:44
本文介绍了如何使用带有 GridSearchCV 对象的 TimeSeriesSplit 来调整 scikit-learn 中的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 sklearn 文档中搜索了 TimeSeriesSplit 和用于交叉验证的 文档 但我一直没能找到一个有效的例子.

I've searched the sklearn docs for TimeSeriesSplit and the docs for cross-validation but I haven't been able to find a working example.

我使用的是 sklearn 0.19 版.

I'm using sklearn version 0.19.

这是我的设置

import xgboost as xgb
from sklearn.model_selection import TimeSeriesSplit
from sklearn.grid_search import GridSearchCV
import numpy as np
X = np.array([[4, 5, 6, 1, 0, 2], [3.1, 3.5, 1.0, 2.1, 8.3, 1.1]]).T
y = np.array([1, 6, 7, 1, 2, 3])
tscv = TimeSeriesSplit(n_splits=2)
for train, test in tscv.split(X):
    print(train, test)

给出:

[0 1] [2 3]
[0 1 2 3] [4 5]

如果我尝试:

model = xgb.XGBRegressor()
param_search = {'max_depth' : [3, 5]}

my_cv = TimeSeriesSplit(n_splits=2).split(X)
gsearch = GridSearchCV(estimator=model, cv=my_cv,
                        param_grid=param_search)
gsearch.fit(X, y)

它给出:TypeError: object of type 'generator' has no len()

我遇到了问题:GridSearchCV 正在尝试调用 len(cv)my_cv 是一个没有长度的迭代器.但是,文档GridSearchCV 状态我可以使用

I get the problem: GridSearchCV is trying to call len(cv) but my_cv is an iterator without length. However, the docs for GridSearchCV state I can use a

int,交叉验证生成器或可迭代的,可选的

int, cross-validation generator or an iterable, optional

我尝试在没有 .split(X) 的情况下使用 TimeSeriesSplit 但它仍然不起作用.

I tried using TimeSeriesSplit without the .split(X) but it still didn't work.

我确定我忽略了一些简单的东西,谢谢!!

I'm sure I'm overlooking something simple, thanks!!

推荐答案

事实证明问题是我使用了来自 sklearn.grid_searchGridSearchCV,它已被弃用.从 sklearn.model_selection 导入 GridSearchCV 解决了问题:

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 xgb
from sklearn.model_selection import TimeSeriesSplit, GridSearchCV
import numpy as np
X = np.array([[4, 5, 6, 1, 0, 2], [3.1, 3.5, 1.0, 2.1, 8.3, 1.1]]).T
y = 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)

给出:

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)

这篇关于如何使用带有 GridSearchCV 对象的 TimeSeriesSplit 来调整 scikit-learn 中的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆