绘制决策边界 matplotlib [英] plot decision boundary matplotlib

查看:36
本文介绍了绘制决策边界 matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am very new to matplotlib and am working on simple projects to get acquainted with it. I was wondering how I might plot the decision boundary which is the weight vector of the form [w1,w2], which basically separates the two classes lets say C1 and C2, using matplotlib.

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

Is it as simple as plotting a line from (0,0) to the point (w1,w2) (since W is the weight "vector") if so, how do I extend this like in both directions if I need to?

现在我所做的就是:

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

提前致谢.

推荐答案

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

Decision boundary is generally much more complex then just a line, and so (in 2d dimensional case) it is better to use the code for generic case, which will also work well with linear classifiers. The simplest idea is to plot contour plot of the decision function

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

这篇关于绘制决策边界 matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆