numpy.random.randn(d0,d1,…,dn)
返回一個或一組符合“標准正態分布“的樣本。dn表格每個維度,返回值為指定維度的array。
標准正態分布—-standard normal distribution
標准正態分布又稱為u分布,是以0為均值、以1為標准差的正態分布,記為N(0,1)。
1. 當沒有參數時,返回單個數據
>>> np.random.randn()
0.2856573215723444
2. 指定維數
>>> np.random.randn(2,4) array([[-1.80031882, 0.10318817, 1.05343294, -0.74251429], [-0.02053998, 0.58558613, -1.02284653, 0.33441884]]) >>> np.random.randn(2,2,2) array([[[ 0.76631648, -1.94176884], [-1.08613881, 0.70950631]], [[-1.04016301, -0.15358818], [-0.95679036, -0.23024994]]])
3. 生成符合$N(\mu, \sigma^2)$的樣本
sigma * np.random.randn(...) + mu
>>> 2*np.random.randn()+1 -1.6004536471034108 >>> 2.5*np.random.randn(2,4)+3 array([[ 3.58233674, -0.8538981 , 3.11623316, 3.15277312], [ 5.40095888, -1.34397929, 5.51338625, 6.74921732]])
numpy.random.standard_normal(size=None)
與前面的功能一模一樣,也是生成符合“標准正態分布”的樣本。不同之處在於其參數是元組形式。
>>> np.random.standard_normal() -0.42487006671195265 >>> np.random.standard_normal(10) array([ 0.78368072, 1.80768154, -0.49297587, 0.66436509, 0.35496744, 0.52050209, 0.06490782, -0.6404993 , -1.37450919, 0.27419667]) >>> np.random.standard_normal((2,2)) array([[ 0.15698487, -1.1685891 ], [-1.11065158, 0.61010709]]) >>> 2*np.random.standard_normal((2,2))+1 array([[ 1.01941899, 1.46028102], [-0.39625573, -1.52947869]])
numpy.random.normal(mu, sigma, num)
一種最為直接的方式
>>> np.random.normal(100,20,5) # (均值,標准差,個數) array([119.32107559, 121.76767718, 74.93802922, 91.40390427, 86.96341875])
參考鏈接:
