【发布时间】:2013-07-30 09:36:02
【问题描述】:
例如,我们有 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()
有人能用文字解释一下这个绘图是如何工作的吗?
【问题讨论】:
-
pl.scatter是否在绘制您要询问的线?在我看来,很多重要的代码都丢失了,我们需要理解这个问题。也许是因为我对SVM一无所知。我想你问的那行可能只是包含在 X[:,1] 中,但除非你说你是如何初始化 X 的,否则我们无法真正知道。 -
matty T pain, pl.scatter(...) 绘制数据示例但不绘制决策边界。它是 pylab 的库函数。
-
你弄明白了吗?如果您发布该人物的图片并告诉我们您了解哪些部分以及您不了解哪些部分,将会有所帮助。
标签: python numpy machine-learning svm svc