Generating 3D Gaussian distribution in Python

前端 未结 2 1325
暗喜
暗喜 2021-02-15 18:18

I want to generate a Gaussian distribution in Python with the x and y dimensions denoting position and the z dimension denoting the magnitude of a certain quantity.

The

2条回答
  •  梦谈多话
    2021-02-15 19:00

    As of scipy 0.14, you can use scipy.stats.multivariate_normal.pdf()

    import numpy as np
    from scipy.stats import multivariate_normal
    
    x, y = np.mgrid[-1.0:1.0:30j, -1.0:1.0:30j]
    # Need an (N, 2) array of (x, y) pairs.
    xy = np.column_stack([x.flat, y.flat])
    
    mu = np.array([0.0, 0.0])
    
    sigma = np.array([.025, .025])
    covariance = np.diag(sigma**2)
    
    z = multivariate_normal.pdf(xy, mean=mu, cov=covariance)
    
    # Reshape back to a (30, 30) grid.
    z = z.reshape(x.shape)
    

提交回复
热议问题