【问题标题】:How do I get the estimator key reference to OneVsRestClassifier(LinearSVC()) for GridSearchCV() in Scikit-Learn?如何在 Scikit-Learn 中获取 GridSearchCV() 的 OneVsRestClassifier(LinearSVC()) 的估算器键参考?
【发布时间】:2020-01-15 17:55:17
【问题描述】:

我正在 scikit-learn 中对GridSearchCV 的超参数进行网格搜索。

这就是我准备 ML 算法及其要搜索的相关参数的方式。 LogisticRegression()RandomForestClassifier() 分别用它们正确的估计键 logisticregression__randomforestclassifier__ 指定。

ml_algo_param_dict = \
                {   'LR_OVR': {'clf': LogisticRegression(),
                                'param': [{
                                    'logisticregression__solver': ['lbfgs', 'liblinear'],
                                    'logisticregression__penalty': ['l2'],
                                    'logisticregression__C': [0.1, 1, 10],
                                    'logisticregression__class_weight': [None],
                                    'logisticregression__multi_class': ['ovr'],
                                    'logisticregression__max_iter': [1000, 4000],
                                }, {
                                    'logisticregression__solver': ['newton-cg'],
                                    'logisticregression__penalty': ['l2'],
                                    'logisticregression__C': [0.1, 1, 10],
                                    'logisticregression__class_weight': [None],
                                    'logisticregression__multi_class': ['ovr'],
                                    'logisticregression__max_iter': [1000, 4000],
                                }]},
                    'RF_OVR': {'clf': RandomForestClassifier(),
                                'param': [{
                                    'randomforestclassifier__n_estimators': [100],
                                    'randomforestclassifier__max_depth': [150, 200],
                                    'randomforestclassifier__random_state': [888],
                                }]},
                    'SVC_OVR': {'clf': OneVsRestClassifier(LinearSVC()),
                                'param': [{
                                        'onevsrestclassifier_linearsvc__C': [100],
                                        'onevsrestclassifier_linearsvc__max_iter': [400, 6000],
                                }]},

但是OneVsRestClassifier(LinearSVC()) 呢?我尝试了很多方法(即onevsrestclassifier_linearsvc__onevsrestclassifier__linearsvc__),但一直收到错误Check the list of available parameters with estimator.get_params().keys()。如何找到正确的估算器键?


添加以下代码以显示 dict 的使用方式

transformer_num = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='median')),
    ('scaler', StandardScaler())])

transformer_cat = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='constant', fill_value='')),
    ('onehotencoder', OneHotEncoder(handle_unknown='ignore'))])

preprocessor = ColumnTransformer(
    transformers=[
        ('num', transformer_num, feature_list_num),
        ('cat', transformer_cat, feature_list_cat),
        ])

for algo_key, algo_val in ml_algo_param_dict.items():
    f1 = make_scorer(f1_score , average='micro')
    pipe = make_pipeline(preprocessor, algo_val['clf'])
    grid = GridSearchCV(pipe, algo_val['param'], n_jobs=-1, cv=5, scoring=f1, refit=True)
    grid.fit(X_train, y_train)

我试过'onevsrestclassifier_linearsvc__C', onevsrestclassifier_linearsvc_estimator__C', 'onevsrestclassifier__C', 'linearsvc__C', 'onevsrestclassifier__linearsvc__C', 'onevsrestclassifier-linearsvc__C', 'onevsrestclassifier_linearsvc_estimator__C', 'estimator__C',但都给了我同样的错误Check the list of available parameters with "estimator.get_params().keys()"

【问题讨论】:

    标签: python-3.x machine-learning scikit-learn grid-search


    【解决方案1】:

    以下是正常工作的引用命名:

    'SVC_OVR': {'clf': OneVsRestClassifier(LinearSVC()),
                'param': [{
                    'onevsrestclassifier__estimator__C': [1, 10],
                    'onevsrestclassifier__estimator__max_iter': [10000],
                          }]},
    

    【讨论】:

      猜你喜欢
      • 2017-03-14
      • 2014-07-28
      • 2019-12-04
      • 2019-08-18
      • 2020-03-02
      • 2020-09-25
      • 2013-10-01
      • 2020-03-24
      • 2017-09-09
      相关资源
      最近更新 更多