numpy随机函数
numpy的random子库下的随机函数
目录
- 1.rand
- 2.randn
- 3.randint
- 4.seed
- 5.shuffle
- 6.permutation
- 7.choice
- 8.uniform
- 9.normal
- 10.poisson
1.rand
rand是随机生成0~1间的数
import numpy as np
a = np.random.rand(3,4,5)
a
Out[24]:
array([[[ 0.05264203, 0.77135103, 0.39844803, 0.63423337, 0.95030544],
[ 0.53296936, 0.54940495, 0.8576418 , 0.74994533, 0.40514132],
[ 0.39233205, 0.16553792, 0.38658738, 0.23610075, 0.88490145],
[ 0.0424519 , 0.90977379, 0.42196034, 0.11599203, 0.24613255]],
[[ 0.5738224 , 0.23753014, 0.43827026, 0.29509809, 0.37022078],
[ 0.96650836, 0.97909827, 0.76880287, 0.94782555, 0.24924757],
[ 0.59571374, 0.17013425, 0.91762409, 0.00171431, 0.18229415],
[ 0.61888901, 0.22581218, 0.43153159, 0.38375907, 0.75613752]],
[[ 0.84475676, 0.02163288, 0.88988614, 0.01136896, 0.17098993],
[ 0.91532591, 0.88403463, 0.63324186, 0.71577691, 0.07208857],
[ 0.58328676, 0.95853235, 0.91042669, 0.31245884, 0.85970963],
[ 0.12147907, 0.52869864, 0.59917159, 0.10043494, 0.3671587 ]]])
2.randn
randn是按照正态分布的特点生成随机数
sn = np.random.randn(3,4,5)
sn
Out[26]:
array([[[ 0.23392058, 0.56810173, 0.07808279, -0.66913955, 1.60417157],
[-0.12676697, 1.6127401 , 0.64101265, 0.36979941, -0.82205561],
[ 2.18564337, -1.90988757, -1.16970225, 0.27135946, 0.51510032],
[ 0.67365435, 1.1772794 , -1.36251015, 0.25417482, -0.52221412]],
[[-1.6532865 , -0.63458442, 0.53373936, -0.38657552, -0.13062583],
[-0.77471447, -0.89519535, -1.39090452, 1.02563876, 0.07769467],
[ 1.62532173, 0.25651162, 0.15851992, -0.42143294, 0.60057105],
[-1.16098414, -0.32735729, 1.80220265, 0.01483692, 3.32443294]],
[[-2.47463154, -1.00232746, 0.58410508, -0.49289054, 1.39070243],
[-0.06172589, -0.88556799, 0.17307217, 1.17729613, -0.29747808],
[-0.76346769, -1.52811156, 0.99343325, -0.05743596, 0.2848067 ],
[-1.71415298, 0.98683116, -0.05011351, 0.09894812, -0.75880232]]])
3.randint
randint是生成随机整数
b = np.random.randint(100,200,(3,4))
b
Out[28]:
array([[110, 160, 147, 144],
[119, 195, 107, 111],
[169, 188, 160, 126]])
4.seed函数
随机数种子seed,可以记住你生成的随机数,只要使用这个种子,就可以随机出一样的数据。
np.random.seed(10)
np.random.randint(100,200,(3,4))
Out[30]:
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[32]:
array([[109, 115, 164, 128],
[189, 193, 129, 108],
[173, 100, 140, 136]])
5.shuffle(a)函数
根据数组的第一轴进行随机排列,改变数组x。
a = np.random.randint(100,200,(3,4))
a
Out[35]:
array([[116, 111, 154, 188],
[162, 133, 172, 178],
[149, 151, 154, 177]])
np.random.shuffle(a)
a
Out[37]:
array([[116, 111, 154, 188],
[149, 151, 154, 177],
[162, 133, 172, 178]])
np.random.shuffle(a)
a
Out[39]:
array([[162, 133, 172, 178],
[116, 111, 154, 188],
[149, 151, 154, 177]])
6.permutation(a)函数
根据数组的第一轴产生一个新的乱序数组,不改变数组x。
使用后可能并不会产生乱序。
a = np.random.randint(100,200,(3,4))
a
Out[41]:
array([[113, 192, 186, 130],
[130, 189, 112, 165],
[131, 157, 136, 127]])
np.random.permutation(a)
Out[42]:
array([[113, 192, 186, 130],
[130, 189, 112, 165],
[131, 157, 136, 127]])
np.random.permutation(a)
Out[43]:
array([[131, 157, 136, 127],
[113, 192, 186, 130],
[130, 189, 112, 165]])
a
Out[44]:
array([[113, 192, 186, 130],
[130, 189, 112, 165],
[131, 157, 136, 127]])
7.choice(a[,size,replace,p])函数
从一维数组中以概率P抽取元素(相当于给元素赋以权重),形成size形状数组,replace表示是否可以重用元素,默认False。
b = np.random.randint(100,200,(8,))
b
Out[48]: array([111, 117, 146, 107, 175, 128, 133, 184])
np.random.choice(b, (3,2),)
Out[49]:
array([[146, 111],
[111, 175],
[128, 175]])
np.random.choice(b, (3,2),replace=False)
Out[50]:
array([[117, 107],
[175, 128],
[146, 133]])
np.random.choice(b, (3,2),p=b/np.sum(b))
Out[51]:
array([[184, 175],
[184, 146],
[117, 184]])
8.uniform(low,high,size)函数
产生具有均匀分布的数组,(起始值,结束值,形状)
u = np.random.uniform(0,10,(3,4))
u
Out[53]:
array([[ 6.59036304, 5.95439605, 4.36353698, 3.56250327],
[ 5.87130925, 1.49471337, 1.71238598, 3.97164523],
[ 6.37951564, 3.72519952, 0.02406761, 5.48816356]])
9.normal(loc,scale,size)函数
产生具有正态分布的数组,(均值,标准差,形状)。正态分布是围绕某一均值,在特定方差下,一种随机变量的取值的空间可能。
n = np.random.normal(10,5,(3,4))
n
Out[55]:
array([[ 13.57941572, 4.07115727, 6.81836048, 6.94593078],
[ 3.40304302, 7.19135792, 11.02692287, 5.23319662],
[ 11.60758976, 2.39530663, -0.80726459, 11.72656647]])
10.poisson(lam,size)函数
产生具有泊松分布的数组,(随机事件发生率,形状)