ValueError:使用GridSearchCV时,估算器SelectFromModel的参数C无效

时间:2020-03-31 14:19:52

标签: python scikit-learn pipeline logistic-regression grid-search

我正在使用python 3.7.6,并且正在尝试使用GridSearchCV调整一些超参数

我通过以下步骤创建了pipelinescaling-> feature selection -> model

但是我在功能选择步骤的C参数上遇到了错误。

    steps = [('scaler', StandardScaler()),
             ('FeatureSelection', SelectFromModel(LogisticRegression(penalty='l1', solver='liblinear'))),
             ('SVM', SVC())]
    pipeline = Pipeline(steps)  # define the pipeline object.

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=30, stratify=y)

    parameteres = {'SVM__C': [0.001, 0.1, 10, 100, 10e5],
                   'SVM__gamma':[0.1,0.01],
                   'FeatureSelection__C':['0','0.5']}
    grid = GridSearchCV(pipeline, param_grid=parameteres, cv=5, n_jobs=-1)
    grid.fit(X_train, y_train)
    print("pipeline score: ", grid.score(X_test, y_test))

我遇到以下错误:

ValueError: Invalid parameter C for estimator SelectFromModel(estimator=LogisticRegression(C=1.0, class_weight=None,
                                             dual=False, fit_intercept=True,
                                             intercept_scaling=1, l1_ratio=None,
                                             max_iter=100, multi_class='auto',
                                             n_jobs=None, penalty='l1',
                                             random_state=None,
                                             solver='liblinear', tol=0.0001,
                                             verbose=0, warm_start=False),
                max_features=None, norm_order=1, prefit=False, threshold=None). Check the list of available parameters with `estimator.get_params().keys()`.

出什么问题了,我该如何解决?

1 个答案:

答案 0 :(得分:1)

照原样,管道在C中查找参数SelectFromModel,找不到一个参数(不足为奇,因为模块没有这样的参数),并引发错误。由于您实际上想要C的参数LogisticRegression,因此您应该更深入一点:在FeatureSelection__C网格中将FeatureSelection__estimator__C更改为parameters,就可以了。