2
votes

I am trying to use GridSearchCV and Pipeline to check parameters of SVM. The code is as follows.

parameter={'svm_C':(0.1, 1, 10, 100), 'svm_gamma':(0.001, 0.01, 0.1, 10)}
pipe=Pipeline([("scaler", StandardScaler), ("svm", SVC())])
print(parameter)
print(pipe)

print(xtrain.shape)
print(ytrain1.shape)
grid=GridSearchCV(pipe, parameter, cv=3, n_jobs=-1)
grid.fit(xtrain,ytrain1)
print("Best set score:{}".format(grid.best_score_))
print("Test set Score:{}".format(grid.score(xtest,ytest1)))
print("Best paameters:{}".format(grid.best_params_))
filename='finalized_svm.sav'
filename1='gridfinal_svm.sav'
joblib.dump(best_estimator_, filename)
joblib.dump(grid, filename1)
pred=grid.predict(xtest)
confusion=confusion_matrix(ytest1,pred), print(confusion)

I loaded some .mat files for xtrain and ytrain. The issue is generated in line "grid.fit(xtrain,ytrain1)".

The error generated is as follows

Traceback (most recent call last):
  File "C:/code net/mysvm.py", line 44, in <module>
    grid.fit(xtrain,ytrain1)
  File "C:\Users\Manisha\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\model_selection\_search.py", line 626, in fit
    base_estimator = clone(self.estimator)
  File "C:\Users\Manisha\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\base.py", line 62, in clone
    new_object_params[name] = clone(param, safe=False)
  File "C:\Users\Manisha\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\base.py", line 50, in clone
    return estimator_type([clone(e, safe=safe) for e in estimator])
  File "C:\Users\Manisha\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\base.py", line 50, in <listcomp>
    return estimator_type([clone(e, safe=safe) for e in estimator])
  File "C:\Users\Manisha\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\base.py", line 50, in clone
    return estimator_type([clone(e, safe=safe) for e in estimator])
  File "C:\Users\Manisha\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\base.py", line 50, in <listcomp>
    return estimator_type([clone(e, safe=safe) for e in estimator])
  File "C:\Users\Manisha\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\base.py", line 60, in clone
    new_object_params = estimator.get_params(deep=False)
TypeError: get_params() missing 1 required positional argument: 'self'

By using parameter={'svm_C':(0.1, 1, 10, 100), 'svm_gamma':(0.001, 0.01, 0.1, 10)} pipe=Pipeline([("scaler", StandardScaler()), the error comes out

Traceback (most recent call last):
  File "C:\Users\Manisha\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\externals\joblib\externals\loky\process_executor.py", line 420, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "C:\Users\Manisha\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 563, in __call__
    return self.func(*args, **kwargs)
  File "C:\Users\Manisha\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\externals\joblib\parallel.py", line 261, in __call__
    for func, args, kwargs in self.items]
  File "C:\Users\Manisha\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\externals\joblib\parallel.py", line 261, in <listcomp>
    for func, args, kwargs in self.items]
  File "C:\Users\Manisha\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\model_selection\_validation.py", line 514, in _fit_and_score
    estimator.set_params(**parameters)
  File "C:\Users\Manisha\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\pipeline.py", line 147, in set_params
    self._set_params('steps', **kwargs)
  File "C:\Users\Manisha\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\utils\metaestimators.py", line 52, in _set_params
    super(_BaseComposition, self).set_params(**params)
  File "C:\Users\Manisha\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\base.py", line 213, in set_params
    (key, self))
ValueError: Invalid parameter svm_C for estimator Pipeline(memory=None,
     steps=[('scaler', StandardScaler(copy=True, with_mean=True, with_std=True)), ('svm', SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
  kernel='rbf', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False))]). Check the list of available parameters with `estimator.get_params().keys()`.
"""

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-7-83184f586b10> in <module>
      2 print(ytrain1.shape)
      3 grid1=GridSearchCV(pipe, parameter, cv=3, n_jobs=-1)
----> 4 grid1.fit(xtrain,ytrain1)
      5 print("Best set score:{}".format(grid.best_score_))
      6 print("Test set Score:{}".format(grid.score(xtest,ytest1)))

~\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
    720                 return results_container[0]
    721 
--> 722             self._run_search(evaluate_candidates)
    723 
    724         results = results_container[0]

~\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\model_selection\_search.py in _run_search(self, evaluate_candidates)
   1189     def _run_search(self, evaluate_candidates):
   1190         """Search all candidates in param_grid"""
-> 1191         evaluate_candidates(ParameterGrid(self.param_grid))
   1192 
   1193 

~\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\model_selection\_search.py in evaluate_candidates(candidate_params)
    709                                for parameters, (train, test)
    710                                in product(candidate_params,
--> 711                                           cv.split(X, y, groups)))
    712 
    713                 all_candidate_params.extend(candidate_params)

~\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self, iterable)
    994 
    995             with self._backend.retrieval_context():
--> 996                 self.retrieve()
    997             # Make sure that we get a last message telling us we are done
    998             elapsed_time = time.time() - self._start_time

~\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\externals\joblib\parallel.py in retrieve(self)
    897             try:
    898                 if getattr(self._backend, 'supports_timeout', False):
--> 899                     self._output.extend(job.get(timeout=self.timeout))
    900                 else:
    901                     self._output.extend(job.get())

~\AppData\Local\conda\conda\envs\tfp36\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py in wrap_future_result(future, timeout)
    515         AsyncResults.get from multiprocessing."""
    516         try:
--> 517             return future.result(timeout=timeout)
    518         except LokyTimeoutError:
    519             raise TimeoutError()

~\AppData\Local\conda\conda\envs\tfp36\lib\concurrent\futures\_base.py in result(self, timeout)
    430                 raise CancelledError()
    431             elif self._state == FINISHED:
--> 432                 return self.__get_result()
    433             else:
    434                 raise TimeoutError()

~\AppData\Local\conda\conda\envs\tfp36\lib\concurrent\futures\_base.py in __get_result(self)
    382     def __get_result(self):
    383         if self._exception:
--> 384             raise self._exception
    385         else:
    386             return self._result

ValueError: Invalid parameter svm_C for estimator Pipeline(memory=None,
     steps=[('scaler', StandardScaler(copy=True, with_mean=True, with_std=True)), ('svm', SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
  kernel='rbf', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False))]). Check the list of available parameters with `estimator.get_params().keys()`.
1
Have you looked this question?Hemerson Tacon
You are doing ("scaler", StandardScaler). Here StandardScaler is a class, not an instance. You need to instantiate it by using () after it. Like this: ("scaler", StandardScaler()), just as you correctly did with the SVC after it.Vivek Kumar
I tried ("scaler", StandardScaler()), then i get above mentioned error.M. Jangra
Now this becomes another question: Here's the answer: stackoverflow.com/questions/41899132/…Vivek Kumar

1 Answers

0
votes

I have done what you want to do in this way, and it is working:

    # Setup the pipeline
    steps = [('scaler', StandardScaler()),
             ('SVM', SVC())]

    pipeline = Pipeline(steps)

    # Specify the hyperparameter space
    parameters = {'SVM__C':[1, 10, 100],
                  'SVM__gamma':[0.1, 0.01]}

I see 2 differences respect to your code:

  1. When defining the steps in the pipeline instantination you write StandardScaler, without parenthesis:

    Your code is:

    pipe=Pipeline([("scaler", StandardScaler), ("svm", SVC())])
    

    In my code I write StandardScaler(), look:

    steps = [('scaler', StandardScaler()),('SVM', SVC())]
    pipeline = Pipeline(steps)
    
  2. The second difference is that when you specify the keys for your parameters grid parameters. You provide the keys: 'svm_C', 'svm_gamma' instead of 'SVM__C' and 'SVM__gamma' respectively.

You need double-under_square _+_ , this is, __ , as the 1st person that answered your question said before.

So, instead to write :

    parameter={'svm_C':(0.1, 1, 10, 100), 'svm_gamma':(0.001, 0.01, 0.1, 10)}

You should write

    parameters = {'SVM__C':[1, 10, 100],'SVM__gamma':[0.1, 0.01]}

I even have gotten an error writting 'svm__c' instead of 'SVM__C' and 'svm__gamma' instead of 'SVM__gamma'. This last I do not why.

I hope, it helps