【问题标题】:Pipeline with RF, PCA and CV generating error带有 RF、PCA 和 CV 生成错误的管道
【发布时间】:2020-12-12 08:39:20
【问题描述】:

我正在拟合一个随机森林回归模型,使用流水线 PCA 和网格搜索进行超参数选择,但它以某种方式给了我一个错误。以下是我的代码:

params_rf = {'RandomForestRegressor__n_estimators': [300, 400, 500],
             'RandomForestRegressor__max_depth': [4, 6, 8],
             'RandomForestRegressor__min_samples_leaf': [0.1, 0.2],
             'RandomForestRegressor__max_features': ['log2', 'sqrt']}

pipe = Pipeline([('scaler', StandardScaler()),
                 ('reducer', PCA(n_components=50)),
                  ('regressor',RandomForestRegressor(verbose = 3))])

rf_cv = GridSearchCV(estimator = pipe,
                     param_grid = params_rf,
                     cv =3,
                     verbose=3)

rf_cv.fit(X_train,y_train)

错误信息:

Invalid parameter RandomForestRegressor_max_depth for estimator Pipeline(steps=[('scaler', StandardScaler()), ('reducer', PCA(n_components=50)),
                ('regressor', RandomForestRegressor(verbose=3))]). Check the list of available parameters with `estimator.get_params().keys()`.

我已尝试删除“RandomForestRegressor_”前缀,但问题仍然存在。而且我很喜欢max_depth 实际上是RandomForestRegressor 中的一个超参数

【问题讨论】:

    标签: scikit-learn pipeline random-forest cross-validation grid-search


    【解决方案1】:

    RandomForestRegressor,在您的管道中使用,已经有一个名称,regressor;你应该用这个名字来引用它,而不是RandomForestRegressor。将您的 params_rf 更改为:

    params_rf = {'regressor__n_estimators': [300, 400, 500],
                 'regressor__max_depth': [4, 6, 8],
                 'regressor__min_samples_leaf': [0.1, 0.2],
                 'regressor__max_features': ['log2', 'sqrt']}
    

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      • 2014-02-27
      • 2021-09-16
      • 1970-01-01
      • 2011-03-03
      • 2020-12-29
      相关资源
      最近更新 更多