【发布时间】:2014-06-05 22:14:00
【问题描述】:
我将从以下堆栈溢出问题中借用图像来帮助我描述我的问题: Drawing decision boundary of two multivariate gaussian
我有 2 个具有 2D 点的类,我感兴趣的是决策边界(或判别式)。
我已经编写了返回判别函数结果的函数(一个浮点值),允许我将样本分类为这两种模式。
如果一个样本点是例如,x_i = [x, y]
我可以调用函数
和
如果 g1(x,y) > g2(x,y) 是 1 类,反之亦然
g1(x,y) <= g2(x,y) 2 级
所以决策边界应该在g1(x,y) == g2(x,y)
编辑:
希望一个例子有帮助:
1) 假设我从数据集中抽取 1 个样本 x = [1, 2]
2) 然后我会调用例如g1(1,2) --> 返回0.345g2(1,2) --> 返回0.453
--> 样本 x 属于第 2 类,因为 g2(1,2) > g1(1,2)
3) 现在对于决策边界,我有g2(x,y) == g1(x,y),
或g1(x,y) - g2(x,y) == 0
4) 我生成了一系列x 值,例如1,2,3,4,5,现在我想
查找产生 g1(x,y) - g2(x,y) == 0 的相应 y 值
5) 然后我可以使用这些x,y 对来绘制决策边界
在我上面链接的 StackOverflow 帖子中,建议是
您可以简单地绘制 f(x,y) 的等高线 := pdf1(x,y) > pdf2(x,y)。因此,您将函数 f 定义为 1 iff pdf1(x,y)>pdf2(x,y)。 这样,唯一的轮廓将沿着曲线放置 pdf1(x,y)==pdf2(x,y) 是决策边界(判别式)。如果 你想定义“好”的功能,你可以简单地通过设置 f(x,y) = sgn( pdf1(x,y) - pdf2(x,y) ),并绘制其等高线图 将产生完全相同的判别式。
但是我将如何在 Python 和 matplotlib 中做到这一点,我真的迷失了设置代码来做到这一点。我很感激任何帮助!
编辑:
更多关于函数g() 本身:
def discr_func(x, y, cov_mat, mu_vec):
"""
Calculates the value of the discriminant function for a dx1 dimensional
sample given covariance matrix and mean vector.
Keyword arguments:
x_vec: A dx1 dimensional numpy array representing the sample.
cov_mat: dxd numpy array of the covariance matrix.
mu_vec: dx1 dimensional numpy array of the sample mean.
Returns a float value as result of the discriminant function.
"""
x_vec = np.array([[x],[y]])
W_i = (-1/2) * np.linalg.inv(cov_mat)
assert(W_i.shape[0] > 1 and W_i.shape[1] > 1), 'W_i must be a matrix'
w_i = np.linalg.inv(cov_mat).dot(mu_vec)
assert(w_i.shape[0] > 1 and w_i.shape[1] == 1), 'w_i must be a column vector'
omega_i_p1 = (((-1/2) * (mu_vec).T).dot(np.linalg.inv(cov_mat))).dot(mu_vec)
omega_i_p2 = (-1/2) * np.log(np.linalg.det(cov_mat))
omega_i = omega_i_p1 - omega_i_p2
assert(omega_i.shape == (1, 1)), 'omega_i must be a scalar'
g = ((x_vec.T).dot(W_i)).dot(x_vec) + (w_i.T).dot(x_vec) + omega_i
return float(g)
当我执行它时,它会返回一个浮点数,例如,
discr_func(1, 2, cov_mat=cov_est_1, mu_vec=mu_est_1)-3.726426544537969
如果我没记错的话,应该是这个等式:
非常感谢您对轮廓的建议,但是,我在实现它时遇到了问题:
import pylab as pl
X, Y = np.mgrid[-6:6:100j, -6:6:100j]
x = X.ravel()
y = Y.ravel()
p = (discr_func(x, y, cov_mat=cov_est_1, mu_vec=mu_est_1) -\
discr_func(x, y, cov_mat=cov_est_2, mu_vec=mu_est_2)).reshape(X.shape)
#pl.scatter(X_train[:, 0], X_train[:, 1])
pl.contour(X, Y, p, levels=[0])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-192-28c1c8787237> in <module>()
5 y = Y.ravel()
6
----> 7 p = (discr_func(x, y, cov_mat=cov_est_1, mu_vec=mu_est_1) - discr_func(x, y, cov_mat=cov_est_2, mu_vec=mu_est_2)).reshape(X.shape)
8
9 #pl.scatter(X_train[:, 0], X_train[:, 1])
<ipython-input-184-fd2f8b7fad82> in discr_func(x, y, cov_mat, mu_vec)
25 assert(omega_i.shape == (1, 1)), 'omega_i must be a scalar'
26
---> 27 g = ((x_vec.T).dot(W_i)).dot(x_vec) + (w_i.T).dot(x_vec) + omega_i
28 return float(g)
ValueError: objects are not aligned
我的感觉是.ravel() 列表的传递与我设置此功能的方式效果不佳...有什么建议吗?
【问题讨论】:
-
您的 discr_func 函数无法通过一次调用计算网格上的所有值。所以你需要一个 for 循环来为网格上的每个点调用 discr_func。
标签: python matplotlib