您假设使用make_pipeline
以错误的方式。从文档 http://scikit-learn.org/stable/modules/generated/sklearn.pipeline.make_pipeline.html:-
这是 Pipeline 构造函数的简写;它不需要,
并且不允许命名估计器。相反,他们的名字将
自动设置为其类型的小写。
因此,这意味着当您提供 PCA 对象时,其名称将被设置为“pca”(小写),当您向其提供 RandomForestClassifier 对象时,它将被命名为“randomforestclassifier”,而不是您所想的“clf” 。
所以现在你制作的参数网格是无效的,因为它包含clf__
并且它不存在于管道中。
解决方案1:
替换这一行:
pca_clf = make_pipeline(pca, clf)
With
pca_clf = Pipeline([('pca', pca), ('clf', clf)])
解决方案2:
如果您不想更改pca_clf = make_pipeline(pca, clf)
行,然后替换所有出现的 clf 在你的parameters
像这样的“随机森林分类器”:
parameters = {'randomforestclassifier__n_estimators': [4, 6, 9],
'randomforestclassifier__max_features': ['log2', 'sqrt','auto'],
'randomforestclassifier__criterion': ['entropy', 'gini'],
'randomforestclassifier__max_depth': [2, 3, 5, 10],
'randomforestclassifier__min_samples_split': [2, 3, 5],
'randomforestclassifier__min_samples_leaf': [1,5,8] }
Sidenote:无需在代码中执行此操作:
clf = grid_RF.best_estimator_
clf.fit(X_train, y_train)
The best_estimator_
将已经安装了具有最佳找到参数的整个数据,因此您调用clf.fit()
是多余的。