gpt4 book ai didi

python - GridsearchCV sklearn 中的错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:17 25 4
gpt4 key购买 nike

我正在尝试使用 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

最佳答案

我看到了 3 种可能的方法来解决这个问题:

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

2)尝试替换

from sklearn.grid_search import GridSearchCV

与:

from sklearn.model_selection import GridSearchCV

3) 如果你想在 GridSearchCV 中使用 n_jobs > 1 那么你必须使用 if __name__ == '__main__':

来保护脚本

例如

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 个步骤

关于python - GridsearchCV sklearn 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29776822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com