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

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

问题描述

我对matplotlib还是很陌生,并且正在从事一些简单的项目以熟悉它.我想知道如何绘制决策边界,该边界是形式为[w1,w2]的权重向量,它使用matplotlib基本上将两个类(例如C1和C2)分开.

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天全站免登陆