二维高斯函数不能产生正确的结果

2024-05-01

我想写一个返回一个的函数np.array尺寸的nx x ny包含具有均值的中心高斯分布mu and sd sig。下面的代码在某些情况下有效,但在许多情况下无效 - 有什么问题或者我还应该写什么来获得我需要的东西?

import matplotlib.pyplot as plt
import numpy as np

def create2dGaussian(mu, sigma, nx, ny):
    x, y = np.meshgrid(np.linspace(-nx / 2.0, +nx / 2.0, nx), np.linspace(-ny / 2.0, +ny / 2.0, ny))
    d = np.sqrt(x * x + y * y)
    g = np.exp(-((d - mu) ** 2 / (2.0 * sigma ** 2)))

    # just for debugging:
    np.set_printoptions(precision=1, suppress=True)
    print(g.shape)
    print(g)
    plt.imshow(g, cmap='jet', interpolation='nearest')
    plt.colorbar()
    plt.show()

    return g

以下是一些带有注释的测试用例:

from create2dGaussian import create2dGaussian

create2dGaussian(1, 10, 25, 25) # seems to work
create2dGaussian(1, 5, 25, 25) # the middle is not quite the peak anymore
create2dGaussian(1, 1, 25, 25) # the above problem more clearly visible
create2dGaussian(1, 1, 5, 5) # here it is extrem as the middle is now only 0.6

create2dGaussian(5, 10, 25, 25) # mean is still 1 and not 5

您的建议中存在与平均值的混淆。在一维情况下,说它居中就是说它的平均值是0。对于二维高斯,可以说有两种均值,定义为期望x and of y。再次说它是居中的,就是说它们都是0.

总而言之,您的密度不是中心二维高斯的密度,它应该是

exp(-((x**2 +y**2) / (2.0 * sigma ** 2)))

如果高斯分布的中心为(xm, ym)那么密度是

exp(-(((x-xm)**2 +(y-ym)**2) / (2.0 * sigma ** 2)))

但不存在具有均值的中心高斯分布mu.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

二维高斯函数不能产生正确的结果 的相关文章

随机推荐