我想写一个函数,返回一个np.array
大小nx
xny
,其中包含一个中心高斯分布,平均值为mu
和sdsig
。下面的代码在某些情况下有效,但在许多情况下不-有什么错或我应该写什么来获得我需要的?你知道吗
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
您的问题是,在创建数据集时,您创建的值已经具有平均值和标准偏差。它们都在nx,ny参数所暗示的相互距离处。然后应用高斯分布并提供与数据集的均值和标准差不同的均值和标准差时,它将显示不在中心的数据集的实际均值,正如您在不查看数据的情况下指定的那样。你知道吗
举个例子:
你告诉它平均值是1,但分布的中心是0。你知道吗
输出:
你的提议与中庸之道不符。在1D的情况下,说它在中心就是说它的平均值是
0
。对于二维高斯分布,可以说有两种平均值,定义为x
和y
的期望值。再说一遍,说它是中心的,就是说它们都是0
。你知道吗总而言之,你的密度不是一个中心二维高斯的密度,应该是
如果高斯分布在
(xm, ym)
中心,则密度为但是没有一个中心高斯平均数
mu
。你知道吗相关问题 更多 >
编程相关推荐