首页 > 解决方案 > 类型错误:GridSearchCV 问题

问题描述

我正在尝试使用 GridSearchCV 和 Pipeline 来检查 SVM 的参数。代码如下。

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)

我为 xtrain 和 ytrain 加载了一些 .mat 文件。问题是在“grid.fit(xtrain,ytrain1)”行中生成的。

产生的错误如下

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'

通过使用参数={'svm_C':(0.1, 1, 10, 100), 'svm_gamma':(0.001, 0.01, 0.1, 10)} pipe=Pipeline([("scaler", StandardScaler()),错误出来

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()`.

标签: python-3.xscikit-learnsvm

解决方案


我已经以这种方式完成了您想做的事情,并且它正在工作:

    # 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]}

我看到您的代码有两个不同之处:

  1. steps在您编写的管道实例化中定义时StandardScaler,不带括号:

    你的代码是:

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

    在我编写的代码StandardScaler()中,请看:

    steps = [('scaler', StandardScaler()),('SVM', SVC())]
    pipeline = Pipeline(steps)
    
  2. 第二个区别是当您为参数 grid 指定键时parameters。您提供键:'svm_C''svm_gamma'而不是'SVM__C''SVM__gamma'分别。

您需要double-under_square _+_,这是 , __,正如回答您问题的第一个人之前所说的那样。

所以,改为写:

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

你应该写

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

我什至得到了一个错误,'svm__c'而不是'SVM__C''svm__gamma'而不是'SVM__gamma'。这最后我不知道为什么。

我希望,它有帮助


推荐阅读