python - 绘制决策边界 matplotlib

标签 python vector matplotlib machine-learning perceptron

我是 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 文档中的一些例子

enter image description here

关于python - 绘制决策边界 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19054923/

相关文章:

python - pip3 列表出现 AssertionError

multidimensional-array - 如何在 Dyalog APL 中将三元组向量转换为 3xnx3 矩阵?

c++ - vector push_back 错误

python - 通过 keras 正确 reshape MNIST 图像

python - 使用 Matplotlib 进行 3D 绘图时 cmath 的问题

python - 用 Python 制作彩色 Ascii 文本

c++ - 如何将 "full screen"按钮添加到最右侧的标题栏?

python - 高复制数据存储中的重复条目

java - 我们可以将包含父类(super class)对象的 vector 转换为包含子类对象的 vector 吗?

python - Matplotlib: Nose , Tornado