Python numpy
np.random.randn及np.random.rand的区别
numpy中有一些常用的用来产生随机数的函数,randn()和rand()就属于这其中。
numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值。
numpy.random.rand(d0, d1, …, dn)的随机样本位于[0, 1)之间。
import numpy as np
print(np.random.rand(2,2))
print('--------------------')
print(np.random.rand(3,2))
print('********************')
print(np.random.randn(2,2))
print('--------------------')
print(np.random.randn(1,3))
output:
[[0.18098186 0.7482656 ]
[0.2342925 0.37032098]]
--------------------
[[0.77782759 0.82793442]
[0.91272684 0.49250067]
[0.12239144 0.50111556]]
********************
[[ 1.31480601 1.07406308]
[ 0.59012071 -0.767058 ]]
--------------------
[[-0.70718168 0.73662156 0.28464163]]
np.random.rand
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
numpy.random.rand(d0, d1, …, dn)的随机样本位于[0, 1)之间。
np.random.randn
numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值。
Notes
For random samples from N ( μ , σ 2 ) N(\mu, \sigma^2) N(μ,σ2), use:
sigma * np.random.randn(…) + mu
Examples
>>>
>>> np.random.randn()
2.1923875335537315 #random
Two-by-four array of samples from N(3, 6.25):
>>>
>>> 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], #random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) #random
参考文档:https://docs.scipy.org/doc/numpy-1.16.0/reference/routines.random.html