【问题标题】:This RandomForestClassifier instance is not fitted yet此 RandomForestClassifier 实例尚未安装
【发布时间】:2021-12-19 16:17:10
【问题描述】:

我正在使用 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.

【问题讨论】:

  • 我没有看到你在任何地方都适合这个模型。

标签: python scikit-learn


【解决方案1】:

cross_val_predict的源代码看,这种方法计算预测的时候估计是克隆了,所以forest_clf在运行cross_val_predict后将无法拟合。

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

forest_clf.fit(X_train, y_train)

打电话之前

final_predictions = forest_clf.predict(test_prepared)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2018-12-26
    • 2019-03-10
    • 2019-12-13
    • 2021-08-28
    • 2019-10-05
    相关资源
    最近更新 更多