shaare.it

ValueError: 'class_weight' not supported for estimator

ValueError: class_weight not supported

$ python -c "from sklearn.naive_bayes import GaussianNB; GaussianNB(class_weight='balanced')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: class_weight is not supported by GaussianNB

Why this happens

Estimator does not implement class weighting.

Fix

Use sample_weight during fit or choose a supporting estimator.

Wrong code

from sklearn.naive_bayes import GaussianNB
GaussianNB(class_weight='balanced')

Fixed code

from sklearn.naive_bayes import GaussianNB
import numpy as np
X = [[0],[1],[2],[3]]
y = [0,1,1,1]
sample_weight = np.array([1.0, 3.0, 3.0, 3.0])
GaussianNB().fit(X,y, sample_weight=sample_weight)