ValueError

来源:4-6 网格搜索与k近邻算法中更多超参数

慕斯卡4676759

2021-06-29

param_grid = [
{‘wights’:[‘uniform’],
‘n_neighbors’:[i for i in range(1,11)]
},
{
‘wights’:[‘distance’],
‘n_neighbors’:[i for i in range(1,11)],
‘p’:[i for i in range(1,6)]
}
]
knn_clf = KNeighborsClassifier()
from sklearn.model_selection import GridSearchCV
grid_search = GridSearchCV(knn_clf,param_grid)
grid_search.fit(X_train,y_train)
执行fit后报错
ValueError Traceback (most recent call last)
in
----> 1 grid_search.fit(X_train,y_train)

~/opt/anaconda3/lib/python3.8/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

~/opt/anaconda3/lib/python3.8/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

~/opt/anaconda3/lib/python3.8/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

~/opt/anaconda3/lib/python3.8/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,

~/opt/anaconda3/lib/python3.8/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

~/opt/anaconda3/lib/python3.8/site-packages/joblib/parallel.py in dispatch_one_batch(self, iterator)
857 return False
858 else:
–> 859 self._dispatch(tasks)
860 return True
861

~/opt/anaconda3/lib/python3.8/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

~/opt/anaconda3/lib/python3.8/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)

~/opt/anaconda3/lib/python3.8/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):

~/opt/anaconda3/lib/python3.8/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

~/opt/anaconda3/lib/python3.8/site-packages/joblib/parallel.py in (.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

~/opt/anaconda3/lib/python3.8/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)

~/opt/anaconda3/lib/python3.8/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()

~/opt/anaconda3/lib/python3.8/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 wights for estimator KNeighborsClassifier(n_neighbors=1). Check the list of available parameters with estimator.get_params().keys().

写回答

1回答

liuyubobobo

2021-06-29

参数名称 weight 的拼写有错。


继续加油!:)

0
0

Python3入门机器学习 经典算法与应用  

Python3+sklearn,兼顾原理、算法底层实现和框架使用。

5839 学习 · 2437 问题

查看课程