LinearSVC中参数class_weight的最佳值是多少?

时间:2019-11-17 14:46:33

标签: python scikit-learn svm libsvm scikit-multilearn

我有一个多标签数据(某些类有2个标签和10个标签),我的模型对均衡值和无值过度拟合。为class_weight参数设置的最佳值是什么?

from sklearn.svm import LinearSVC
svm = LinearSVC(C=0.01,max_iter=100,dual=False,class_weight=None,verbose=1)

1 个答案:

答案 0 :(得分:0)

class_weight参数实际上通过以下方式控制C参数:

  

class_weight:{dict,“ balanced”},可选

     

设置参数C   将SVC的类i设置为class_weight[i]*C。如果未给出,则所有类都是   应该重一。 “平衡”模式使用y的值   自动调整与班级成反比的权重   输入数据中的频率为n_samples / (n_classes * np.bincount(y))

尝试与class_weight一起玩,同时保持C不变,例如C = 0.1


编辑

这是为您的171个班级创建class_weight的好方法。

# store the weights for each class in a list
weights_per_class = [2,3,4,5,6]

#Let's assume that you have a `y` like this:
y = [121, 122, 123, 124, 125]

您需要:

# create the `class_weight` dictionary
class_weight = {val:weights_per_class[index] for index,val in enumerate (y)}

print(class_weight)
#{121: 2, 122: 3, 123: 4, 124: 5, 125: 6}

# Use it as argument
svm = LinearSVC(class_weight=class_weight)