此 RandomForestClassifier 实例尚未安装

This RandomForestClassifier instance is not fitted yet

我正在使用 Rendom Forest 进行预测

from sklearn.ensemble import RandomForestClassifier
forest_clf = RandomForestClassifier(random_state=42)
y_train_pred = cross_val_predict(forest_clf, X_train, y_train, cv=3)

对于 y_train_pred 系统显示 95% 的精度

我正在使用相同的模型进行实际预测

test_stocks        = test_set.drop("is4PercPrft", axis=1)
test_stocks_labels = test_set["is4PercPrft"].copy()
test_prepared = full_pipeline.transform(test_stocks)
test_stocks_labels = test_stocks_labels.astype(np.uint8)
test_stocks_labels.value_counts()

对于下面的行我收到错误

final_predictions = forest_clf.predict(test_prepared)

This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

cross_val_predict的源代码来看,这种方法计算预测时似乎克隆了estimator,所以forest_clf不会在运行之后拟合cross_val_predict.

您需要训练实际使用的估算器:

forest_clf.fit(X_train, y_train)

调用前

final_predictions = forest_clf.predict(test_prepared)