在机器学习中经常会遇到随机数据的生成,但是总是容易弄混,这里做一下总结对于numpy中的随机数生成模块。
函数 | 功能 | 返回值类型 |
---|---|---|
np.random.rand(d0,d1,…,dn) | 产生元素在(0,1)均匀分布的数组 | 浮点型 |
np.random.uniform(low,high,size) | 产生元素在[low,high]均匀分布的数组 | 浮点数 |
np.random.randint(low,high,size) | 产生元素在(low,high)均匀分布的整数 | 整数 |
np.random.randn(d0,d1,…,dn) | 产生呈标准正态分布的数组 | 浮点数 |
np.random.normal(mv,std,size) | 产生呈正态分布的数组 | 浮点数 |
代码:
import numpy as np
x = np.random.rand(3, 2)
print(x)
print('**'*15)
y = np.random.uniform(-100,100,20)
print(y)
print('**'*15)
a = np.random.randint(-100,100,20)
print(a)
print('**'*15)
b = np.random.randn(2,2,2)
print(b)
print('**'*15)
c = np.random.normal(2,0.5,6)
print(c)
运行结果
[[0.57625727 0.54617987]
[0.07921413 0.79125039]
[0.76974222 0.14886053]]
******************************
[ 75.38487896 -53.79125549 88.62105214 63.35055533 50.48705619
29.51594034 -50.84106033 37.3954892 90.95285229 -51.53446924
-43.21193046 -26.14729232 -86.96321266 69.83668251 -5.57903342
-2.6205638 -40.45688946 -45.45254006 12.32097219 -33.75984398]
******************************
[ 1 76 -72 40 82 30 33 -27 25 6 -42 -84 -77 -44
-65 39 -38 -54 -100 12]
******************************
[[[ 0.47601291 0.05191076]
[-0.31335795 1.55532678]]
[[ 0.07248383 -0.59444396]
[-0.76099338 -0.5459049 ]]]
******************************
[2.63077706 1.77712256 2.80524075 1.34858267 1.70068987 1.29810127]