How to generate 2D gaussian with Python?

后端 未结 6 897
囚心锁ツ
囚心锁ツ 2020-12-24 06:42

I can generate Gaussian data with random.gauss(mu, sigma) function, but how can I generate 2D gaussian? Is there any function like that?

6条回答
  •  失恋的感觉
    2020-12-24 07:10

    Numpy has a function to do this. It is documented here. Additionally to the method proposed above it allows to draw samples with arbitrary covariance.

    Here is a small example, assuming ipython -pylab is started:

    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], '.')
    

提交回复
热议问题