在svm scikit中,class weight = none和auto之间的区别是什么?

dan*_*ous 4 python machine-learning scikit-learn

在scikit中学习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)
Run Code Online (Sandbox Code Playgroud)

但是使用自动模式有什么好处.我无法理解它的实现.

IVl*_*lad 8

这发生在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)
Run Code Online (Sandbox Code Playgroud)

这意味着你拥有的每个类(in classes)获得的权重等于1该类出现在data(y)中的次数,因此更频繁出现的类将获得较低的权重.然后将其进一步除以所有逆类频率的平均值.

优点是您不再需要担心自己设置类权重:这应该已经适用于大多数应用程序.

如果你在源代码中查看上面的内容None,weight则填充了一些内容,因此每个类都有相同的权重.

  • 所以特别不同的是,"none"不会对类进行加权,而"auto"会根据类的分布来计算权重. (3认同)

小智 5

这是一篇很老的帖子,但对于所有刚刚遇到这个问题的人,请注意 class_weight == 'auto' 从 0.17 版开始已被弃用。使用 class_weight == 'balanced' 代替。

http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html

这是按如下方式实现的:

n_samples / (n_classes * np.bincount(y))

干杯!