我使用GridSearchCV时出现了什么错误?

2024-04-24 22:24:49 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用GridSearhCV为SVR找到最佳参数设置时,出现了一些错误。 下面是代码

from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVR
param_grid = [
    {'kernel':["poly"], 'degree':[2, 3, 4],'C':[0.001, 0.1, 0.5, 1, 5, 10, 50, 100], 'episilon':[0.1, 0.2, 0.4]},
    {'kernal':["rbf"], 'gamma':[1, 2, 5, 10], 'C':[0.001, 0.1, 0.5, 1, 5, 10, 50, 100]}
]
svm_reg = SVR()
grid_search = GridSearchCV(svm_reg, param_grid, cv=10, scoring = 'neg_mean_squared_error', return_train_score=True)
grid_search.fit(X_train, y_train)

输入数据X_列和y_列已正确处理。JupyterLab中显示的错误如下

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-52-39598b5bc032> in <module>
      7 svm_reg = SVR()
      8 grid_search = GridSearchCV(svm_reg, param_grid, cv=10, scoring = 'neg_mean_squared_error', return_train_score=True)
----> 9 grid_search.fit(X_train, y_train)

~\anaconda3\envs\jupyterlab\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

~\anaconda3\envs\jupyterlab\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
    839                 return results
    840 
--> 841             self._run_search(evaluate_candidates)
    842 
    843             # multimetric is determined here because in the case of a callable

~\anaconda3\envs\jupyterlab\lib\site-packages\sklearn\model_selection\_search.py in _run_search(self, evaluate_candidates)
   1286     def _run_search(self, evaluate_candidates):
   1287         """Search all candidates in param_grid"""
-> 1288         evaluate_candidates(ParameterGrid(self.param_grid))
   1289 
   1290 

~\anaconda3\envs\jupyterlab\lib\site-packages\sklearn\model_selection\_search.py in evaluate_candidates(candidate_params, cv, more_results)
    793                               n_splits, n_candidates, n_candidates * n_splits))
    794 
--> 795                 out = parallel(delayed(_fit_and_score)(clone(base_estimator),
    796                                                        X, y,
    797                                                        train=train, test=test,

~\anaconda3\envs\jupyterlab\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
   1039             # remaining jobs.
   1040             self._iterating = False
-> 1041             if self.dispatch_one_batch(iterator):
   1042                 self._iterating = self._original_iterator is not None
   1043 

~\anaconda3\envs\jupyterlab\lib\site-packages\joblib\parallel.py in dispatch_one_batch(self, iterator)
    857                 return False
    858             else:
--> 859                 self._dispatch(tasks)
    860                 return True
    861 

~\anaconda3\envs\jupyterlab\lib\site-packages\joblib\parallel.py in _dispatch(self, batch)
    775         with self._lock:
    776             job_idx = len(self._jobs)
--> 777             job = self._backend.apply_async(batch, callback=cb)
    778             # A job can complete so quickly than its callback is
    779             # called before we get here, causing self._jobs to

~\anaconda3\envs\jupyterlab\lib\site-packages\joblib\_parallel_backends.py in apply_async(self, func, callback)
    206     def apply_async(self, func, callback=None):
    207         """Schedule a func to be run"""
--> 208         result = ImmediateResult(func)
    209         if callback:
    210             callback(result)

~\anaconda3\envs\jupyterlab\lib\site-packages\joblib\_parallel_backends.py in __init__(self, batch)
    570         # Don't delay the application, to avoid keeping the input
    571         # arguments in memory
--> 572         self.results = batch()
    573 
    574     def get(self):

~\anaconda3\envs\jupyterlab\lib\site-packages\joblib\parallel.py in __call__(self)
    260         # change the default number of processes to -1
    261         with parallel_backend(self._backend, n_jobs=self._n_jobs):
--> 262             return [func(*args, **kwargs)
    263                     for func, args, kwargs in self.items]
    264 

~\anaconda3\envs\jupyterlab\lib\site-packages\joblib\parallel.py in <listcomp>(.0)
    260         # change the default number of processes to -1
    261         with parallel_backend(self._backend, n_jobs=self._n_jobs):
--> 262             return [func(*args, **kwargs)
    263                     for func, args, kwargs in self.items]
    264 

~\anaconda3\envs\jupyterlab\lib\site-packages\sklearn\utils\fixes.py in __call__(self, *args, **kwargs)
    220     def __call__(self, *args, **kwargs):
    221         with config_context(**self.config):
--> 222             return self.function(*args, **kwargs)

~\anaconda3\envs\jupyterlab\lib\site-packages\sklearn\model_selection\_validation.py in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, split_progress, candidate_progress, error_score)
    579             cloned_parameters[k] = clone(v, safe=False)
    580 
--> 581         estimator = estimator.set_params(**cloned_parameters)
    582 
    583     start_time = time.time()

~\anaconda3\envs\jupyterlab\lib\site-packages\sklearn\base.py in set_params(self, **params)
    228             key, delim, sub_key = key.partition('__')
    229             if key not in valid_params:
--> 230                 raise ValueError('Invalid parameter %s for estimator %s. '
    231                                  'Check the list of available parameters '
    232                                  'with `estimator.get_params().keys()`.' %

ValueError: Invalid parameter episilon for estimator SVR(C=0.001, degree=2). Check the list of available parameters with `estimator.get_params().keys()`.

据说我应该检查SVR的密钥,但我找不到错误所在


Tags: inpyselfsearchreturnparallellibpackages
1条回答
网友
1楼 · 发布于 2024-04-24 22:24:49

只是打字错误。在代码中:

param_grid = [
    {'kernel':["poly"], 'degree':[2, 3, 4],'C':[0.001, 0.1, 0.5, 1, 5, 10, 50, 100], 'episilon':[0.1, 0.2, 0.4]},
    {'kernal':["rbf"], 'gamma':[1, 2, 5, 10], 'C':[0.001, 0.1, 0.5, 1, 5, 10, 50, 100]}
]

请检查episilonkernal

相关问题 更多 >