如何用Python生成2D高斯?

24 python gaussian

我可以用random.gauss(mu, sigma)函数生成高斯数据,但是如何生成2D高斯?有没有这样的功能?

NPE*_*NPE 46

如果可以使用numpy,就有numpy.random.multivariate_normal(mean, cov[, size]).

例如,要获得10,000个2D样本:

np.random.multivariate_normal(mean, cov, 10000)
Run Code Online (Sandbox Code Playgroud)

在哪里mean.shape==(2,)cov.shape==(2,2).


ken*_*ytm 13

由于标准的二维高斯分布只是两个一维高斯分布的乘积,如果两个轴之间没有相关性(即协变矩阵是对角线),则只需调用random.gauss两次.

def gauss_2d(mu, sigma):
    x = random.gauss(mu, sigma)
    y = random.gauss(mu, sigma)
    return (x, y)
Run Code Online (Sandbox Code Playgroud)

  • 说两次打电话不是一个充分的答案.你将有2个1D阵列.完整的答案将解释如何将两个1D阵列组合成2D阵列. (4认同)
  • @Octopus:对 2D 高斯进行采样会得到一个 2 元组数组,即 2×N 矩阵,而不是 2D 数组(N×N 矩阵)。我不明白它有什么不足。 (2认同)
  • @shakram02 从字面上调用“random.gauss”两次。请参阅更新。 (2认同)

gie*_*sel 13

我想用指数函数添加近似值.这直接生成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)
Run Code Online (Sandbox Code Playgroud)

作为参考和增强功能,它在此处作为要点进行托管.拉请求欢迎!


小智 8

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)
Run Code Online (Sandbox Code Playgroud)

二维高斯函数的直接实现和示例。这里 sx 和 sy 是 x 和 y 方向的传播,mx 和 my 是中心坐标。


Joh*_*nes 5

Numpy 有一个功能可以做到这一点。它记录在此处。除了上面提出的方法之外,它还允许绘制具有任意协方差的样本。

这是一个小例子,假设ipython -pylab已启动:

samples = multivariate_normal([-0.5, -0.5], [[1, 0],[0, 1]], 1000)
plot(samples[:, 0], samples[:, 1], '.')

samples = multivariate_normal([0.5, 0.5], [[0.1, 0.5],[0.5, 0.6]], 1000)
plot(samples[:, 0], samples[:, 1], '.')
Run Code Online (Sandbox Code Playgroud)


小智 5

如果有人发现这个线程并且正在寻找更通用的东西(就像我所做的那样),我已经修改了@giessel的代码。下面的代码将允许不对称和旋转。

import numpy as np

def makeGaussian2(x_center=0, y_center=0, theta=0, sigma_x = 10, sigma_y=10, x_size=640, y_size=480):
    # x_center and y_center will be the center of the gaussian, theta will be the rotation angle
    # sigma_x and sigma_y will be the stdevs in the x and y axis before rotation
    # x_size and y_size give the size of the frame 

    theta = 2*np.pi*theta/360
    x = np.arange(0,x_size, 1, float)
    y = np.arange(0,y_size, 1, float)
    y = y[:,np.newaxis]
    sx = sigma_x
    sy = sigma_y
    x0 = x_center
    y0 = y_center

    # rotation
    a=np.cos(theta)*x -np.sin(theta)*y
    b=np.sin(theta)*x +np.cos(theta)*y
    a0=np.cos(theta)*x0 -np.sin(theta)*y0
    b0=np.sin(theta)*x0 +np.cos(theta)*y0

    return np.exp(-(((a-a0)**2)/(2*(sx**2)) + ((b-b0)**2) /(2*(sy**2))))
Run Code Online (Sandbox Code Playgroud)