繁体   English   中英

无法理解SVM和LR中的决策边界图

[英]Cannot understand plotting of decision boundary in SVM and LR

例如,我们有f(x)= x。 如何绘制? 我们取一些x,然后计算y,然后再次执行此操作,然后按点绘制图表。 简单明了。

但是我无法如此清晰地绘制决策边界-当我们没有y绘制时,只有x。

SVM的Python代码:

h = .02  # step size in the mesh
Y = y
# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0  # SVM regularization parameter
svc = svm.SVC(kernel='linear', C=C).fit(X, Y)
rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, Y)
poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, Y)
lin_svc = svm.LinearSVC(C=C).fit(X, Y)

# 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
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))


for i, clf in enumerate((svc, rbf_svc, poly_svc, lin_svc)):
    # Plot the decision boundary. For that, we will asign a color to each
    # point in the mesh [x_min, m_max]x[y_min, y_max].

我所理解的一切都在这里:

    pl.subplot(2, 2, i + 1)
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    pl.contourf(xx, yy, Z, cmap=pl.cm.Paired)
    pl.axis('off')

    # Plot also the training points
    pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)

pl.show()

有人可以用言语解释该绘图如何工作吗?

基本上,您正在绘制函数f : R^2 -> {0,1}所以它是一个从二维空间到只有两个值01的退化空间的函数。

首先,生成要在其上可视化功能的网格。 f(x)=y示例中,您将选择某个间隔[x_min,x_max]在该间隔上将获取具有一定距离eps点并绘制f的对应值

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))

接下来,我们计算函数值,在本例中为SVM.predict函数,结果为01

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

它与您为所有分析的x计算f(x)示例相同

现在,可能导致误解的“棘手”部分是

pl.contourf(xx, yy, Z, cmap=pl.cm.Paired)

此函数绘制f函数的轮廓。 为了可视化平面上的3维功能,通常会创建轮廓图,就像绘制功能高度图一样。 如果在点之间检测到f值的较大变化,则在点之间绘制一条线。

Mathworld中的好例子 样本轮廓图

显示了这样一个情节的例子。

对于SVM,我们只有两个可能的值 01 ,因此结果是等高线恰好位于2d空间的这些部分中,其中一侧我们有f(x)=0而在一侧其他f(x)=1 因此,即使看起来像是“ 2d图”,也并非如此-您可以观察到的这种形状(决策边界)是3d函数中最大差异的可视化。

在为多分类示例可视化的sklearn文档中,当我们有f : R^2 -> {0,1,2} ,想法是完全相同的,但是在这样的相邻x1和x2之间绘制了轮廓f(x1)!=f(x2)

SVM多类

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM