在numpy中生成随机数的函数和matlab有很多的相似性,可以以矩阵为基本单位来生成各种不同的随机数,下面是通过random函数中的normal函数来生成服从正态分布的随机数
作者:采石工
链接:https://www.zhihu.com/question/39823283/answer/115241445
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
import numpy as np
from numpy.linalg import cholesky
import matplotlib.pyplot as plt
sampleNo = 1000;
# 一维正态分布
# 下面三种方式是等效的
mu = 3
sigma = 0.1
np.random.seed(0)
s = np.random.normal(mu, sigma, sampleNo )
plt.subplot(141)
plt.hist(s, 30, normed=True)
np.random.seed(0)
s = sigma * np.random.randn(sampleNo ) + mu
plt.subplot(142)
plt.hist(s, 30, normed=True)
np.random.seed(0)
s = sigma * np.random.standard_normal(sampleNo ) + mu
plt.subplot(143)
plt.hist(s, 30, normed=True)
# 二维正态分布
mu = np.array([[1, 5]])
Sigma = np.array([[1, 0.5], [1.5, 3]])
R = cholesky(Sigma)
s = np.dot(np.random.randn(sampleNo, 2), R) + mu
plt.subplot(144)
# 注意绘制的是散点图,而不是直方图
plt.plot(s[:,0],s[:,1],'+')
plt.show()