处理平局的支持向量机¶
(译者注:支持向量机依靠决策边界来进行分类,当一个点更靠近某条决策边界,这个点就更可能被分到这个决策边界所代表的那一类。但会存在一些点,位于边际的中间,即到两个决策边界的距离是一致的,此时这个点的状况就会被叫做“平局”)
如果decision_function_shape的值是'ovr',则打破平局的计算代价是高的,因此默认情况下不启用ovr选项。此示例说明了break_ties参数对多类分类问题和Decision_function_shape ='ovr'的影响。
这两个图像的区别仅在于类别被绑在一起的中间区域。如果break_ties = False,则该区域中的所有输入将归为一类,而如果break_ties = True,则平局决胜机制将在该区域中创建非凸决策边界。
输入:
print(__doc__)
# 代码来源: Andreas Mueller, Adrin Jalali
# 执照: BSD 3 clause
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.datasets import make_blobs
X, y = make_blobs(random_state=27)
fig, sub = plt.subplots(2, 1, figsize=(5, 8))
titles = ("break_ties = False",
"break_ties = True")
for break_ties, title, ax in zip((False, True), titles, sub.flatten()):
svm = SVC(kernel="linear", C=1, break_ties=break_ties,
decision_function_shape='ovr').fit(X, y)
xlim = [X[:, 0].min(), X[:, 0].max()]
ylim = [X[:, 1].min(), X[:, 1].max()]
xs = np.linspace(xlim[0], xlim[1], 1000)
ys = np.linspace(ylim[0], ylim[1], 1000)
xx, yy = np.meshgrid(xs, ys)
pred = svm.predict(np.c_[xx.ravel(), yy.ravel()])
colors = [plt.cm.Accent(i) for i in [0, 4, 7]]
points = ax.scatter(X[:, 0], X[:, 1], c=y, cmap="Accent")
classes = [(0, 1), (0, 2), (1, 2)]
line = np.linspace(X[:, 1].min() - 5, X[:, 1].max() + 5)
ax.imshow(-pred.reshape(xx.shape), cmap="Accent", alpha=.2,
extent=(xlim[0], xlim[1], ylim[1], ylim[0]))
for coef, intercept, col in zip(svm.coef_, svm.intercept_, classes):
line2 = -(line * coef[1] + intercept) / coef[0]
ax.plot(line2, line, "-", c=colors[col[0]])
ax.plot(line2, line, "--", c=colors[col[1]])
ax.set_xlim(xlim)
ax.set_ylim(ylim)
ax.set_title(title)
ax.set_aspect("equal")
plt.show()
脚本的总运行时间:(0分钟1.077秒)