Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVC with class_weight in scikit-learn

I would like to use class_weight to create a weighted SVC classifier in sikit-learn. Nevertheless, I'm not sure if I'm configuring correctly my model. Please consider the example below:

x = np.array([[0,0,1],[0,1,1],[1,0,0]])
y = np.array([1,1,0])

cw = {}
for l in set(y):
    cw[l] = np.sum(y == l)
print(cw)

m = SVC(probability = True, max_iter = 1000, class_weight = cw)
m = m.fit(x,y)

I obtained the model:

SVC(C=1.0, cache_size=200, class_weight={0: 1, 1: 2}, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=1000, probability=True, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

With class_weight={0: 1, 1: 2} corresponding to the number of data points in each class.

QUESTION: Is it correct to proceed in this way?

like image 211
B.Gees Avatar asked Sep 13 '25 22:09

B.Gees


1 Answers

As you have a 2:1 ratio of class labels, this weighting appears to be correct.

One other thing you can do if you don't want to manually calculate the class weights is to pass class_weight='balanced' and let the SVC balance the weights for you

like image 161
G. Anderson Avatar answered Sep 16 '25 13:09

G. Anderson