numpy中生成隨機(jī)數(shù)的幾種常用函數(shù)(小結(jié))
1、使用numpy生成隨機(jī)數(shù)的幾種方式

1)生成指定形狀的0-1之間的隨機(jī)數(shù):np.random.random()和np.random.rand()
array1 = np.random.random((3)) display(array1) # ----------------------------------- array2 = np.random.random((3,4)) display(array2) # ----------------------------------- array3 = np.random.rand(3) display(array3) # ----------------------------------- array4 = np.random.rand(2,3) display(array4)
① 操作如下


② 區(qū)別如下

2)生成指定數(shù)值范圍內(nèi)的隨機(jī)整數(shù):np.random.randint()

① 操作如下
array9 = np.random.randint(low=1, high=10, size=6, dtype=np.int32) display(array9) # --------------------------------------------------------- array10 = np.random.randint(low=1, high=10, size=(2,3), dtype=np.int64) display(array10) # --------------------------------------------------------- array11 = np.random.randint(low=1, high=10, size=(2,3,4), dtype=np.int32) display(array11)
② 結(jié)果如下

3)與正態(tài)分布有關(guān)的幾個(gè)隨機(jī)函數(shù):np.random.randn()和np.random.normal()
- np.random.randn 生成服從均值為0,標(biāo)準(zhǔn)差為1的標(biāo)準(zhǔn)正態(tài)分布隨機(jī)數(shù);
- np.random.normal 生成指定均值和標(biāo)準(zhǔn)差的正態(tài)分布隨機(jī)數(shù);
array5 = np.random.randn(3) display(array5) # --------------------------------------------- array6 = np.random.randn(2,3) display(array6) # --------------------------------------------- array7 = np.random.normal(loc=2,scale=0.5,size=6) display(array7) # --------------------------------------------- array8 = np.random.normal(loc=2,scale=0.5,size=6).reshape(2,3) display(array8)
① 結(jié)果如下

② 區(qū)別如下

4)均勻分布隨機(jī)函數(shù):np.random.uniform()
用法:生成指定范圍內(nèi)的服從均勻分布的隨機(jī)數(shù);
array11 = np.random.uniform(1,10,5) display(array11) # --------------------------------- array12 = np.random.uniform(1,10,(2,3)) display(array12)
① 結(jié)果如下

5)np.random.seed():按照種子來生成隨機(jī)數(shù),種子一樣,則生成的隨機(jī)數(shù)結(jié)果必一致

① 操作如下
np.random.seed(3) a = np.random.rand(3) display(a) np.random.seed(3) b = np.random.rand(3) display(b) # -------------------------- np.random.seed() a = np.random.rand(3) display(a) np.random.seed() b = np.random.rand(3) display(b)
② 結(jié)果如下

6)np.random.shuffle():打亂數(shù)組元素順序(原地操作數(shù)組)
c = np.arange(10) display(c) np.random.shuffle(c) display(c)
① 結(jié)果如下

7)np.random.choice():按照指定概率從指定數(shù)組中,生成隨機(jī)數(shù);
① np.random.choice()函數(shù)的用法說明
d = np.random.choice([1,2,3,4], p=[0.1, 0.2, 0.3, 0.4]) display(d)
說明:上述函數(shù)第一個(gè)參數(shù)表示的是數(shù)組,第二個(gè)參數(shù)表示的是概率值。上述函數(shù)的含義是當(dāng)進(jìn)行n多次重復(fù)實(shí)驗(yàn)的時(shí)候,抽取1的概率為0.1,抽取2的概率為0.2,抽取3的概率為0.3,抽取4的概率為0.4。
② 結(jié)果如下

③ 隨即進(jìn)行10000次重復(fù)實(shí)驗(yàn),檢測(cè)每一個(gè)數(shù),被抽取到的概率
list1 = [0,0,0,0] for i in range(100000): f = np.random.choice([1,2,3,4], p=[0.1, 0.2, 0.3, 0.4]) list1[f-1] = list1[f-1] + 1 display(list1) result_list = [value/sum(list1) for value in list1] display(result_list)
④ 結(jié)果如下

⑤ 模擬進(jìn)行100000次擲硬幣重復(fù)實(shí)驗(yàn),檢測(cè)每一面,被抽取到的概率
list1 = [0,0] for i in range(100000): f = np.random.choice([0,1], p=[0.5,0.5]) list1[f] = list1[f] + 1 display(list1) result_list = [value/sum(list1) for value in list1] display(result_list)
⑥ 結(jié)果如下

到此這篇關(guān)于numpy中生成隨機(jī)數(shù)的幾種常用函數(shù)(小結(jié))的文章就介紹到這了,更多相關(guān)numpy 生成隨機(jī)數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- NumPy隨機(jī)數(shù)據(jù)分布與Seaborn可視化詳解
- NumPy數(shù)組排序、過濾與隨機(jī)數(shù)生成詳解
- Numpy創(chuàng)建數(shù)組和隨機(jī)數(shù)組的方法小結(jié)
- numpy中幾種隨機(jī)數(shù)生成函數(shù)的用法
- Python NumPy中的隨機(jī)數(shù)及ufuncs函數(shù)使用示例詳解
- numpy 產(chǎn)生隨機(jī)數(shù)的幾種方法
- python numpy之np.random的隨機(jī)數(shù)函數(shù)使用介紹
- python numpy 常用隨機(jī)數(shù)的產(chǎn)生方法的實(shí)現(xiàn)
- Python使用numpy產(chǎn)生正態(tài)分布隨機(jī)數(shù)的向量或矩陣操作示例
- NumPy隨機(jī)數(shù)生成函數(shù)的多種實(shí)現(xiàn)方法
相關(guān)文章
Python中aiohttp模塊的簡(jiǎn)單運(yùn)用方式
這篇文章主要介紹了Python中aiohttp模塊的簡(jiǎn)單運(yùn)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
pytorch 實(shí)現(xiàn)變分自動(dòng)編碼器的操作
這篇文章主要介紹了pytorch 實(shí)現(xiàn)變分自動(dòng)編碼器的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python實(shí)現(xiàn)語音常用度量方法的代碼詳解
由于語音信號(hào)是一種緩慢變化的短時(shí)平穩(wěn)信號(hào),因而在不同時(shí)間段上的信噪比也應(yīng)不一樣。為了改善上面的問題,可以采用分段信噪比。接下來通過本文給大家介紹python實(shí)現(xiàn)語音常用度量方法,感興趣的朋友跟隨小編一起看看吧2021-05-05
python使用itchat實(shí)現(xiàn)手機(jī)控制電腦
這篇文章主要為大家詳細(xì)介紹了python使用itchat實(shí)現(xiàn)手機(jī)控制電腦,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
使用Python實(shí)現(xiàn)一個(gè)強(qiáng)大的文件系統(tǒng)結(jié)構(gòu)創(chuàng)建器
這篇文章主要為大家詳細(xì)介紹了一個(gè)基于?wxPython?的文件系統(tǒng)結(jié)構(gòu)創(chuàng)建器程序,展示如何通過?CustomTreeCtrl?組件實(shí)現(xiàn)文件夾和文件的可視化管理,感興趣的可以了解下2025-05-05
Matplotlib可視化之使用pandas和Seaborn繪制精美圖表詳解
這篇文章主要為大家詳細(xì)介紹了Matplotlib可視化之使用pandas和Seaborn繪制精美圖表的相關(guān)內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-12-12
python中使用zip函數(shù)出現(xiàn)<zip object at 0x02A9E418>錯(cuò)誤的原因
這篇文章主要介紹了python中使用zip函數(shù)出現(xiàn)<zip object at 0x02A9E418>錯(cuò)誤的原因分析及解決方法,需要的朋友可以參考下2018-09-09

