python-3.x - 带有随机森林的 GridsearchCV

标签 python-3.x machine-learning random-forest grid-search

所以我正在使用 RandomForest 和 GridsearchCV 做一些参数处理。这是我的代码。

#Import 'GridSearchCV' and 'make_scorer'
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import make_scorer

Create the parameters list you wish to tune
parameters = {'n_estimators':[5,10,15]}

#Initialize the classifier
clf = GridSearchCV(RandomForestClassifier(), parameters)

#Make an f1 scoring function using 'make_scorer' 
f1_scorer = make_scorer(f1_scorer)

#Perform grid search on the classifier using the f1_scorer as the scoring method
grid_obj = GridSearchCV(clf, param_grid=parameters, scoring=f1_scorer,cv=5)

print(clf.get_params().keys())

#Fit the grid search object to the training data and find the optimal parameters
grid_obj = grid_obj.fit(X_train_100,y_train_100)

因此问题是以下错误:“ValueError:估算器 GridSearchCV 的参数 max_features 无效。使用 estimator.get_params().keys() 检查可用参数列表。”

我遵循了错误给出的建议, print(clf.get_params().keys()) 的输出如下。然而,即使我将这些标题复制并粘贴到参数字典中,我仍然收到错误。我一直在寻找堆栈溢出,大多数人都使用与我非常相似的参数字典。有人知道如何解决这个问题吗?再次感谢!

dict_keys(['pre_dispatch', 'cv', 'estimator__max_features', 'param_grid', 'refit', 'estimator__min_impurity_split', 'n_jobs', 'estimator__random_state', 'error_score', '详细', 'estimator__min_samples_split', 'estimator__n_jobs'、'fit_params'、'estimator__min_weight_fraction_leaf'、'评分'、'estimator__warm_start'、'estimator__criterion'、'estimator__verbose'、'estimator__bootstrap'、'estimator__class_weight'、'estimator__oob_score'、'iid'、'估计器', '估计器__max_深度', 'estimator__max_leaf_nodes', 'estimator__min_samples_leaf', 'estimator__n_estimators', 'return_train_score'])

最佳答案

我认为问题出在这两行上:

clf = GridSearchCV(RandomForestClassifier(), parameters)
grid_obj = GridSearchCV(clf, param_grid=parameters, scoring=f1_scorer,cv=5)

这本质上是创建一个具有如下结构的对象:

grid_obj = GridSearchCV(GridSearchCV(RandomForestClassifier()))

这可能比您想要的多一个 GridSearchCV

关于python-3.x - 带有随机森林的 GridsearchCV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45403482/

相关文章:

python - 如何提高 python 中的合并排序速度

python - sudo/suid 非根嵌套失败

python - 用两个物体做螺旋

随机森林(rfsrc 包): multivariate analysis

r - 利用可用数据并忽略缺失数据来构建分类器

python - 在 Python 中利用蒙特卡洛预测收入

machine-learning - 有没有办法增加预训练词嵌入的维度?

Python beam 搜索生成相同序列的 Keras LSTM 模型

machine-learning - 受限玻尔兹曼机中 "extracting 8x8 patches"背后的原因是什么?

machine-learning - 随机森林分类器 class_weight