【发布时间】:2021-03-07 14:46:01
【问题描述】:
为了了解 SVM-OVR (One-Vs-Rest) 的工作原理,我正在测试以下代码:
import matplotlib.pyplot as plt
import numpy as np
from sklearn.svm import SVC
x = np.array([[1,1.1],[1,2],[2,1]])
y = np.array([0,100,250])
classifier = SVC(kernel='linear', decision_function_shape='ovr')
classifier.fit(x,y)
print(classifier.predict([[1,2]]))
print(classifier.decision_function([[1,2]]))
输出是:
[100]
[[ 1.05322128 2.1947332 -0.20488118]]
这意味着样本[1,2]在100类中被正确预测(这很明显,因为[1,2]也用于训练)。
但是,让我们来看看决策函数。 SVM-OVA 应该生成三个分类器,即三行。第一个将class1 与class2 U class3 分开,第二个将class2 与class1 U class3 分开,第三个将class3 与class1 U class2 分开。我最初的目标是准确理解决策函数值的含义。我知道正值意味着样本在平面的右侧,反之亦然;值越大,样本与超平面之间的距离(本例中为直线)越大,样本属于该类的置信度越大。
但是,由于两个决策函数值是正的,因此显然有些错误,而假设只有正确的类应该报告正的决策函数(因为预测值也是训练样本)。为此,我尝试绘制分隔线。
fig, ax = plt.subplots()
ax.scatter(x[:, 0], x[:, 1], c=y, cmap=plt.cm.winter, s=25)
# create a mesh to plot in
x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1
xx2, yy2 = np.meshgrid(np.arange(x_min, x_max, .2),np.arange(y_min, y_max, .2))
Z = classifier.predict(np.c_[xx2.ravel(), yy2.ravel()])
Z = Z.reshape(xx2.shape)
ax.contourf(xx2, yy2, Z, cmap=plt.cm.winter, alpha=0.3)
w = classifier.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (classifier.intercept_[0]) / w[1]
ax.plot(xx,yy)
w = classifier.coef_[1]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (classifier.intercept_[1]) / w[1]
ax.plot(xx,yy)
w = classifier.coef_[2]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (classifier.intercept_[2]) / w[1]
ax.plot(xx,yy)
ax.axis([x_min, x_max,y_min, y_max])
plt.show()
这是我得到的:
惊喜:确实,当计算 OVO(One-Vs-One)策略时,这些分隔线代表超平面:确实,您可以注意到这些线将 class1 与 class2 分开,class2 与 @987654338 分开@ 和 class1 来自 class3。
我也尝试添加一个类:
import matplotlib.pyplot as plt
import numpy as np
from sklearn.svm import SVC
x = np.array([[1,1.1],[1,2],[2,1],[3,3]])
y = np.array([0,100,250, 500])
classifier = SVC(kernel='linear', decision_function_shape='ovr')
classifier.fit(x,y)
然后发生的是表示决策函数的向量的长度等于 4(根据 OVA 策略),但又生成了 6 行(好像我已经实现了 OVO 策略)。
classifier.decision_function([[1,2]])
[[ 2.14182753 3.23543808 0.83375105 -0.22753309]]
classifier.coef_
array([[ 0. , -0.9 ],
[-1. , 0.1 ],
[-0.52562421, -0.49934299],
[-1. , 1. ],
[-0.8 , -0.4 ],
[-0.4 , -0.8 ]])
我的最后一个问题:决策函数值代表什么?为什么即使应用 OVA 策略,也会生成n(n-1)/2 超平面,而不是n 超平面?
【问题讨论】:
标签: python machine-learning scikit-learn svm multiclass-classification