python numpy之np.random的隨機(jī)數(shù)函數(shù)使用介紹
np.random的隨機(jī)數(shù)函數(shù)(1)
| 函數(shù) | 說明 |
|---|---|
| rand(d0,d1,..,dn) | 根據(jù)d0‐dn創(chuàng)建隨機(jī)數(shù)數(shù)組,浮點(diǎn)數(shù), [0,1),均勻分布 |
| randn(d0,d1,..,dn) | 根據(jù)d0‐dn創(chuàng)建隨機(jī)數(shù)數(shù)組,標(biāo)準(zhǔn)正態(tài)分布 |
| randint(low[,high,shape]) | 根據(jù)shape創(chuàng)建隨機(jī)整數(shù)或整數(shù)數(shù)組,范圍是[low, high) |
| seed(s) | 隨機(jī)數(shù)種子, s是給定的種子值 |
np.random.rand
import numpy as np
a = np.random.rand(3, 4, 5)
a
Out[3]:
array([[[0.28576737, 0.96566496, 0.59411491, 0.47805199, 0.97454449],
[0.15970049, 0.35184063, 0.66815684, 0.13571458, 0.41168113],
[0.66737322, 0.91583297, 0.68033204, 0.49083857, 0.33549182],
[0.52797439, 0.23526146, 0.39731129, 0.26576975, 0.26846021]],
[[0.46860445, 0.84988491, 0.92614786, 0.76410349, 0.00283208],
[0.88036955, 0.01402271, 0.59294569, 0.14080713, 0.72076521],
[0.0537956 , 0.08118672, 0.59281986, 0.60544876, 0.77931621],
[0.41678215, 0.24321042, 0.25167563, 0.94738625, 0.86642919]],
[[0.36137271, 0.21672667, 0.85449629, 0.51065516, 0.16990425],
[0.97507815, 0.78870518, 0.36101021, 0.56538782, 0.56392004],
[0.93777677, 0.73199966, 0.97342172, 0.42147127, 0.73654324],
[0.83139234, 0.00221262, 0.51822612, 0.60964223, 0.83029954]]])
np.random.randn
b = np.random.randn(3, 4, 5)
b
Out[5]:
array([[[ 0.09170952, -0.36083675, -0.18189783, -0.52370155,
-0.61183783],
[ 1.05285606, -0.82944771, -0.93438396, 0.32229904,
-0.85316565],
[ 1.41103666, -0.32534111, -0.02202953, 1.02101228,
1.59756695],
[-0.33896372, 0.42234042, 0.14297587, -0.70335248,
0.29436318]],
[[ 0.73454216, 0.35412624, -1.76199508, 1.79502353,
1.05694614],
[-0.42403323, -0.36551581, 0.54033378, -0.04914723,
1.15092556],
[ 0.48814148, 1.09265266, 0.65504441, -1.04280834,
0.70437122],
[ 2.92946803, -1.73066859, -0.30184912, 1.04918753,
-1.58460681]],
[[ 1.24923498, -0.65467868, -1.30427044, 1.49415265,
0.87520623],
[-0.26425316, -0.89014489, 0.98409579, 1.13291179,
-0.91343016],
[-0.71570644, 0.81026219, -0.00906133, 0.90806035,
-0.914998 ],
[ 0.22115875, -0.81820313, 0.66359573, -0.1490853 ,
0.75663096]]])
np.random.randint
c = np.random.randint(100, 200, (3, 4))
c
Out[9]:
array([[104, 140, 161, 193],
[134, 147, 126, 120],
[117, 141, 162, 137]])
numpy.random.randint的詳細(xì)用法 - python
函數(shù)的作用是,返回一個(gè)隨機(jī)整型數(shù),范圍從低(包括)到高(不包括),即[low, high)。如果沒有寫參數(shù)high的值,則返回[0,low)的值。
numpy.random.randint(low, high=None, size=None, dtype='l')
參數(shù)如下:
| 參數(shù) | 描述 |
|---|---|
| low: int | 生成的數(shù)值最低要大于等于low。 (hign = None時(shí),生成的數(shù)值要在[0, low)區(qū)間內(nèi)) |
| high: int (可選) | 如果使用這個(gè)值,則生成的數(shù)值在[low, high)區(qū)間。 |
| size: int or tuple of ints(可選) | 輸出隨機(jī)數(shù)的尺寸,比如size=(m * n* k)則輸出同規(guī)模即m * n* k個(gè)隨機(jī)數(shù)。默認(rèn)是None的,僅僅返回滿足要求的單一隨機(jī)數(shù)。 |
| dtype: dtype(可選): | 想要輸出的格式。如int64、int等等 |
輸出:
返回一個(gè)隨機(jī)數(shù)或隨機(jī)數(shù)數(shù)組
例子
>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
>>> np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])>>>np.random.randint(2, high=10, size=(2,3))
array([[6, 8, 7],
[2, 5, 2]])
np.random.seed
隨機(jī)種子生成器,使下一次生成的隨機(jī)數(shù)為由種子數(shù)決定的“特定”的隨機(jī)數(shù),如果seed中參數(shù)為空,則生成的隨機(jī)數(shù)“完全”隨機(jī)。參考和文檔。
np.random.seed(10)
np.random.randint(100, 200, (3 ,4))
Out[11]:
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[13]:
array([[109, 115, 164, 128],
[189, 193, 129, 108],
[173, 100, 140, 136]])
np.random的隨機(jī)數(shù)函數(shù)(2)
| 函數(shù) | 說明 |
|---|---|
| shuffle(a) | 根據(jù)數(shù)組a的第1軸(也就是最外層的維度)進(jìn)行隨排列,改變數(shù)組x |
| permutation(a) | 根據(jù)數(shù)組a的第1軸產(chǎn)生一個(gè)新的亂序數(shù)組,不改變數(shù)組x |
| choice(a[,size,replace,p]) | 從一維數(shù)組a中以概率p抽取元素,形成size形狀新數(shù)組replace表示是否可以重用元素,默認(rèn)為False |
np.random.shuffle
a = np.random.randint(100, 200, (3, 4))
a
Out[15]:
array([[116, 111, 154, 188],
[162, 133, 172, 178],
[149, 151, 154, 177]])
np.random.shuffle(a)
a
Out[17]:
array([[116, 111, 154, 188],
[149, 151, 154, 177],
[162, 133, 172, 178]])
np.random.shuffle(a)
a
Out[19]:
array([[162, 133, 172, 178],
[116, 111, 154, 188],
[149, 151, 154, 177]])
可以看到,a發(fā)生了變化,軸。
np.random.permutation
b = np.random.randint(100, 200, (3, 4))
b
Out[21]:
array([[113, 192, 186, 130],
[130, 189, 112, 165],
[131, 157, 136, 127]])
np.random.permutation(b)
Out[22]:
array([[113, 192, 186, 130],
[130, 189, 112, 165],
[131, 157, 136, 127]])
b
Out[24]:
array([[113, 192, 186, 130],
[130, 189, 112, 165],
[131, 157, 136, 127]])
可以看到,b沒有發(fā)生改變。
np.random.choice
c = np.random.randint(100, 200, (8,))
c
Out[26]: array([123, 194, 111, 128, 174, 188, 109, 115])
np.random.choice(c, (3, 2))
Out[27]:
array([[111, 123],
[109, 115],
[123, 128]])#默認(rèn)可以出現(xiàn)重復(fù)值
np.random.choice(c, (3, 2), replace=False)
Out[28]:
array([[188, 111],
[123, 115],
[174, 128]])#不允許出現(xiàn)重復(fù)值
np.random.choice(c, (3, 2),p=c/np.sum(c))
Out[29]:
array([[194, 188],
[109, 111],
[174, 109]])#指定每個(gè)值出現(xiàn)的概率
np.random的隨機(jī)數(shù)函數(shù)(3)
| 函數(shù) | 說明 |
|---|---|
| uniform(low,high,size) | 產(chǎn)生具有均勻分布的數(shù)組,low起始值,high結(jié)束值,size形狀 |
| normal(loc,scale,size) | 產(chǎn)生具有正態(tài)分布的數(shù)組,loc均值,scale標(biāo)準(zhǔn)差,size形狀 |
| poisson(lam,size) | 產(chǎn)生具有泊松分布的數(shù)組,lam隨機(jī)事件發(fā)生率,size形狀 |
u = np.random.uniform(0, 10, (3, 4))
u
Out[31]:
array([[9.83020867, 4.67403279, 8.75744495, 2.96068699],
[1.31291053, 8.42817933, 6.59036304, 5.95439605],
[4.36353698, 3.56250327, 5.87130925, 1.49471337]])
n = np.random.normal(10, 5, (3, 4))
n
Out[33]:
array([[ 8.17771928, 4.17423265, 3.28465058, 17.2669643 ],
[10.00584724, 9.94039808, 13.57941572, 4.07115727],
[ 6.81836048, 6.94593078, 3.40304302, 7.19135792]])
p = np.random.poisson(2.0, (3, 4))
p
Out[35]:
array([[0, 2, 2, 1],
[2, 0, 1, 3],
[4, 2, 0, 3]])
數(shù)據(jù)分析師分析問題第一步,必須明確這是不是一個(gè)問題?。?!
- Python?numpy中np.random.seed()的詳細(xì)用法實(shí)例
- Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解
- numpy.random.shuffle打亂順序函數(shù)的實(shí)現(xiàn)
- numpy.random模塊用法總結(jié)
- Numpy之random函數(shù)使用學(xué)習(xí)
- 基于numpy.random.randn()與rand()的區(qū)別詳解
- numpy.random.seed()的使用實(shí)例解析
- 詳述numpy中的np.random.random()系列函數(shù)用法
相關(guān)文章
Python?OpenCV超詳細(xì)講解圖像堆疊的實(shí)現(xiàn)
OpenCV用C++語言編寫,它具有C ++,Python,Java和MATLAB接口,并支持Windows,Linux,Android和Mac OS,OpenCV主要傾向于實(shí)時(shí)視覺應(yīng)用,并在可用時(shí)利用MMX和SSE指令,本篇文章帶你通過OpenCV實(shí)現(xiàn)圖像堆疊2022-04-04
詳解pyqt中解決國際化tr()函數(shù)不起作用的問題
本文主要介紹了pyqt中解決國際化tr()函數(shù)不起作用的問題,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Python Numpy實(shí)現(xiàn)計(jì)算矩陣的均值和標(biāo)準(zhǔn)差詳解
NumPy(Numerical Python)是Python的一種開源的數(shù)值計(jì)算擴(kuò)展。這種工具可用來存儲(chǔ)和處理大型矩陣,比Python自身的嵌套列表結(jié)構(gòu)要高效的多。本文主要介紹用NumPy實(shí)現(xiàn)計(jì)算矩陣的均值和標(biāo)準(zhǔn)差,感興趣的小伙伴可以了解一下2021-11-11
python動(dòng)態(tài)視頻下載器的實(shí)現(xiàn)方法
這里向大家分享一下python爬蟲的一些應(yīng)用,主要是用爬蟲配合簡單的GUI界面實(shí)現(xiàn)視頻,音樂和小說的下載器。今天就先介紹如何實(shí)現(xiàn)一個(gè)動(dòng)態(tài)視頻下載器,需要的朋友可以參考下2019-09-09
淺析Python中g(shù)lobal和nonlocal關(guān)鍵字的妙用
這篇文章主要來和大家一起深入探討Python中關(guān)鍵詞global和nonlocal的用法,包括詳細(xì)的示例代碼和實(shí)際應(yīng)用場景,感興趣的可以了解下2024-04-04
Python調(diào)整matplotlib圖片大小的3種方法匯總
我們在使用matplotlib作圖時(shí),會(huì)遇到圖片不清晰或者圖片大小不是我們想要的,這個(gè)時(shí)候就需要調(diào)整下,這篇文章主要給大家介紹了關(guān)于Python調(diào)整matplotlib圖片大小的3種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

