为什么 sklearn.svm.SVC 的属性 coef_ 的形状 = [n_class * (n_class-1) / 2, n_features]?

why sklearn.svm.SVC's attribute coef_ has shape = [n_class * (n_class-1) / 2, n_features]?

我正在使用 sklearn.svm.SVC 和线性内核,我想获得特征的重要性,所以我使用属性 coeff_ ,它(如此处解释:https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html)是一个具有以下形状的数组[n_class * (n_class-1) / 2, n_features],例如,在我的例子中,我有 10 个 classes 和 54 个特征,所以形状是[45,54],

为什么我得到 45 个权重数组?这些数组中的每一个的含义是什么?因为凭直觉我会期望 10 个权重数组,每个 class

形状确实[n_class * (n_class-1) / 2, n_features]但是为什么呢?

这是因为如果你有 超过 2 个 类 即如果你遇到的问题 不是 binary 那么函数的multiclass支持按照一对一的方案处理。


示例: 如果您有 3 个 类,假设 1,2,3,那么分类器将适用于以下情况:1vs21vs32vs3。所以在这里,我们有 n_class * (n_class-1) / 2 = 3 * (3-1) / 2 = 3.


让我们验证一下上面的内容:

import numpy as np
from sklearn.svm import SVC

X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
y = np.array([1, 1, 2, 3]) # 3 classes

clf = SVC(kernel='linear')
clf.fit(X, y)

print(clf.coef_)
[[-0.5        -0.5       ]
 [-0.46153846 -0.30769231]
 [-1.          0.        ]]

这里,在clf.coef_中,每一行分别对应上述情况1vs21vs32vs3。因此,第一行即 [-0.5, -0.5] 给出了 1vs2 分类拟合情况下第一和第二 feature/variable 的系数。


P.S:在二进制分类的情况下,print(clf.coef_)将return仅用于1vs2分类情况的一行。


关于行的顺序:

In the case of “one-vs-one” SVC, the layout of the attributes is a little more involved. In the case of having a linear kernel, the attributes coef_ and intercept_ have the shape [n_class * (n_class - 1) / 2, n_features] and [n_class * (n_class - 1) / 2] respectively. This is similar to the layout for LinearSVC described above, with each row now corresponding to a binary classifier. The order for classes 0 to n is “0 vs 1”, “0 vs 2” , … “0 vs n”, “1 vs 2”, “1 vs 3”, “1 vs n”, . . . “n-1 vs n”.

https://scikit-learn.org/stable/modules/svm.html

中找到的段落