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)