下面是我的代码:
tuned_parameters = [
{'kernel': ['linear], 'C':[1, 10], 'class_weight': ['auto']}, {'kernel': ['rbf'], 'C':[1,10], 'class_weight':['auto']}]
clf = GridSearchCV(svm.SVC(), tuned_parameters, cv=5, scoring='accuracy')
clf.fit(x_train,y_train)
但是出现以下错误:
Traceback (most recent call last):
File "/home/arajabi/PycharmProjects/Muffin/classification.py", line 77, in <module>
clf3.fit(x_train, y_train)
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/model_selection/_search.py", line 639, in fit
cv.split(X, y, groups)))
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py", line 779, in __call__
while self.dispatch_one_batch(iterator):
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py", line 625, in dispatch_one_batch
self._dispatch(tasks)
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py", line 588, in _dispatch
job = self._backend.apply_async(batch, callback=cb)
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 111, in apply_async
result = ImmediateResult(func)
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 332, in __init__
self.results = batch()
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py", line 131, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py", line 131, in <listcomp>
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/model_selection/_validation.py", line 458, in _fit_and_score
estimator.fit(X_train, y_train, **fit_params)
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/svm/base.py", line 150, in fit
y = self._validate_targets(y)
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/svm/base.py", line 502, in _validate_targets
self.class_weight_ = compute_class_weight(self.class_weight, cls, y_)
File "/home/arajabi/anaconda3/lib/python3.5/site-packages/sklearn/utils/class_weight.py", line 62, in compute_class_weight
" got: %r" % class_weight)
ValueError: class_weight must be dict, 'balanced', or None, got: 'auto'
我对python比较陌生。有人可以给我一个简单的解决方案吗?
答案 0 :(得分:1)
您为SVC传递了invalid parameter,'auto'
。该错误消息告诉您可能的值是:类的平衡,平衡或无。如果希望它使用默认行为,请将其保留为空白或输入None
class_weight : {dict, ‘balanced’}, optional
通常,当我对SVC进行网格搜索时,除非我有想要尝试的特定类平衡,否则我将使用'class_weight': [None, 'Balanced]
答案 1 :(得分:1)
sklearn.svm.SVC的文档正确here。
参数class_weight
不接受'auto'作为输入值。那是你的错误。
您可以通过以下方法解决此问题:
'class_weight': ['auto']
具有:
'class_weight': ['balanced']
答案 2 :(得分:0)
这不是典型的Python错误,它是GridSearchCV,它不喜欢您在class_weight中的auto
参数:
tuned_parameters = [
{'kernel': ['linear], 'C':[1, 10], 'class_weight': ['auto' <---
我不熟悉,我只能重复错误消息中所说的内容:
class_weight must be dict, 'balanced', or None, got: 'auto'
有关更多信息,您必须查看http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV。