参考链接: NumPy官网
参考链接: NumPy: the absolute basics for beginners
参考链接: Quickstart tutorial
参考链接: Broadcasting广播
参考链接: NumPy 中文教程
参考链接: Python数据分析与展示
使用方式: np.random.XXX
np.random的随机数函数:
函数 | 说明 |
---|---|
rand(d0,d1,…,dn) | 根据d0‐dn创建随机数数组,浮点数,[0,1),均匀分布 |
randn(d0,d1,…,dn) | 根据d0‐dn创建随机数数组,标准正态分布 |
randint(low[,high,shape]) | 根据shape创建随机整数或整数数组,范围是[low, high) |
seed(s) | 随机数种子,s是给定的种子值 |
shuffle(a) | 根据数组a的第1轴进行随排列,改变数组x |
permutation(a) | 根据数组a的第1轴产生一个新的乱序数组,不改变数组x |
choice(a[,size,replace,p]) | 从一维数组a中以概率p抽取元素,形成size形状新数组,replace表示是否可以重用元素,默认为False |
uniform(low,high,size) | 产生具有均匀分布的数组,low起始值,high结束值,size形状 |
normal(loc,scale,size) | 产生具有正态分布的数组,loc均值,scale标准差,size形状 |
poisson(lam,size) | 产生具有泊松分布的数组,lam随机事件发生率,size形状 |
实验代码展示:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> a = np.random.rand(3,4,5)
>>> a
array([[[0.43461118, 0.67152066, 0.92047132, 0.56249509, 0.06518187],
[0.8849577 , 0.14584999, 0.04442541, 0.79034329, 0.37392931],
[0.71974235, 0.60252472, 0.67803274, 0.33991318, 0.09877394],
[0.12174179, 0.65811069, 0.31359034, 0.25219828, 0.44985309]],
[[0.15000427, 0.58408875, 0.24537165, 0.62653904, 0.22474145],
[0.91677429, 0.08491544, 0.36055195, 0.89321353, 0.00688322],
[0.93760713, 0.68377657, 0.86612548, 0.42936391, 0.70617769],
[0.78155284, 0.29953589, 0.94067568, 0.11961233, 0.845394 ]],
[[0.42883889, 0.85582328, 0.72197987, 0.74451102, 0.52555016],
[0.1128279 , 0.05550925, 0.02739479, 0.01039148, 0.06343216],
[0.23364621, 0.8521507 , 0.61302197, 0.09323585, 0.21797215],
[0.31202329, 0.56006766, 0.100646 , 0.50536627, 0.67868073]]])
>>> sn = np.random.randn(3,4,5)
>>> sn
array([[[ 0.5614552 , 0.93187108, -1.08447351, 1.52800922,
1.90302427],
[ 1.57919135, 1.29580869, 2.33811499, -1.06149503,
-2.30370559],
[-1.72034522, 0.31021075, -1.15561603, -1.76427148,
0.47669142],
[ 0.35410063, 1.06367956, 0.48307513, -0.84284657,
0.55155055]],
[[-0.12646501, -1.76574052, -0.34643498, -1.11375249,
0.20681335],
[ 0.63951291, -1.15254676, 1.95572231, 0.19580836,
0.2592714 ],
[-1.00948396, 1.05556101, -1.64752325, 0.49618588,
0.15097269],
[-0.19022664, 0.43933103, -0.41849364, 1.23566172,
1.06333256]],
[[ 0.15856644, 0.12855378, -0.06889352, -0.76362172,
0.65449572],
[ 0.29455946, 0.60693076, -0.81099247, -1.64899023,
0.38440449],
[-1.51466305, 0.41013325, 0.62540417, -0.41832723,
-1.96782065],
[ 0.69615753, -0.16730052, -0.33291391, -0.31524605,
0.00262408]]])
>>> b = np.random.randint(100,200,(3,4))
>>> b
array([[190, 130, 148, 137],
[199, 182, 139, 180],
[145, 128, 192, 145]])
>>> np.random.seed(10)
>>> np.random.randint(100,200,(3,4))
array([[109, 115, 164, 128],
[189, 193, 129, 108],
[173, 100, 140, 136]])
>>> a = np.random.randint(100,200,(3,4))
>>> a
array([[116, 111, 154, 188],
[162, 133, 172, 178],
[149, 151, 154, 177]])
>>> np.random.shuffle(a)
>>> a
array([[116, 111, 154, 188],
[149, 151, 154, 177],
[162, 133, 172, 178]])
>>> # a已经改变
>>> np.random.permutation(a)
array([[162, 133, 172, 178],
[116, 111, 154, 188],
[149, 151, 154, 177]])
>>> a
array([[116, 111, 154, 188],
[149, 151, 154, 177],
[162, 133, 172, 178]])
>>> # a不会发生变化
>>> np.random.permutation(a)
array([[162, 133, 172, 178],
[116, 111, 154, 188],
[149, 151, 154, 177]])
>>> a
array([[116, 111, 154, 188],
[149, 151, 154, 177],
[162, 133, 172, 178]])
>>> b = np.random.randint(100,200,(8,))
>>> b
array([186, 130, 130, 189, 112, 165, 131, 157])
>>> np.random.choice(b,(3,2))
array([[112, 189],
[130, 165],
[165, 131]])
>>> np.random.choice(b,(3,2),replace=False)
array([[130, 112],
[130, 165],
[189, 186]])
>>> np.random.choice(b,(3,2),replace=True)
array([[130, 157],
[130, 186],
[131, 157]])
>>>
>>>
>>>
>>>
>>> b = np.random.randint(100,200,(8,))
>>> b
array([188, 111, 117, 146, 107, 175, 128, 133])
>>> np.random.choice(b,(3,2))
array([[107, 117],
[188, 188],
[107, 175]])
>>> np.random.choice(b,(3,2),replace=False)
array([[111, 146],
[133, 175],
[117, 128]])
>>> np.random.choice(b,(3,2),replace=False)
array([[111, 146],
[128, 107],
[117, 133]])
>>> np.random.choice(b,(3,2),replace=True)
array([[107, 188],
[128, 128],
[111, 111]])
>>> np.random.choice(b,(3,2),replace=True)
array([[133, 107],
[188, 117],
[175, 111]])
>>> np.random.choice(b,(3,2),replace=True)
array([[146, 111],
[117, 175],
[188, 111]])
>>> np.random.choice(b,(3,2),replace=True)
array([[111, 175],
[188, 117],
[133, 146]])
>>> np.random.choice(b,(3,2),replace=True)
array([[175, 188],
[107, 117],
[188, 128]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b))
array([[111, 111],
[146, 111],
[188, 107]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b))
array([[111, 107],
[107, 117],
[175, 128]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b))
array([[188, 107],
[117, 146],
[188, 117]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b),replace=False)
array([[188, 175],
[146, 133],
[107, 111]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b),replace=False)
array([[117, 128],
[146, 188],
[175, 111]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b),replace=False)
array([[107, 175],
[128, 188],
[146, 111]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b),replace=True)
array([[107, 111],
[111, 128],
[188, 146]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b),replace=True)
array([[111, 117],
[188, 117],
[133, 111]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b))
array([[175, 133],
[128, 175],
[111, 107]])
>>> np.random.choice(b,(3,2),p=b/np.sum(b))
array([[133, 175],
[133, 175],
[117, 188]])
>>> u = np.random.uniform(0,10,(3,4))
>>> u
array([[2.20627863, 1.37373246, 7.44295925, 3.46690659],
[5.77664255, 1.68352936, 0.37971704, 8.17176704],
[8.12464853, 1.8843566 , 3.73062398, 7.44192136]])
>>> n = np.random.normal(10,5,(3,4))
>>> n
array([[ 8.26351631, 3.67967796, 13.43147307, 3.99146985],
[13.12600404, 12.46786526, 13.11474845, 7.95478642],
[ 8.4110399 , 5.40749947, 12.44144566, 10.96753023]])
>>> p = np.random.poisson(5,(3,4))
>>> p
array([[4, 2, 7, 1],
[7, 7, 6, 4],
[3, 4, 5, 3]])
>>>
>>>