首页 > 解决方案 > 使用 Scikit-Learn 在 RegressorChain 上进行 GridSearch?

问题描述

我目前正在研究一个多输出回归问题,我试图一次预测多个输出值。我知道有标准的回归器本身就支持这项任务。

但是,我想使用 aRegressorChain并调整 RegressorChain 中 Regressor 的超参数GridSearchCV。我为此编写了以下代码:

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV

# setup the pipeline
pipeline = Pipeline(steps = [('scale', StandardScaler(with_mean=True, with_std=True)),
                             ('estimator', RegressorChain(SVR())])

# setup the parameter grid
param_grid = {'estimator__estimator__C': [0.1,1,10,100]}           

# setup the grid search
grid = GridSearchCV(pipeline, 
                    param_grid, 
                    scoring='neg_mean_squared_error')
# fit model   
grid.fit(X, y)

它尝试过:

param_grid = {'estimator__C': [0.1,1,10,100]}  

和:

param_grid = {'estimator__estimator__C': [0.1,1,10,100]}

但我两次得到以下结果ValueError

ValueError: Invalid parameter C for estimator RegressorChain(base_estimator=SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto_deprecated', kernel='rbf', max_iter=-1,收缩=True,tol=0.001,详细=False),cv=None,order=None,random_state=None)。使用 来检查可用参数列表estimator.get_params().keys()

有谁知道如何正确设置此管道?谢谢!

标签: pythonscikit-learnregressiongridsearchcv

解决方案


如错误消息所示,打印结果,RegressorChain(SVR()).get_params()您将获得:

{
    'base_estimator__C': 1.0, 
    'base_estimator__cache_size': 200, 
    'base_estimator__coef0': 0.0, 
    'base_estimator__degree': 3,
    ...
}

鉴于您定义的管道,这意味着您应该使用

param_grid = {'estimator__base_estimator__C': [0.1, 1, 10, 100]} 

在网格搜索的迭代过程中C为您的对象设置可能的值。SVR


推荐阅读