【问题标题】:How to go around the error "Invalid parameter estimator for estimator Pipeline" while using GridSearchCV with SVR?在将 GridSearchCV 与 SVR 结合使用时,如何解决错误“估计器管道的参数估计器无效”?
【发布时间】:2022-08-19 05:02:58
【问题描述】:

我正在尝试使用以下代码对最佳超参数进行 GridSearch:

search =GridSearchCV( 
    make_pipeline(RobustScaler(), 
                    SVR()#,
                    #cv=kf
                    #refit=True
                   ),
    param_grid = {
                \'estimator__svr__kernel\': (\'linear\', \'rbf\',\'poly\')#,
                #\'estimator__svr__C\':[ 10,20]
                #\'estimator__svr__gamma\': [1e-5, 3e-4 ],
                #\'estimator__svr__epsilon\':[0.001,0.002,0.006,0.008]#,
                # \'cv\' : [10]
                 },
    refit=True)

search.fit(train, target)

我收到此错误: ValueError:估计器管道的参数估计器无效(步骤= [(\'robustscaler\',RobustScaler()),(\'svr\',SVR())])。使用estimator.get_params().keys() 检查可用参数列表

该错误没有指出参数网格中的任何特定条目。此外,estimator.get_params().keys() 列出了我使用的参数:

dict_keys([\'cv\', \'error_score\', \'estimator__memory\', \'estimator__steps\', \'estimator__verbose\', \'estimator__robustscaler\', \'estimator__svr\', \'estimator__robustscaler__copy\', \'estimator__robustscaler__quantile_range\', \'estimator__robustscaler__unit_variance\', \'estimator__robustscaler__with_centering\', \'estimator__robustscaler__with_scaling\', \'estimator__svr__C\', \'estimator__svr__cache_size\', \'estimator__svr__coef0\', \'estimator__svr__degree\', \'estimator__svr__epsilon\', \'estimator__svr__gamma\', \'estimator__svr__kernel\', \'estimator__svr__max_iter\', \'estimator__svr__shrinking\', \'estimator__svr__tol\', \'estimator__svr__verbose\', \'estimator\', \'n_jobs\', \'param_grid\', \'pre_dispatch\', \'refit\', \'return_train_score\', \'scoring\', \'verbose\'])

param_grid 的任何组合似乎都不起作用。

    标签: python scikit-learn pipeline svm gridsearchcv


    【解决方案1】:

    我认为您应该为estimator__svr__kernel 使用方括号而不是括号:

    'estimator__svr__kernel': ['linear', 'rbf','poly']

    编辑:

    通过在参数网格中使用 svr__kernel 而不是 estimator__svr__kernel,我能够针对 iris 数据集运行您的脚本:

    from sklearn.preprocessing import RobustScaler
    from sklearn.model_selection import GridSearchCV
    from sklearn.pipeline import make_pipeline
    from sklearn.svm import SVR
    from sklearn import datasets
    
    iris = datasets.load_iris()
    X = iris.data[:, :2]
    y = iris.target
    
    search =GridSearchCV( 
    make_pipeline(RobustScaler(), 
                    SVR()#,
                    #cv=kf
                    #refit=True
                   ),
    param_grid = {'svr__kernel': ('linear', 'rbf','poly')},
    refit=True)
    
    search.fit(X, y)
    

    这将返回:

    GridSearchCV(estimator=Pipeline(steps=[('robustscaler', RobustScaler()),
                                           ('svr', SVR())]),
                 param_grid={'svr__kernel': ('linear', 'rbf', 'poly')})
    

    【讨论】:

    • 谢谢,我试过了,还是一样的错误。
    猜你喜欢
    • 2020-08-11
    • 2021-06-01
    • 2020-12-18
    • 2020-02-22
    • 2020-09-11
    • 2017-06-13
    • 2020-05-31
    • 2020-07-12
    • 2017-12-28
    相关资源
    最近更新 更多