在 scikit learn svm 分类器中,class_weight = None 和 class_weight = Auto 之间有什么区别?
从文档中它给出为
将类 i 的参数 C 设置为 SVC 的 class_weight[i]*C。如果没有给出,则所有类别的权重都应该为一。 “自动”模式使用 y 的值来自动调整与类别频率成反比的权重。
class sklearn.svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, shrinking=True, probability=False, tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, random_state=None)
但使用自动模式有什么好处呢?我无法理解它的实现。
这发生在class_weight.py 文件:
elif class_weight == 'auto':
# Find the weight of each class as present in y.
le = LabelEncoder()
y_ind = le.fit_transform(y)
if not all(np.in1d(classes, le.classes_)):
raise ValueError("classes should have valid labels that are in y")
# inversely proportional to the number of samples in the class
recip_freq = 1. / bincount(y_ind)
weight = recip_freq[le.transform(classes)] / np.mean(recip_freq)
这意味着您的每个班级(在classes
) 得到的权重等于1
除以该类别在数据中出现的次数(y
),因此出现频率较高的类的权重会较低。然后将其进一步除以所有逆类别频率的平均值。
优点是您不再需要担心自己设置类权重:这对于大多数应用程序来说应该已经很好了。
如果你查看上面的源代码,对于None
, weight
充满了 ,因此每个类别的权重相等。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)