情节决策边界matplotlib

ano*_*428 3 python vector machine-learning matplotlib perceptron

我是matplotlib的新手,正在研究简单的项目以熟悉它.我想知道如何绘制决策边界,它是形式[w1,w2]的权重向量,它基本上将两个类分开,比如说C1和C2,使用matplotlib.

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

现在我所做的只是:

 import matplotlib.pyplot as plt
   plt.plot([0,w1],[0,w2])
   plt.show()
Run Code Online (Sandbox Code Playgroud)

提前致谢.

lej*_*lot 16

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

# 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)
Run Code Online (Sandbox Code Playgroud)

sklearn文档中的一些示例

在此输入图像描述