【问题标题】:grid search random forests "RandomForestClassifier instance is not fitted yet"网格搜索随机森林“尚未安装 RandomForestClassifier 实例”
【发布时间】:2019-05-06 11:46:12
【问题描述】:

我正在尝试对随机森林分类器进行网格搜索,我正在尝试测试不同的 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

在最后一行中,我收到一条错误消息“尚未安装此 RandomForestClassifier 实例。在使用此方法之前,请使用适当的参数调用 'fit'。”

【问题讨论】:

    标签: machine-learning scikit-learn random-forest grid-search


    【解决方案1】:

    这是一个非常简单的错误 - 您正在调用其方法的 RandomForestClassifier 尚未适合,这意味着您尚未调用 model_rf.fit。您的 grid_cv_rf 对象不适合该对象。

    我认为您想要的是 grid_cv_rf.predict(x_test),因为 grid_cv_rf 对象既可以进行 PCA 拟合,也可以进行 RF 拟合。

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 2022-01-04
      • 2017-12-29
      • 1970-01-01
      • 2015-11-27
      • 2011-11-28
      • 2019-06-09
      • 2017-01-12
      • 2013-06-26
      相关资源
      最近更新 更多