我如何使用 matplotlib 绘制决策边界,它是 [w1,w2] 形式的权重向量,它基本上分隔了两个类,比如 C1 和 C2?
是否就像绘制一条从 (0,0) 到点 (w1,w2) 的线一样简单(因为 W 是权重“向量”)如果是这样,如果需要,我如何在两个方向上扩展它?
现在我所做的就是:
import matplotlib.pyplot as plt
plt.plot([0,w1],[0,w2])
plt.show()
决策边界通常比一条线复杂得多,因此(在二维情况下)最好使用通用情况的代码,这也适用于线性分类器。最简单的想法是绘制决策函数的等高线图
# X - some data in 2dimensional np.array
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))
# here "model" is your model's prediction (classification) function
Z = model(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=pl.cm.Paired)
plt.axis('off')
# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)
来自
sklearn
文档的一些示例