标签:python scikit-learn cross-validation random-forest
我正在尝试将GridSearchCV用于RandomForestRegressor,但总是得到ValueError:找到带有暗淡100的数组.预计500.考虑这个玩具示例:
import numpy as np
from sklearn import ensemble
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import r2_score
if __name__ == '__main__':
X = np.random.rand(1000, 2)
y = np.random.rand(1000)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=1)
# Set the parameters by cross-validation
tuned_parameters = {'n_estimators': [500, 700, 1000], 'max_depth': [None, 1, 2, 3], 'min_samples_split': [1, 2, 3]}
# clf = ensemble.RandomForestRegressor(n_estimators=500, n_jobs=1, verbose=1)
clf = GridSearchCV(ensemble.RandomForestRegressor(), tuned_parameters, cv=5, scoring=r2_score, n_jobs=-1, verbose=1)
clf.fit(X_train, y_train)
print clf.best_estimator_
这就是我得到的:
Fitting 5 folds for each of 36 candidates, totalling 180 fits
Traceback (most recent call last):
File "C:\Users\abudis\Dropbox\machine_learning\toy_example.py", line 21, in <module>
clf.fit(X_train, y_train)
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\grid_search.py", line 596, in fit
return self._fit(X, y, ParameterGrid(self.param_grid))
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\grid_search.py", line 378, in _fit
for parameters in parameter_iterable
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\externals\joblib\parallel.py", line 653, in __call__
self.dispatch(function, args, kwargs)
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\externals\joblib\parallel.py", line 400, in dispatch
job = ImmediateApply(func, args, kwargs)
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\externals\joblib\parallel.py", line 138, in __init__
self.results = func(*args, **kwargs)
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\cross_validation.py", line 1240, in _fit_and_score
test_score = _score(estimator, X_test, y_test, scorer)
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\cross_validation.py", line 1296, in _score
score = scorer(estimator, X_test, y_test)
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\metrics\metrics.py", line 2324, in r2_score
y_type, y_true, y_pred = _check_reg_targets(y_true, y_pred)
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\metrics\metrics.py", line 65, in _check_reg_targets
y_true, y_pred = check_arrays(y_true, y_pred)
File "C:\Users\abudis\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\utils\validation.py", line 254, in check_arrays
% (size, n_samples))
ValueError: Found array with dim 100. Expected 500
出于某种原因,GridSearchCV认为n_estimators参数应该等于每个折叠的大小.如果我在tuned_parameters列表中更改n_estimators的第一个值,我会得到另一个期望值的ValueError.
使用clf = ensemble.RandomForestRegressor(n_estimators = 500,n_jobs = 1,verbose = 1)训练一个模型的工作正常,所以不确定我做错了什么或scikit中有一个错误 – 在某处学习.
解决方法:
看起来像一个错误,但在你的情况下,如果你使用RandomForestRegressor自己的得分手(巧合的是R ^ 2得分),它不应该在GridSearchCV中指定任何评分函数:
clf = GridSearchCV(ensemble.RandomForestRegressor(), tuned_parameters, cv=5,
n_jobs=-1, verbose=1)
编辑:正如@jnothman在#4081中所提到的,这是一个真正的问题:
scoring does not accept a metric function. It accepts a function of signature (estimator, > X, y_true=None) -> float score. You can use scoring=’r2′ or scoring=make_scorer(r2_score).
标签:python,scikit-learn,cross-validation,random-forest 来源: https://codeday.me/bug/20190628/1318913.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。