如何使用判别函数绘制 3 个类之间的决策边界 [英] How to plot decision boundaries between 3 classes using discriminant functions

查看:36
本文介绍了如何使用判别函数绘制 3 个类之间的决策边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个判别函数,将2D空间划分为3个区域.我想画出这些区域之间的决策边界.我不知道如何在 python 中使用 matplotlib meshgrid.

I have 3 discriminant functions dividing 2D space into 3 regions. I would like to plot decision boundaries between these regions. I couldn't figure out how to do that using matplotlib meshgrid in python.

在2个判别函数的情况下,过程很简单.我计算函数和等高线图之间的差异,值为0.

In case of 2 discriminant function, the process is simple. I calculate difference between function and contour plot for values of 0.

lin_param = (-5, 5, 100)
xx = np.linspace(*lin_param)
yy = np.linspace(*lin_param)

x, y = np.meshgrid(xx, yy)

z = g1(x, y) - g2(x, y)
cp = plt.contour(x, y, z, levels=[0], colors="k")

plt.scatter(0, 0)
plt.scatter(2, 2)
plt.show()

其中g1和g2是均值(0,0)和(2,2)的多元高斯分布.(分布并不重要,我想将其应用于任何判别函数)

where g1 and g2 are multivariate gaussian distributions with mean (0, 0) and (2, 2). (Distributions are not important, I want to apply this to any discriminant function)

def pdf(x, y, mean, cov):
  var = multivariate_normal(mean=mean, cov=cov)
  pos = np.empty(x.shape + (2,))
  pos[:, :, 0] = x; pos[:, :, 1] = y
  return var.pdf(pos)

def g1(x, y):
  return pdf(x, y, mean=[0,0], cov=[[1,0],[0,1]])

def g2(x, y):
  return pdf(x, y, mean=[2,2], cov=[[1,0],[0,1]])

def g3(x, y):
  return pdf(x, y, mean=[-2,2], cov=[[1,0],[0,1]])

这里一侧为负,另一侧为正.沿决策边界的值都为零.现在,我要添加第三个函数g3,其平均值为(-2,2).绘制结果决策边界并不是一件容易的事.我尝试采用最多3个函数的2个值并将它们的差值指定为z值,但无法实现我想要的.

Here one side is negative and other side is positive. Values are all zero along decision boundary. Now I would to add third function g3 whose mean is at (-2, 2). Plotting resulting decision boundaries is not straightforward. I tried taking maximum 2 values of 3 function and assigning the difference of them as z value but couldn't achieve what I want.

我希望看到类似于下图的内容:

What I would like to see is something similar to image below:

是否可以通过类似的网格网格轮廓图方法来实现?我不想明确计算线.

Is it possible achieve it with similar meshgrid-contour plot approach? I don't want to calculate the line explicitly.

更新

通过使用contourf方法,可以用不同的颜色填充区域.然而,绘制边界线仍然是个问题.

By using contourf method, regions can be filled with different color. However, drawing boundary lines is still problem.

推荐答案

我想我已经达到了期望的输出.

I think I achieved desired output.

我没有将函数之间的差异分配给z数组,而是分配了具有最大值的函数索引.然后我使用类标签(或函数索引)之间的数字作为级别参数.例如,为了在 0 类和 1 类之间绘制边界,我在 levels 参数中添加了 0.5.

Instead of assigning difference between functions to z array, I assigned index of function having largest value. Then I used numbers between class labels (or function index) as levels parameter. For example, to draw boundary between class 0 and class 1, I added 0.5 to levels parameter.

z = np.array((g1(x, y), g2(x, y), g3(x, y)))
z = np.argmax(z, axis=0)
cp = plt.contour(x, y, z, colors="k", levels=[0.5, 1.5, 2.5])

这篇关于如何使用判别函数绘制 3 个类之间的决策边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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