python numpy 常用隨機(jī)數(shù)的產(chǎn)生方法的實(shí)現(xiàn)
numpy 中 的random模塊有多個(gè)函數(shù)用于生成不同類型的隨機(jī)數(shù),常見(jiàn)的有 uniform、rand、random、randint、random_interges
下面介紹一下各自的用法
1、np.random.uniform的用法
np.random.uniform(low=0.0, high=1.0, size=None)
作用:可以生成[low,high)中的隨機(jī)數(shù),可以是單個(gè)值,也可以是一維數(shù)組,也可以是多維數(shù)組
參數(shù)介紹:
- low :float型,或者是數(shù)組類型的,默認(rèn)為0
- high:float型,或者是數(shù)組類型的,默認(rèn)為1
- size:int型,或元組,默認(rèn)為空
In[1]: import numpy as np
In[2]: np.random.uniform() # 默認(rèn)為0到1
Out[2]: 0.827455693512018
In[3]: np.random.uniform(1,5)
Out[3]: 2.93533586182789
In[4]: np.random.uniform(1,5,4) #生成一維數(shù)組
Out[4]: array([ 3.18487512, 1.40233721, 3.17543152, 4.06933042])
In[5]: np.random.uniform(1,5,(4,3)) #生成4x3的數(shù)組
Out[5]:
array([[ 2.33083328, 1.592934 , 2.38072 ],
[ 1.07485686, 4.93224857, 1.42584919],
[ 3.2667912 , 4.57868281, 1.53218578],
[ 4.17965117, 3.63912616, 2.83516143]])
In[6]: np.random.uniform([1,5],[5,10])
Out[6]: array([ 2.74315143, 9.4701426 ])
2、np.random.random_sample的用法
和np.random.random作用一樣
random_sample(size=None)
- 作用:返回[0,1)之間的浮點(diǎn)型隨機(jī)數(shù),通過(guò)size控制返回的形狀
np.random.random_sample()
0.47108547995356098
type(np.random.random_sample())
<type 'float'>
np.random.random_sample((5,))
array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])
Three-by-two array of random numbers from [-5, 0):
5 * np.random.random_sample((3, 2)) - 5
array([[-3.99149989, -0.52338984],
[-2.99091858, -0.79479508],
[-1.23204345, -1.75224494]])
3、np.random.rand的用法
rand(d0, d1, …, dn)
作用:返回[0,1)內(nèi)的浮點(diǎn)數(shù),輸入的d0,d1…dn代表維度信息,沒(méi)有輸入時(shí),則返回[0,1)內(nèi)的一個(gè)隨機(jī)值
In[15]: np.random.rand()
Out[15]: 0.9027797355532956
In[16]:np.random.rand(3,3)
Out[16]:
array([[ 0.47507608, 0.64225621, 0.9926529 ],
[ 0.95028412, 0.18413813, 0.91879723],
[ 0.89995217, 0.42356103, 0.81312942]])
In[17]: np.random.rand(3,3,3)
Out[17]:
array([[[ 0.30295904, 0.76346848, 0.33125168],
[ 0.77845927, 0.75020602, 0.84670385],
[ 0.2329741 , 0.65962263, 0.93239286]],
[[ 0.24575304, 0.9019242 , 0.62390674],
[ 0.43663215, 0.93187574, 0.75302239],
[ 0.62658734, 0.01582182, 0.66478944]],
[[ 0.22152418, 0.51664503, 0.41196781],
[ 0.47723318, 0.19248885, 0.29699868],
[ 0.11664651, 0.66718804, 0.39836448]]])
4、np.random.randint的用法
randint(low, high=None, size=None, dtype='l')
作用:生成整型隨機(jī)數(shù),可以是單個(gè)隨機(jī)數(shù),也可以是多維的隨機(jī)數(shù)構(gòu)成的數(shù)組
參數(shù)介紹
- low:int 型,隨機(jī)數(shù)的下限
- high:int 型,默認(rèn)為空,隨機(jī)數(shù)的上限,當(dāng)此值為空時(shí),函數(shù)生成[0,low)區(qū)間內(nèi)的隨機(jī)數(shù)
- size:int、或ints、或元組,指明生成的隨機(jī)數(shù)的類型
- dtype:可選'int' ,'int32',默認(rèn)為'l'
In[7]: np.random.randint(4)
Out[7]: 1
In[8]: np.random.randint(4,size=4)
Out[8]: array([2, 2, 2, 0])
In[9]: np.random.randint(4,10,size=6)
Out[9]: array([7, 9, 7, 8, 6, 9])
np.random.randint(4,10,size=(2,2),dtype='int32')
Out[10]:
array([[7, 4],
[6, 9]])
5、np.random.random_integers的用法
random_integers(low, high=None, size=None)
和randint的用法較為相似,區(qū)別在于[low,high]
的右邊界能夠取到,且改函數(shù)即將被拋棄,可以使用
np.random.randint(low,high+1)進(jìn)行代替
總結(jié):隨機(jī)數(shù)可以分為兩大類,一類是浮點(diǎn)型的,常以np.random.uniform為代表,np.random.rand,np.random.radnom和np.random.random_simple可以看作是np.random.uniform的特例;另一類是整數(shù)型的,以np.random.randint為代表,也有np.random.random_integers 但是后者將被前者取代
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- NumPy隨機(jī)數(shù)據(jù)分布與Seaborn可視化詳解
- NumPy數(shù)組排序、過(guò)濾與隨機(jī)數(shù)生成詳解
- Numpy創(chuàng)建數(shù)組和隨機(jī)數(shù)組的方法小結(jié)
- numpy中幾種隨機(jī)數(shù)生成函數(shù)的用法
- Python NumPy中的隨機(jī)數(shù)及ufuncs函數(shù)使用示例詳解
- numpy 產(chǎn)生隨機(jī)數(shù)的幾種方法
- numpy中生成隨機(jī)數(shù)的幾種常用函數(shù)(小結(jié))
- python numpy之np.random的隨機(jī)數(shù)函數(shù)使用介紹
- Python使用numpy產(chǎn)生正態(tài)分布隨機(jī)數(shù)的向量或矩陣操作示例
- NumPy隨機(jī)數(shù)生成函數(shù)的多種實(shí)現(xiàn)方法
相關(guān)文章
使用Python中的greenlet包實(shí)現(xiàn)并發(fā)編程的入門教程
這篇文章主要介紹了使用Python中的greenlet包實(shí)現(xiàn)并發(fā)編程的入門教程,Python由于GIL的存在并不能實(shí)現(xiàn)真正的多線程并發(fā),greenlet可以做到一個(gè)相對(duì)的替換方案,需要的朋友可以參考下2015-04-04
Python網(wǎng)絡(luò)請(qǐng)求之Requests庫(kù)的高級(jí)功能運(yùn)用
在這篇文章中我們將進(jìn)一步深入學(xué)習(xí)Requests庫(kù)的高級(jí)功能,包括處理重定向,設(shè)置超時(shí),處理大文件以及錯(cuò)誤和異常處理,需要的朋友可以參考下2023-08-08
Python中判斷當(dāng)前操作系統(tǒng)的幾種方法
這篇文章主要為大家詳細(xì)介紹了Python中判斷當(dāng)前操作系統(tǒng)的三種方法,主要是os.name,sys.platform和platform.system(),下面小編就來(lái)和大家詳細(xì)講講具體操作吧2025-05-05
Python使用itchat模塊實(shí)現(xiàn)群聊轉(zhuǎn)發(fā),自動(dòng)回復(fù)功能示例
這篇文章主要介紹了Python使用itchat模塊實(shí)現(xiàn)群聊轉(zhuǎn)發(fā),自動(dòng)回復(fù)功能,結(jié)合實(shí)例形式分析了Python基于itchat模塊針對(duì)微信信息的發(fā)送、回復(fù)等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
numpy使用技巧之?dāng)?shù)組過(guò)濾實(shí)例代碼
這篇文章主要介紹了numpy使用技巧之?dāng)?shù)組過(guò)濾實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Python 內(nèi)置函數(shù)memoryview(obj)的具體用法
本篇文章主要介紹了Python 內(nèi)置函數(shù)memoryview(obj)的具體用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
scipy.interpolate插值方法實(shí)例講解
這篇文章主要介紹了scipy.interpolate插值方法介紹,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
淺談python中常用的8種經(jīng)典數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了python中常用的8種經(jīng)典數(shù)據(jù)結(jié)構(gòu),包括原生數(shù)據(jù)結(jié)構(gòu),NumPy包中的數(shù)據(jù)結(jié)構(gòu),以及Pandas包中的數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2023-03-03

