二维高斯函数不产生正确的结果
2d gaussian function does not produce correct results
我想编写一个 returns 大小为 nx
x ny
的 np.array
的函数,它包含一个中心高斯分布,均值为 mu
和 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
您的问题是,在创建数据集时,您创建的值已经具有均值和标准差。它们彼此之间的距离由 nx, ny 参数暗示。然后,当您应用高斯并提供与数据集不同的均值和标准差时,它将显示不在中心的数据集的实际均值,因为您在不查看数据的情况下指定了它。
以这个案例为例:
create2dGaussian(1, 1, 5, 5)
你告诉它的均值为 1,但分布的中心是 0。
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 = mu * np.exp(-((d - mu) ** 2 / (2.0 * sigma ** 2)))
np.set_printoptions(precision=1, suppress=True)
print(("x", x))
print(("y", y))
print(("d", d))
plt.imshow(g, cmap='jet', interpolation='nearest')
plt.colorbar()
plt.show()
return g
#create2dGaussian(1, 10, 25, 25) # seems to work
#create2dGaussian(1, 5, 25, 25) # the middle is not quite the peak anymore
#create2dGaussian(1, 5, 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.0, 10.0, 25.0, 25.0) # mean is still 1 and not 5
输出:
('x', array([
[-2.5, -1.2, 0. , 1.2, 2.5],
[-2.5, -1.2, 0. , 1.2, 2.5],
[-2.5, -1.2, 0. , 1.2, 2.5],
[-2.5, -1.2, 0. , 1.2, 2.5],
[-2.5, -1.2, 0. , 1.2, 2.5]]))
('y', array([
[-2.5, -2.5, -2.5, -2.5, -2.5],
[-1.2, -1.2, -1.2, -1.2, -1.2],
[ 0. , 0. , 0. , 0. , 0. ],
[ 1.2, 1.2, 1.2, 1.2, 1.2],
[ 2.5, 2.5, 2.5, 2.5, 2.5]]))
('d', array([
[3.5, 2.8, 2.5, 2.8, 3.5],
[2.8, 1.8, 1.2, 1.8, 2.8],
[2.5, 1.2, 0. , 1.2, 2.5],
[2.8, 1.8, 1.2, 1.8, 2.8],
[3.5, 2.8, 2.5, 2.8, 3.5]]))
('g', array([
[0. , 0.2, 0.3, 0.2, 0. ],
[0.2, 0.7, 1. , 0.7, 0.2],
[0.3, 1. , 0.6, 1. , 0.3],
[0.2, 0.7, 1. , 0.7, 0.2],
[0. , 0.2, 0.3, 0.2, 0. ]]))
您的建议中存在对均值的混淆。在一维的情况下,说它居中就是说它的平均值是 0
。对于 2D 高斯分布,可以说有两种方法,定义为 x
和 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
。
我想编写一个 returns 大小为 nx
x ny
的 np.array
的函数,它包含一个中心高斯分布,均值为 mu
和 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
您的问题是,在创建数据集时,您创建的值已经具有均值和标准差。它们彼此之间的距离由 nx, ny 参数暗示。然后,当您应用高斯并提供与数据集不同的均值和标准差时,它将显示不在中心的数据集的实际均值,因为您在不查看数据的情况下指定了它。
以这个案例为例:
create2dGaussian(1, 1, 5, 5)
你告诉它的均值为 1,但分布的中心是 0。
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 = mu * np.exp(-((d - mu) ** 2 / (2.0 * sigma ** 2)))
np.set_printoptions(precision=1, suppress=True)
print(("x", x))
print(("y", y))
print(("d", d))
plt.imshow(g, cmap='jet', interpolation='nearest')
plt.colorbar()
plt.show()
return g
#create2dGaussian(1, 10, 25, 25) # seems to work
#create2dGaussian(1, 5, 25, 25) # the middle is not quite the peak anymore
#create2dGaussian(1, 5, 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.0, 10.0, 25.0, 25.0) # mean is still 1 and not 5
输出:
('x', array([
[-2.5, -1.2, 0. , 1.2, 2.5],
[-2.5, -1.2, 0. , 1.2, 2.5],
[-2.5, -1.2, 0. , 1.2, 2.5],
[-2.5, -1.2, 0. , 1.2, 2.5],
[-2.5, -1.2, 0. , 1.2, 2.5]]))
('y', array([
[-2.5, -2.5, -2.5, -2.5, -2.5],
[-1.2, -1.2, -1.2, -1.2, -1.2],
[ 0. , 0. , 0. , 0. , 0. ],
[ 1.2, 1.2, 1.2, 1.2, 1.2],
[ 2.5, 2.5, 2.5, 2.5, 2.5]]))
('d', array([
[3.5, 2.8, 2.5, 2.8, 3.5],
[2.8, 1.8, 1.2, 1.8, 2.8],
[2.5, 1.2, 0. , 1.2, 2.5],
[2.8, 1.8, 1.2, 1.8, 2.8],
[3.5, 2.8, 2.5, 2.8, 3.5]]))
('g', array([
[0. , 0.2, 0.3, 0.2, 0. ],
[0.2, 0.7, 1. , 0.7, 0.2],
[0.3, 1. , 0.6, 1. , 0.3],
[0.2, 0.7, 1. , 0.7, 0.2],
[0. , 0.2, 0.3, 0.2, 0. ]]))
您的建议中存在对均值的混淆。在一维的情况下,说它居中就是说它的平均值是 0
。对于 2D 高斯分布,可以说有两种方法,定义为 x
和 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
。