我是 matplotlib 的新手,正在从事简单的项目以熟悉它。我想知道如何使用 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
文档中的一些例子
关于python - 绘制决策边界 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19054923/