网格搜索随机森林 "RandomForestClassifier instance is not fitted yet"
grid search random forests "RandomForestClassifier instance is not fitted yet"
我正在尝试对随机森林分类器进行网格搜索,我正在尝试测试不同的 PCA 组件和 n_estimators
model_rf = RandomForestClassifier()
pca_rf = Pipeline([('pca', PCA()), ('rf', RandomForestClassifier())])
param_grid_rf = [{
'pca__n_components': [20],
'rf__n_estimators': [5]
}]
grid_cv_rf = GridSearchCV(estimator=pca_rf, cv=5,param_grid=param_grid_rf)
grid_cv_rf.fit(x_train, y_train1)
test_pca_evaluate = pca.transform(x_test)
y_pred = model_rf.predict(test_pca_evaluate)#error here
最后一行出现错误"This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this method."
这是一个非常简单的错误 - 您正在调用其方法的 RandomForestClassifier
尚未适合,这意味着您尚未调用 model_rf.fit
。该对象不适合您的 grid_cv_rf
对象。
我想你想要的是 grid_cv_rf.predict(x_test)
,因为那个 grid_cv_rf
对象既可以进行 PCA 也可以进行 RF 拟合。
我正在尝试对随机森林分类器进行网格搜索,我正在尝试测试不同的 PCA 组件和 n_estimators
model_rf = RandomForestClassifier()
pca_rf = Pipeline([('pca', PCA()), ('rf', RandomForestClassifier())])
param_grid_rf = [{
'pca__n_components': [20],
'rf__n_estimators': [5]
}]
grid_cv_rf = GridSearchCV(estimator=pca_rf, cv=5,param_grid=param_grid_rf)
grid_cv_rf.fit(x_train, y_train1)
test_pca_evaluate = pca.transform(x_test)
y_pred = model_rf.predict(test_pca_evaluate)#error here
最后一行出现错误"This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this method."
这是一个非常简单的错误 - 您正在调用其方法的 RandomForestClassifier
尚未适合,这意味着您尚未调用 model_rf.fit
。该对象不适合您的 grid_cv_rf
对象。
我想你想要的是 grid_cv_rf.predict(x_test)
,因为那个 grid_cv_rf
对象既可以进行 PCA 也可以进行 RF 拟合。