类型错误:“ShuffleSplit"对象不可迭代 [英] TypeError: 'ShuffleSplit' object is not iterable

查看:79
本文介绍了类型错误:“ShuffleSplit"对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ShuffleSplit 来随机播放数据,但我发现有一个错误

I am using ShuffleSplit to shuffle data, but I found there is an error

TypeError                                 Traceback (most recent call last)
<ipython-input-36-192f7c286a58> in <module>()
      1 # Fit the training data to the model using grid search
----> 2 reg = fit_model(X_train, y_train)
      3 
      4 # Produce the value for 'max_depth'
      5 print "Parameter 'max_depth' is {} for the optimal model.".format(reg.get_params()['max_depth'])

<ipython-input-34-18b2799e585c> in fit_model(X, y)
     32 
     33     # Fit the grid search object to the data to compute the optimal model
---> 34     grid = grid.fit(X, y)
     35 
     36     # Return the optimal model after fitting the data

/Library/Python/2.7/site-packages/sklearn/grid_search.pyc in fit(self, X, y)
    827 
    828         """
--> 829         return self._fit(X, y, ParameterGrid(self.param_grid))
    830 
    831 

/Library/Python/2.7/site-packages/sklearn/grid_search.pyc in _fit(self, X, y, parameter_iterable)
    571                                     self.fit_params, return_parameters=True,
    572                                     error_score=self.error_score)
--> 573                 for parameters in parameter_iterable
    574                 for train, test in cv)
    575 

/Library/Python/2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self, iterable)
    756             # was dispatched. In particular this covers the edge
    757             # case of Parallel used with an exhausted iterator.
--> 758             while self.dispatch_one_batch(iterator):
    759                 self._iterating = True
    760             else:

/Library/Python/2.7/site-packages/sklearn/externals/joblib/parallel.pyc in dispatch_one_batch(self, iterator)
    601 
    602         with self._lock:
--> 603             tasks = BatchedCalls(itertools.islice(iterator, batch_size))
    604             if len(tasks) == 0:
    605                 # No more tasks available in the iterator: tell caller to stop.

/Library/Python/2.7/site-packages/sklearn/externals/joblib/parallel.pyc 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 

/Library/Python/2.7/site-packages/sklearn/grid_search.pyc in <genexpr>((parameters,))
    572                                     error_score=self.error_score)
    573                 for parameters in parameter_iterable
--> 574                 for train, test in cv)
    575 
    576         # Out is a list of triplet: score, estimator, n_test_samples

TypeError: 'ShuffleSplit' object is not iterable

它显示了警告

/Library/Python/2.7/site-packages/sklearn/cross_validation.py:44:DeprecationWarning:该模块在 0.18 版本中被弃用,取而代之的是 model_selection 模块,所有重构的类和函数都移入其中.另请注意,新的 CV 迭代器的接口与此模块的接口不同.此模块将在 0.20 中删除.此模块将在 0.20 中删除.",DeprecationWarning)/Library/Python/2.7/site-packages/sklearn/grid_search.py​​:43:DeprecationWarning:该模块在 0.18 版本中被弃用,取而代之的是 model_selection 模块,所有重构的类和函数都移入其中.此模块将在 0.20 中删除.弃用警告)

/Library/Python/2.7/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. "This module will be removed in 0.20.", DeprecationWarning) /Library/Python/2.7/site-packages/sklearn/grid_search.py:43: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. This module will be removed in 0.20. DeprecationWarning)

代码会导致错误

from sklearn.metrics import make_scorer
from sklearn.tree import DecisionTreeRegressor
from sklearn.grid_search import GridSearchCV

虽然下面没有

from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import make_scorer  

当前的 scikit-learn 版本是 0.18.1.

The current scikit-learn version is 0.18.1.

这是怎么发生的?

推荐答案

像这样从 grid_search 导入 GridSearchCV 时:

While importing GridSearchCV from grid_search like this:

from sklearn.grid_search import GridSearchCV

您必须收到如下警告:

这个模块在 0.18 版本中被弃用,以支持model_selection 模块,其中所有重构的类和功能移动.还要注意新简历的界面迭代器与本模块的迭代器不同.该模块将在 0.20 中移除.

This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.

这是不言自明的,并且符合您的体验.

This is self explanatory and conforms to what you are experiencing.

解决方案:-不要使用已弃用的 grid_search.使用新的model_selection.

这篇关于类型错误:“ShuffleSplit"对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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