Python——NumPy的random子庫

NumPy的random子庫

np.random.*

np.random.rand()

np.random.randn()

np.random.randint()

import numpy as npa=np.random.rand(3,4,5)aOut[83]: array([[[ 0.08662874, 0.82948848, 0.68358736, 0.85925231, 0.18250681], [ 0.62005734, 0.38014728, 0.85111772, 0.07739155, 0.9670788 ], [ 0.83148769, 0.98684984, 0.17931358, 0.78663687, 0.32991487], [ 0.41630481, 0.40143165, 0.39719115, 0.35902372, 0.80809515]], [[ 0.83119559, 0.84908059, 0.03704835, 0.99169556, 0.25103526], [ 0.54950967, 0.21890653, 0.50118637, 0.61440841, 0.33158322], [ 0.28599297, 0.6478492 , 0.42480153, 0.64245498, 0.50198969], [ 0.87671252, 0.4551307 , 0.18533867, 0.38861156, 0.98937246]], [[ 0.21903302, 0.76057185, 0.51972563, 0.28018995, 0.9267844 ], [ 0.49750795, 0.86679355, 0.60877593, 0.9502196 , 0.63946047], [ 0.7766992 , 0.51985393, 0.9756528 , 0.57621679, 0.87955331], [ 0.6432478 , 0.35046943, 0.91971312, 0.51282177, 0.13310527]]])sn=np.random.randn(3,4,5)snOut[86]: array([[[-0.15116386, 0.85164049, 2.04232044, 0.5412239 , -0.65171862], [-0.23334418, -0.44215246, -1.19597071, -1.2189118 , 0.02157593], [ 0.91657483, 0.2611884 , 1.11715427, -1.02409543, -1.38927614], [-0.19741865, -0.15042967, 1.174679 , 1.27795408, -0.31847884]], [[ 1.4637826 , 1.43320029, -0.60038343, 1.39244389, -0.75747975], [ 0.52065785, -0.64790451, -0.32049525, 1.17868116, -0.05638849], [ 0.22874314, 0.68671056, -1.69309123, -0.54882906, -0.23721541], [-0.31578954, -0.44044017, -1.31905554, 2.13304617, -0.63259492]], [[ 0.23859545, 0.40294529, -0.2073546 , -0.90358886, -0.07341441], [-0.65382437, -0.21540712, -0.18190539, -1.32444175, -0.49808978], [ 0.68718048, 1.23431895, 0.01745539, 0.74168673, 2.06773505], [-2.61703882, 0.02591586, -0.45429583, -0.09624749, -0.44027003]]])b=np.random.randint(100,200,(3,4))bOut[88]: array([[133, 149, 151, 197], [160, 187, 108, 140], [139, 103, 168, 123]])b=np.random.randint(100,200,(3,4))bOut[90]: array([[166, 144, 136, 107], [106, 194, 175, 127], [115, 107, 132, 178]])np.random.seed(10)np.random.randint(100,200,(3,4))Out[92]: 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[94]: array([[109, 115, 164, 128], [189, 193, 129, 108], [173, 100, 140, 136]])np.random.seed(5)np.random.randint(100,200,(3,4))Out[97]: array([[199, 178, 161, 116], [173, 108, 162, 127], [130, 180, 107, 176]])np.random.seed(5)np.random.randint(100,200,(3,4))Out[99]: array([[199, 178, 161, 116], [173, 108, 162, 127], [130, 180, 107, 176]])

給定隨機數組種子之後,產生的隨機數組不變。

shuffle函數

import numpy as npa=np.random.randint(100,200,(3,4))aOut[102]: array([[115, 153, 180, 127], [144, 177, 175, 165], [147, 130, 184, 186]])np.random.shuffle(a)aOut[104]: array([[147, 130, 184, 186], [115, 153, 180, 127], [144, 177, 175, 165]])np.random.shuffle(a)aOut[106]: array([[147, 130, 184, 186], [115, 153, 180, 127], [144, 177, 175, 165]])np.random.shuffle(a)aOut[108]: array([[144, 177, 175, 165], [147, 130, 184, 186], [115, 153, 180, 127]])

shuffle函數隨意調換兩軸

permutation函數

a=np.random.randint(100,200,(3,4))aOut[110]: array([[141, 162, 101, 182], [116, 178, 105, 158], [100, 180, 104, 136]])np.random.permutation(a)Out[111]: array([[141, 162, 101, 182], [100, 180, 104, 136], [116, 178, 105, 158]])aOut[112]: array([[141, 162, 101, 182], [116, 178, 105, 158], [100, 180, 104, 136]])

permutation 函數作用之後並不改變數組a

choice 函數,抽取

import numpy as npb=np.random.randint(100,200,(8,))bOut[115]: array([127, 131, 102, 168, 138, 183, 119, 118])np.random.choice(b,(3,2))Out[116]: array([[131, 183], [118, 138], [138, 183]])np.random.choice(b,(3,2),replace=False)#replace表示是否可以重複抽取,默認為FalseOut[117]: array([[102, 131], [127, 138], [183, 168]])np.random.choice(b,(3,2),p=b/np.sum(b))#p是隨機概率,出現幾率與數字大小成正比。Out[118]: array([[118, 127], [183, 183], [131, 183]])

import numpy as npq=np.random.uniform(0,10,(3,4))qOut[122]: array([[ 5.75413707, 5.79721399, 0.64506899, 1.7724613 ], [ 3.41527086, 6.08702583, 1.95474956, 1.21548467], [ 9.34679509, 3.10979918, 4.74316569, 0.62211558]])n=np.random.normal(10,5,(3,4))nOut[124]: array([[ 5.46196987, 6.27937203, 9.22652647, 12.7923338 ], [ 2.38821804, 5.53678405, 13.12062969, 5.9740824 ], [ 11.06140028, 12.46176925, 18.3372659 , 0.47620034]])

推薦閱讀:

使用Flask開發簡單博客的教程(上)
看我如何進行Python對象注入利用
0x6:爬蟲
PyQt5系列教程(14):複選框
17個新手常見Python運行時錯誤

TAG:Python | Python教程 | numpy |