随机森林:网格搜索运行时错误

时间:2017-07-24 07:22:57

标签: python scikit-learn random-forest grid-search

我正在通过sklearn使用GridSearchCV调整我的RandomForestClassifier。 我只调整两个参数:

“n_estimators”和“min_samples_leaf”。

这是代码的一部分:

RFC=RandomForestClassifier(criterion='entropy', random_state=42, max_features='log2', warm_start=True, oob_score=True, n_jobs=-1)
our_param_grid = [ {'n_estimators':[400, 500, 600, 700]}, {'min_samples_leaf':[50, 70, 90, 110, 130, 150, 170, 190]} ]
grid = GridSearchCV(estimator = RFC, param_grid = our_param_grid, cv=3)
grid.fit(X, Y)
print grid.best_params_

我收到有关OOB_Score的警告和错误。

这是我得到的警告。

Warning: Some inputs do not have OOB scores. This probably means too few trees where used to compute any reliable oob estimators.                                                                                                                  
warn("Some inputs do not have OOB scores. "

然后弹出一个错误,导致程序终止。这是我得到的确切错误。

RuntimeWarning: invalid value encountered in true_divide predictions[k].sum(axis=1)[:, np.newaxis])
{'n_estimators': 700}

我在制作RFC模型时设置了oob_score = True。任何人都可以帮助我理解我出错的地方。

1 个答案:

答案 0 :(得分:0)

继续发表评论......

您可以使用以下内容并尝试运行GridSearchCV吗?

our_param_grid = {'n_estimators':[400, 500, 600, 700] , 'min_samples_leaf':[50, 70, 90, 110, 130, 150, 170, 190]}

说明:

我认为这是问题,因为GridSearchCV希望param_grid成为字典。

在您的情况下,您有一个列表,似乎在n_estimator = 700之后,它会卡住并且无法“看到”min_samples_leaf