如何在Scikit-Learn中获得GridSearchCV()的OneVsRestClassifier(LinearSVC())的估算器键参考?

时间:2019-09-13 21:47:03

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

我正在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()。如何找出正确的估算器密钥?


添加了以下代码以显示该字典的用法

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()"

2 个答案:

答案 0 :(得分:0)

IIUC,因为您要在OneVsRestClassifier()中嵌套估算器,所以现在必须使用estimator__C,因为OneVsRestClassifier()提供的属性保存LinearSVC()

像这样:


'SVC_OVR': {'clf': OneVsRestClassifier(LinearSVC()),
                                'param': [{
                                        'estimator__C': [100],
                                        'estimator__max_iter': [400, 6000],
                                }]},

答案 1 :(得分:0)

以下是引用的命名,没有错误:

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