numpy.random.normal(loc=0.0, scale=1.0, size=None)
从正态分布(高斯分布)中抽取随机样本。
正态分布的概率密度函数,是De Moivre首次推导得出,并于200年后Gauss和Laplace分别独立推导出。由于它特殊的形状,常被称为钟形曲线。
正态分布在自然界中很常见。比如,它描述了受大量微小随机干扰影响的样本的常见分布,并且每个干扰都有自己独特的分布。
Parameters
loc [float or array_like of floats] 分布的中值("centre")。
scale [float or array_like of floats] 分布的标准差(spread or "width")。
size [int or tuple of ints, optional] 输出的形式(shape)。如果给定的形式为(m, n, k),那么m*n*k个样本将被提取出来;如果其值为None(default),并且参数loc和scale均为纯量(scalars),则返回一个样本;否则,np.broadcast(loc, scale).size个样本将被提取出来。
Returns
out [ndarry or scalar] 从参数化的正态分布中提取样本。
注:官方文档中关于正态分布的内容没有进行翻译。
Examples
从分布中提取样本:
>>> mu, sigma = 0, 0.1 #mean and standard deviation
>>> s = np.random.normal(mu, sigma, 1000)
验证均值和标准差:
>>> abs(mu - np.mean(s)) < 0.01
True
>>> abs(sigma - np.std(s, ddof = 1)) < 0.01
True
绘出样本的直方图和概率密度函数:
>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s, 30, density=True)
>>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
linewidth=2, color='r')
>>> plt.show()