GridsearchCV sklearn中的错误

时间:2015-04-21 15:36:44

标签: python machine-learning scikit-learn grid-search

我正在尝试使用GridsearchCV调整sklearn中的GB分类器。这是代码:

from sklearn.grid_search import GridSearchCV
from sklearn.ensemble import GradientBoostingClassifier

param_grid = {'learning_rate': [0.1, 0.01, 0.001],
              'max_depth': [4, 6],
              'min_samples_leaf': [9, 17],
              'max_features': [0.3, 0.1]}

est = GradientBoostingClassifier(n_estimators=3000)
# this may take some minutes
gs_cv = GridSearchCV(est, param_grid, scoring='f1', n_jobs=-1, verbose=1, pre_dispatch=5).fit(X.values, y)

# best hyperparameter setting
print 'Best hyperparameters: %r' % gs_cv.best_params_

数据集X是100万行* 245个特征。我在一台接近32核的机器上运行。运行上面的代码时出现以下错误,

error                                     Traceback (most recent call last)
<ipython-input-22-cb545fec9989> in <module>()
      9 est = GradientBoostingClassifier(n_estimators=3000)
     10 # this may take some minutes
---> 11 gs_cv = GridSearchCV(est, param_grid, scoring='f1', n_jobs=-1, verbose=1, pre_dispatch=5).fit(X.values, y)
     12 
     13 # best hyperparameter setting

/var/webeng/opensource/aetna-anaconda/lib/python2.7/site-packages/sklearn/grid_search.pyc in fit(self, X, y)
    594 
    595         """
--> 596         return self._fit(X, y, ParameterGrid(self.param_grid))
    597 
    598 

/var/webeng/opensource/aetna-anaconda/lib/python2.7/site-packages/sklearn/grid_search.pyc in _fit(self, X, y, parameter_iterable)
    376                                     train, test, self.verbose, parameters,
    377                                     self.fit_params, return_parameters=True)
--> 378             for parameters in parameter_iterable
    379             for train, test in cv)
    380 

/var/webeng/opensource/aetna-anaconda/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self, iterable)
    658                 # consumption.
    659                 self._iterating = False
--> 660             self.retrieve()
    661             # Make sure that we get a last message telling us we are done
    662             elapsed_time = time.time() - self._start_time

/var/webeng/opensource/aetna-anaconda/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in retrieve(self)
    510                 self._lock.release()
    511             try:
--> 512                 self._output.append(job.get())
    513             except tuple(self.exceptions) as exception:
    514                 try:

/var/webeng/opensource/aetna-anaconda/lib/python2.7/multiprocessing/pool.pyc in get(self, timeout)
    556             return self._value
    557         else:
--> 558             raise self._value
    559 
    560     def _set(self, i, obj):

error: 'i' format requires -2147483648 <= number <= 2147483647

当我使用1000行的子集运行相同的代码时,它可以工作。尝试了不同的pre_dispatch但仍然遇到问题。是因为数据大小还是别的什么?感谢。

在Python 2.7.9上使用sklearn 0.15.2

1 个答案:

答案 0 :(得分:0)

我认为有3种方法可以解决这个问题:

1)尝试将sklearn更新为最新版本

2)尝试替换

from sklearn.grid_search import GridSearchCV

使用:

from sklearn.model_selection import GridSearchCV

3)如果你想使用 n_jobs&gt; 1 在GridSearchCV中,您必须使用保护脚本,如果__name__ =='__ main __':

e.g。

if __name__ == '__main__':
    clf = MLPClassifier()
    my_param_grid = {'activation': ('tanh', 'relu')}
    grid= model_selection.GridSearchCV(clf,   
    param_grid=my_param_grid,n_jobs=-1)
    grid.fit(X, y)

考虑完成所有3个步骤