我们更经常会用到的np.random.randn(size)所谓标准正态分布(μ=0,σ=1),对应于numpy.random.normal(loc=0.0, scale=1.0, size=None)
loc:float 此概率分布的均值(对应着整个分布的中心centre)。
scale:float 此概率分布的标准差(对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高)。
size:int or tuple of ints 输出的shape,默认为None,只输出一个值。
import numpy as np a = np.random.normal(0, 1, (2, 4)) print(a)
import numpy as np import matplotlib.pyplot as plt import math def func_normal_distribution(x, mean, sigma): return np.exp(-1*((x-mean)**2)/(2*(sigma**2)))/(math.sqrt(2*np.pi)* sigma) mean1, sigma1 = 2,0.5 x1 = np.linspace(mean1 - 6*sigma1, mean1 + 6*sigma1, 100) mean2, sigma2 = 2,1 x2 = np.linspace(mean2 - 6*sigma2, mean2 + 6*sigma2, 100) mean3, sigma3 = 3,1 x3 = np.linspace(mean3 - 6*sigma3, mean3 + 6*sigma3, 100) y1 = func_normal_distribution(x1, mean1, sigma1) y2 = func_normal_distribution(x2, mean2, sigma2) y3 = func_normal_distribution(x3, mean3, sigma3) plt.plot(x1, y1, 'r', label='m=2,sig=0.5') plt.plot(x2, y2, 'g', label='m=2,sig=1') plt.plot(x3, y3, 'b', label='m=3,sig=1') plt.legend() plt.grid() plt.show()