问题描述
我正在使用流水线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',RandomForestRegressor(verbose=3))]). Check the list of available parameters with `estimator.get_params().keys()`.
我尝试删除'RandomForestRegressor_'前缀,问题仍然存在。而且我很高兴max_depth
实际上是RandomForestRegressor
解决方法
在管道中使用的
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']}