我可以用random.gauss(mu, sigma)
函数生成高斯数据,但如何生成2D高斯呢?有这样的函数吗?
发布于 2011-10-07 21:07:30
如果你可以使用numpy
,那就是numpy.random.multivariate_normal(mean, cov[, size])
。
例如,要获取10,000个2D样本:
np.random.multivariate_normal(mean, cov, 10000)
在那里mean.shape==(2,)
和cov.shape==(2,2)
。
发布于 2013-01-26 00:17:54
我想添加一个使用指数函数的近似值。这直接生成包含可移动的、对称的2d高斯的2d矩阵。
我应该注意到,我在scipy邮件列表归档中找到了这段代码,并对其进行了一些修改。
import numpy as np
def makeGaussian(size, fwhm = 3, center=None):
""" Make a square gaussian kernel.
size is the length of a side of the square
fwhm is full-width-half-maximum, which
can be thought of as an effective radius.
"""
x = np.arange(0, size, 1, float)
y = x[:,np.newaxis]
if center is None:
x0 = y0 = size // 2
else:
x0 = center[0]
y0 = center[1]
return np.exp(-4*np.log(2) * ((x-x0)**2 + (y-y0)**2) / fwhm**2)
为了便于参考和增强,它被托管为gist here。欢迎拉取请求!
发布于 2019-07-07 22:48:22
import numpy as np
# define normalized 2D gaussian
def gaus2d(x=0, y=0, mx=0, my=0, sx=1, sy=1):
return 1. / (2. * np.pi * sx * sy) * np.exp(-((x - mx)**2. / (2. * sx**2.) + (y - my)**2. / (2. * sy**2.)))
x = np.linspace(-5, 5)
y = np.linspace(-5, 5)
x, y = np.meshgrid(x, y) # get 2D variables instead of 1D
z = gaus2d(x, y)
2D Gaussian function的简单实现和示例。这里sx和sy是x和y方向的分布,mx和my是中心坐标。
https://stackoverflow.com/questions/7687679
复制相似问题