python - SVC MultiClass Classification OVO 决策函数解释

标签 python scikit-learn

我试图理解如何使用一对一方法在多类分类场景中解释决策函数值。我创建了一个 2D 样本数据,每个样本包含 4 个类别的 100 个样本,存储在 X 和 y 变量中。这是代码:

# 4 Classes - Make 4 separate datasets
d1, o1 = make_blobs(n_samples = 100, n_features = 2, centers = 1, random_state=0, cluster_std = 0.5)
d2, o2 = make_blobs(n_samples = 100, n_features = 2, centers = 1, cluster_std = 0.5)
d3, o3 = make_blobs(n_samples = 100, n_features = 2, centers = 1, cluster_std = 0.5)
d4, o4 = make_blobs(n_samples = 100, n_features = 2, centers = 1, cluster_std = 0.5)
X = np.vstack((d1,d2,d3,d4))
y = np.hstack((np.repeat(0,100), np.repeat(1,100), np.repeat(2,100), np.repeat(3,100))).T
print('0 - Red, 1 - Green, 2 - Blue, 3 - Yellow')
cols = np.hstack((np.repeat('r',100), np.repeat('g',100), np.repeat('b',100), np.repeat('y',100))).T
svm_ovr = SVC(kernel='linear', gamma='auto', decision_function_shape='ovr')
svm_ovr.fit(X, y)

svm_ovo = SVC(kernel='linear', gamma='auto', decision_function_shape='ovo')
svm_ovo.fit(X, y)

print('OVR Configuration Costs for 4 Class Classification Data:')
print('Cost: ' + str(svm_ovr.decision_function([[2,2]])))
print('Prediction: ' + str(svm_ovr.predict([[2,2]])))
print('No. Support Vectors: ' + str(svm_ovr.n_support_))
print('OVO Configuration Costs for 4 Class Classification Data:')
print('Cost: ' + str(svm_ovo.decision_function([[2,2]])))
print('Prediction: ' + str(svm_ovo.predict([[2,2]])))
print('No. Support Vectors: ' + str(svm_ovo.n_support_))

代码片段的输出是:

OVR Configuration Costs for 4 Class Classification Data:
Cost: [[ 3.23387565  0.77664387 -0.17878109  2.15179802]]
Prediction: [0]
No. Support Vectors: [2 4 1 3]
OVO Configuration Costs for 4 Class Classification Data:
Cost: [[ 0.68740472  0.77724567  0.88685872  0.14910583 -1.49263233 -0.23041644]]
Prediction: [0]

我猜测在 OVR 场景中,0 vs Rest 模型的最高成本值为 3.23。这表明预测应该为 0。 您能否解释一下,SVC 如何根据 OVO 案例中 6 个模型的成本值将测试点的类别预测为 0。

最佳答案

OVO 模型中,您为每个可能的类对构建一个二元分类器,从而构建 nC2 模型,其中 n 是在您的情况下,类总数为 4(因此您构建 6 模型)。

OVO 中,模型是按照字典顺序构建的,即

Model 1 = Class 1 Vs Class 2
Model 2 = Class 1 Vs Class 3
Model 3 = Class 1 Vs Class 4
Model 4 = Class 2 Vs Class 3
Model 5 = Class 2 Vs Class 4
Model 6 = Class 3 Vs Class 4

因此决策函数是一个 6 维数组,每个元素对应于该点到该模型的分离超平面的距离,负值表示该点在另一个上超平面的边:

Cost: [[ 0.68740472  0.77724567  0.88685872  0.14910583 -1.49263233 -0.23041644]]

因此,通过决策函数数组,您可以根据值对每个模型进行预测。所以你的预测是这样的:

Model 1 = Class 1
Model 2 = Class 1
Model 3 = Class 1
Model 4 = Class 2
Model 5 = Class 4
Model 6 = Class 4

现在,您只需获得模型的多数票,并将其作为预测,在本例中结果为 1 类。

关于python - SVC MultiClass Classification OVO 决策函数解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59497115/

相关文章:

python - 使用 numpy.genfromtxt 读取包含逗号的字符串的 csv 文件

python - 在 scikit-learn 中为新数据保存特征向量

python - 如何进行多标签分层抽样?

python - 如何使用 python 删除 ml.transcription 文件中句子开头和结尾的标签并将其保存在 newml.transcription 文件中?

python - 为什么 cross_validate 返回 NaN 分数?

scikit-learn - 将目标/标签数据传递给 Scikit-learn GridSearchCV 的 OneClassSVM 拟合方法

python - Sklearn - 发现输入变量的样本数量不一致 : [16512, 4128]

python - 在 Django Rest Framework 中列出具有动态字段的序列化程序

python - 将 unicode 列表转换为包含 python 字符串的列表的简单方法?

python - python中变量赋值的问题