np.random的随机数函数(1)
函数 |
说明 |
rand(d0,d1,..,dn) |
根据d0‐dn创建随机数数组,浮点数,[0,1),均匀分布 |
randn(d0,d1,..,dn) |
根据d0‐dn创建随机数数组,标准正态分布 |
randint(low[, high, size, dtype]) |
根据shape创建随机整数或整数数组,范围是[low, high) |
seed(s) |
随机数种子,s是给定的种子值 |
In [111]: a=np.random.rand(3,4,5)
In [112]: a
Out[112]:
array([[[ 0.63950086, 0.49401844, 0.95092445, 0.6296413 , 0.12765929],
[ 0.16655989, 0.65606825, 0.18283651, 0.16115097, 0.52061122],
[ 0.18161837, 0.25358793, 0.57434442, 0.69075542, 0.23953812],
[ 0.08576846, 0.95448335, 0.67868061, 0.0590973 , 0.84406012]],
[[ 0.13214632, 0.86184715, 0.8840964 , 0.76590103, 0.25946326],
[ 0.96485042, 0.84343539, 0.01330944, 0.90897105, 0.31790772],
[ 0.79015567, 0.51506586, 0.27724041, 0.1917755 , 0.44754523],
[ 0.41862119, 0.91555119, 0.43644534, 0.09347175, 0.17778366]],
[[ 0.71664568, 0.90315897, 0.9189659 , 0.1539199 , 0.45731278],
[ 0.14990219, 0.93837411, 0.00191613, 0.0168762 , 0.6536925 ],
[ 0.96888584, 0.79590976, 0.48246297, 0.80830351, 0.31208944],
[ 0.43943862, 0.26383419, 0.9901099 , 0.76466069, 0.63373991]]])
In [113]: sn=np.random.randn(3,4,5)
In [114]: sn
Out[114]:
array([[[-0.08168246, -0.56978596, 1.32457461, 1.43918295, -1.29833204],
[ 0.38862065, 0.4401511 , -0.40245539, -0.13615733, -0.13053578],
[-0.91701962, 1.76102932, 0.68526847, 0.81736871, -0.0638541 ],
[-1.62348669, 0.15310889, 0.23629247, 0.83396329, -0.44141945]],
[[ 0.72809728, -0.27064699, 0.5788344 , -0.24059207, -0.1946698 ],
[-0.21123992, 0.9043685 , 0.56424924, 0.88563487, 0.89425021],
[-1.08759379, -0.12982953, 0.94186859, -0.88278104, -0.8776474 ],
[-1.37680398, 0.51352443, -0.1981024 , 1.68808853, -0.15855771]],
[[ 0.61914595, 0.4525279 , 0.71314203, -1.54645584, 1.23416512],
[-1.16559168, 0.69095569, -0.8341331 , 1.27323713, -0.69553003],
[ 0.55813805, 0.30831781, 0.85372412, -0.08877067, 1.09368115],
[-0.98600832, 0.234836 , -1.5124326 , 0.17147776, -0.99554203]]])
In [115]: b=np.random.randint(100,200,(3,4))
In [116]: b
Out[116]:
array([[114, 160, 184, 124],
[141, 178, 142, 153],
[172, 142, 191, 145]])
b=np.random.randint(100,200,(3,4))
b
Out[116]:
array([[114, 160, 184, 124],
[141, 178, 142, 153],
[172, 142, 191, 145]])
np.random.seed(10)
np.random.randint(100,200,(3,4))
Out[118]:
array([[109, 115, 164, 128],
[189, 193, 129, 108],
[173, 100, 140, 136]])
np.random.seed(10)
np.random.randint(100,200,(3,4))
Out[120]:
array([[109, 115, 164, 128],
[189, 193, 129, 108],
[173, 100, 140, 136]])
通过设定和重复使用随机树种seed,可以得到相同的随机数数组
np.random的随机数函数(2)
函数 |
说明 |
shuffle(a) |
根据数组a的第1轴进行随排列,改变数组x |
permutation(a) |
根据数组a的第1轴产生一个新的乱序数组,不改变数组x |
choice(a[, size, replace, p]) |
从一维数组a中以概率p抽取元素,形成size形状新数组replace表示是否可以重用元素,默认为True |
np.random的随机数函数(3)
函数 |
说明 |
uniform(low,high,size) |
产生具有均匀分布的数组,low起始值,high结束值,size形状 |
normal(loc,scale,size) |
产生具有正态分布的数组,loc均值,scale标准差,size形状 |
poisson(lam,size) |
产生具有泊松分布的数组,lam随机事件发生率,size形状 |
In [125]: u=np.random.uniform(0,10,(3,4))
In [126]: u
Out[126]:
array([[ 4.13667374, 7.78728808, 5.83901366, 1.82631436],
[ 8.26082248, 1.05401833, 2.83576679, 0.65563266],
[ 0.56444187, 7.65455818, 0.11788029, 6.11943341]])
In [127]: n=np.random.normal(10,5,(3,4))
In [128]: n
Out[128]:
array([[ 13.39465597, 0.43222893, 5.28770972, 1.2722851 ],
[ 4.89527367, 9.18965487, 6.7101455 , 12.11056531],
[ 13.12011669, 12.50820048, 14.20717363, 1.79875008]])
网友评论