Arsalan
Arsalan

Reputation: 373

Why do I get error when using class_weight in sklearn GridsearchCV SVM?

Below is my code:

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)

But I get the following error:

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'

I am relatively new to python. Can someone give me a straightforward solution for this problem?

Upvotes: 2

Views: 3656

Answers (3)

Dominik Kolesar
Dominik Kolesar

Reputation: 120

The documentation of sklearn.svm.SVC is right here. Parameter class_weight doesn't accept 'auto' as an input value. That is your error.

You can solve this by replacing:

'class_weight': ['auto']

with:

'class_weight': ['balanced']

Upvotes: 6

return42
return42

Reputation: 561

This is not a typical Python error, it is GridSearchCV which does not like your auto argument in class_weight:

tuned_parameters = [
{'kernel': ['linear], 'C':[1, 10], 'class_weight': ['auto' <---

I'am not familiar with, I can only repeat what the error message says:

class_weight must be dict, 'balanced', or None, got: 'auto'

For more infos, you have to look at http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.

Upvotes: 0

G. Anderson
G. Anderson

Reputation: 5955

You passed in an invalid parameter, 'auto' for the SVC. The error message tells you what the possible values are: a dict of your classes, balanced, or None. If you want it to use the default behavior, leave it blank or enter None

class_weight : {dict, ‘balanced’}, optional

Generally when I gridsearch a SVC, I use 'class_weight': [None, 'Balanced] unless I have a specific class balance I want to try

Upvotes: 1

Related Questions