绘制决策边界的matplotlib

2024-03-29 13:08:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我对matplotlib很陌生,正在做一些简单的项目来熟悉它。我想知道如何使用matplotlib绘制决策边界,它是形式[w1,w2]的权重向量,基本上分隔了两个类,比如C1和C2。

它是否像绘制一条从(0,0)到点(w1,w2)的直线一样简单(因为W是权重“向量”),如果是,如果需要,我如何在两个方向上扩展它?

现在我所做的就是:

 import matplotlib.pyplot as plt
   plt.plot([0,w1],[0,w2])
   plt.show()

提前谢谢。


Tags: 项目matplotlib绘制plt向量形式直线w1
1条回答
网友
1楼 · 发布于 2024-03-29 13:08:35

决策边界通常比直线复杂得多,因此(在二维情况下)最好使用通用情况下的代码,这也适用于线性分类器。最简单的方法是绘制决策函数的等高线图

# 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文档的一些示例

enter image description here

相关问题 更多 >