为了理解SVM-OVR(一对一)的工作原理,我测试了以下代码:
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(一对一)策略时,这些分隔线表示超平面:事实上,您可以注意到这些线将class1
从class2
、class2
从class3
和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
超平面
关键是,默认情况下,SVM确实实现了OvO策略(参见here以供参考)
同时,默认情况下(即使在您的示例中已明确表示)
decision_function_shape
设置为'ovr'
实现OvO策略的原因是SVM算法不能很好地适应训练集的大小(并且使用OvO策略,每个分类器只在训练集中与其必须区分的类相对应的部分进行训练)。 原则上,您可以通过
OneVsRestClassifier
的实例强制SVM分类器实现OvA策略,例如:相关问题 更多 >
编程相关推荐