我有一个多标签数据(有些类有2个标签和10个标签),我的模型对均衡和无值过度拟合。为class_weight参数设置的最佳值是什么?
from sklearn.svm import LinearSVC
svm = LinearSVC(C=0.01,max_iter=100,dual=False,class_weight=None,verbose=1)
该class_weight
参数的控制实际上是C
通过以下方式参数:
class_weight:{dict,'balanced'},可选
将
class_weight[i]*C
SVC的类i的参数C设置为。如果未给出,则所有类均应具有权重一。“平衡”模式使用y值自动调整与输入数据中的类频率成反比的权重,如下所示:n_samples / (n_classes * np.bincount(y))
尝试class_weight
在保持C
相同的同时进行游戏,例如C = 0.1
编辑
这是class_weight
为您的171个班级创建的一种好方法。
# 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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句