TypeError: ‘TimeSeriesSplit’ 对象不可迭代

我正在对一个包含时间序列分割的SVR设计进行网格搜索。我的代码是:

from sklearn.svm import SVRfrom sklearn.grid_search import GridSearchCVfrom sklearn.model_selection import TimeSeriesSplitfrom sklearn import svmfrom sklearn.preprocessing import MinMaxScalerfrom sklearn import preprocessing as preX_feature = X_feature.reshape(-1, 1)y_label = y_label.reshape(-1,1)param = [{'kernel': ['rbf'], 'gamma': [1e-2, 1e-3, 1e-4, 1e-5],                       'C': [1, 10, 100, 1000]},                       {'kernel': ['poly'], 'C': [1, 10, 100, 1000], 'degree': [1, 2, 3, 4]}] reg = SVR(C=1)timeseries_split = TimeSeriesSplit(n_splits=3)clf = GridSearchCV(reg, param, cv=timeseries_split, scoring='neg_mean_squared_error')X= pre.MinMaxScaler(feature_range=(0,1)).fit(X_feature)scaled_X = X.transform(X_feature)y = pre.MinMaxScaler(feature_range=(0,1)).fit(y_label)scaled_y = y.transform(y_label)clf.fit(scaled_X,scaled_y )

我的scaled y数据是:

 [0.11321139] [0.07218848] ... [0.64844211] [0.4926122 ] [0.4030334 ]]

我的scaled X数据是:

[[0.2681013 ] [0.03454225] [0.02062136] ... [0.92857565] [0.64930691] [0.20325924]]

然而,我得到了以下错误信息:

TypeError: 'TimeSeriesSplit' object is not iterable

我的错误回溯信息是:

  ---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-5-4403e696bf0d> in <module>()     19      20 ---> 21 clf.fit(scaled_X,scaled_y )~/anaconda3_501/lib/python3.6/site-packages/sklearn/grid_search.py in fit(self, X, y)    836     837         """--> 838         return self._fit(X, y, ParameterGrid(self.param_grid))    839     840 ~/anaconda3_501/lib/python3.6/site-packages/sklearn/grid_search.py in _fit(self, X, y, parameter_iterable)    572                                     self.fit_params, return_parameters=True,    573                                     error_score=self.error_score)--> 574                 for parameters in parameter_iterable    575                 for train, test in cv)    576 ~/anaconda3_501/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in __call__(self, iterable)    777             # was dispatched. In particular this covers the edge    778             # case of Parallel used with an exhausted iterator.--> 779             while self.dispatch_one_batch(iterator):    780                 self._iterating = True    781             else:~/anaconda3_501/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in dispatch_one_batch(self, iterator)    618     619         with self._lock:--> 620             tasks = BatchedCalls(itertools.islice(iterator, batch_size))    621             if len(tasks) == 0:    622                 # No more tasks available in the iterator: tell caller to stop.~/anaconda3_501/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in __init__(self, iterator_slice)    125     126     def __init__(self, iterator_slice):--> 127         self.items = list(iterator_slice)    128         self._size = len(self.items)    129 ~/anaconda3_501/lib/python3.6/site-packages/sklearn/grid_search.py in <genexpr>(.0)    573                                     error_score=self.error_score)    574                 for parameters in parameter_iterable--> 575                 for train, test in cv)    576     577         # Out is a list of triplet: score, estimator, n_test_samplesTypeError: 'TimeSeriesSplit' object is not iterable

我不确定为什么会这样,我怀疑这是我在最后一行进行拟合时发生的。希望能得到帮助。


回答:

首先,你使用了不兼容的软件包。grid_search是旧版本,已被弃用,不再与model_selection兼容。

替代以下代码:

from sklearn.grid_search import GridSearchCV

使用这个:

from sklearn.model_selection import GridSearchCV

其次,你只需要将TimeSeriesSplit(n_splits=3)发送给cv参数。像这样:

timeseries_split = TimeSeriesSplit(n_splits=3)clf = GridSearchCV(reg, param, cv=timeseries_split, scoring='neg_mean_squared_error')

不需要调用split()。网格搜索会内部调用它。

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注