Search code examples
pythonscikit-learnpipelinegrid-searchgridsearchcv

Invalid parameter for sklearn pipeline


I know this appears to be a common problem and that it's based on the specific name of the parameters, but I'm still getting an error after looking at the keys.

steps=[('classifier', svm.SVC(decision_function_shape="ovo"))]

pipeline = Pipeline(steps)

# Specify the hyperparameter space
parameters = {'estimator__classifier__C':[1, 10, 100],
              'estimator__classifier__gamma':[0.001, 0.0001]}

# Instantiate the GridSearchCV object: cv
SVM = GridSearchCV(pipeline, parameters, cv = 5)

_ = SVM.fit(X_train,y_train)

Which I then get: ValueError: Invalid parameter estimator for estimator ... Check the list of available parameters with `estimator.get_params().keys()`.

So I then look at SVM.get_params().keys() and get the following group, including the two I'm using. What am I missing?

cv error_score estimator__memory estimator__steps estimator__verbose estimator__preprocessor estimator__classifier estimator__preprocessor__n_jobs estimator__preprocessor__remainder estimator__preprocessor__sparse_threshold estimator__preprocessor__transformer_weights estimator__preprocessor__transformers estimator__preprocessor__verbose estimator__preprocessor__scale estimator__preprocessor__onehot estimator__preprocessor__scale__memory estimator__preprocessor__scale__steps estimator__preprocessor__scale__verbose estimator__preprocessor__scale__scaler estimator__preprocessor__scale__scaler__copy estimator__preprocessor__scale__scaler__with_mean estimator__preprocessor__scale__scaler__with_std estimator__preprocessor__onehot__memory estimator__preprocessor__onehot__steps estimator__preprocessor__onehot__verbose estimator__preprocessor__onehot__onehot estimator__preprocessor__onehot__onehot__categories estimator__preprocessor__onehot__onehot__drop estimator__preprocessor__onehot__onehot__dtype estimator__preprocessor__onehot__onehot__handle_unknown estimator__preprocessor__onehot__onehot__sparse estimator__classifier__C estimator__classifier__break_ties estimator__classifier__cache_size estimator__classifier__class_weight estimator__classifier__coef0 estimator__classifier__decision_function_shape estimator__classifier__degree estimator__classifier__gamma estimator__classifier__kernel estimator__classifier__max_iter estimator__classifier__probability estimator__classifier__random_state estimator__classifier__shrinking estimator__classifier__tol estimator__classifier__verbose estimator iid n_jobs param_grid pre_dispatch refit return_train_score scoring verbose


Solution

  • Your param grid should be classifier__C and classifier__gamma. You just need to get rid of estimator in the front because you named your SVC estimator as classifier in your pipeline.

    parameters = {'classifier__C':[1, 10, 100],
                  'classifier__gamma':[0.001, 0.0001]}