Cannot understand plotting of decision boundary in SVM and LR

懵懂的女人 提交于 2019-12-01 13:32:34
lejlot

Basically, you are plotting the function f : R^2 -> {0,1} so it is a function from the 2 dimensional space into the degenerated space of only two values - 0 and 1.

First, you generate the mesh you want to visualize your function on. In case of your example with f(x)=y you would select some interval [x_min,x_max] on which you would take points with some distance eps and plot the corresponding values of 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))

Next, we calculate the function values, in our case it is a SVM.predict function, which results in either 0 or 1

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

It is the same as in your example of calculating f(x) for all the analyzed x

Now, the "tricky" part which may lead to missunderstanding is

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

This function plots the contours of your f function. To visualize 3 dimensional function on the plane one often creates contour plots, it is like a map of height of your function. You draw a line between points if the large change in the value of f is detected around them.

Nice example from the mathworld

Shows an example of such plot.

In the case of SVM we have only two possible values - 0 and 1, so as the result, the contour lines are located exactly in these parts of your 2d space, where on one side we have f(x)=0 and on the other f(x)=1. So even though it seems like a "2d plot" it is not - this shape, that you can observe (the decision boundary) is a visualization of the biggest differences in the 3d function.

In sklearn documentation that visualize it for the multi-classification example, when we have f : R^2 -> {0,1,2}, so the idea is exactly the same, but contour is plotted between such adjacent x1 and x2 that f(x1)!=f(x2).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!