gpt4 book ai didi

python - 为什么在 sklearn GridsearchCV SVM 中使用 class_weight 时会出现错误?

转载 作者:行者123 更新时间:2023-12-01 22:04:26 25 4
gpt4 key购买 nike

下面是我的代码:

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 比较陌生。有人可以给我一个简单的解决方案吗?

最佳答案

sklearn.svm.SVC的文档是对的here .参数 class_weight 不接受“auto”作为输入值。那是你的错误。

你可以通过替换来解决这个问题:

'class_weight': ['auto']

与:

'class_weight': ['balanced']

关于python - 为什么在 sklearn GridsearchCV SVM 中使用 class_weight 时会出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52629767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com