用Matplotlib绘制法向密度判别函数

2024-09-28 05:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画出一些随机数据的正常密度的一般判别函数。我不知道我将如何通过matplotlib来实现它,我希望任何人都能帮我一点忙。在

公式如下:

enter image description hereenter image description hereenter image description here

我已经写了代码,并把它放在IPython笔记本上,希望对我有帮助!在

View iPython notebook

如果你想下载的话,笔记本也在Github上(如果有帮助,我也可以制作一个.py脚本)。 Link to IPython notebook file on Github

谢谢你!在


Tags: 数据代码pygithub脚本viewmatplotlibipython
1条回答
网友
1楼 · 发布于 2024-09-28 05:23:47

代码如下:

import pylab as pl
import numpy as np

D = 2

M1 = np.array([0.0, 0.0])
M2 = np.array([1.0, 1.0])

C1 = np.array([[2.0, 0.4], [0.4, 1.0]])
C2 = np.array([[1.0, 0.6], [0.6, 2.0]])

X, Y = np.mgrid[-2:2:100j, -2:2:100j]
points = np.c_[X.ravel(), Y.ravel()]

invC = np.linalg.inv(C1)
v = points - M1
g1 = -0.5*np.sum(np.dot(v, invC) * v, axis=1) - D*0.5*np.log(2*np.pi) - 0.5*np.log(np.linalg.det(C1))
g1.shape = 100, 100

invC = np.linalg.inv(C2)
v = points - M2
g2 = -0.5*np.sum(np.dot(v, invC) * v, axis=1) - D*0.5*np.log(2*np.pi) - 0.5*np.log(np.linalg.det(C2))
g2.shape = 100, 100

fig, axes = pl.subplots(1, 3, figsize=(15, 5))
ax1, ax2, ax3 = axes.ravel()
for ax in axes.ravel():
    ax.set_aspect("equal")

ax1.pcolormesh(X, Y, g1)
ax2.pcolormesh(X, Y, g2)
ax3.pcolormesh(X, Y, g1 > g2)

输出:

enter image description here

然后使用随机数进行模拟:

^{pr2}$

输出:

{2美元^

相关问题 更多 >

    热门问题