gpt4 book ai didi

python - svm scikit 学习中的 class weight = none 和 auto 之间有什么区别

转载 作者:太空狗 更新时间:2023-10-30 01:51:01 26 4
gpt4 key购买 nike

在 scikit 学习 svm 分类器中,class_weight = None 和 class_weight = Auto 之间有什么区别。

从文档中它被给出为

Set the parameter C of class i to class_weight[i]*C for SVC. If not given, all classes are supposed to have weight one. The ‘auto’ mode uses the values of y to automatically adjust weights inversely proportional to class frequencies.

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 file :

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),因此出现频率更高的类将获得更低的权重。然后将其进一步除以所有逆类频率的平均值。

优点是您不必再担心自己设置类权重:这对大多数应用程序来说应该已经很好了。

如果您查看上面的源代码,对于 Noneweight 填充了 1,因此每个类的权重相等。

关于python - svm scikit 学习中的 class weight = none 和 auto 之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28621518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com