首页 > 解决方案 > 如何在 Scikit-Learn 中获取 GridSearchCV() 的 OneVsRestClassifier(LinearSVC()) 的估计器键引用?

问题描述

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

这就是我准备 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.xmachine-learningscikit-learngrid-search

解决方案


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

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

推荐阅读