日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

numpy.random详解

發(fā)布時間:2025/3/16 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 numpy.random详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

numpy庫中的random模塊中有很多函數(shù),下面,我做一個簡要的歸類、舉例:

1、rand(d0,d1,d2,d3....dn)??

? ? ?創(chuàng)建一個數(shù)組,隨機產(chǎn)生一個[0,-1)的值,參數(shù):整數(shù),可以為空。

1 In [6]: np.random.randn(4,2) 2 Out[6]: 3 array([[-0.17971383, -1.84787176], 4 [-0.1860349 , 0.5546173 ], 5 [-1.57698523, 0.7272754 ], 6 [ 0.45259596, 0.32255619]]) View Code

2、randn(d0,d1,d2,d3....dn)

? ? ?從正態(tài)分布中返回數(shù)組。d0,d1...dn 表示維度

In [15]: np.random.randn(2,3) Out[15]: array([[-0.81662523, 0.01087083, -2.38323491],[-1.67111495, -0.08231917, -0.65780322]])In [17]: 5*np.random.randn(2,4)+6 Out[17]: array([[ 5.24384164, 1.86308445, -2.85359765, 1.13427893],[ 3.70242114, 6.28739043, 7.23047234, 0.36169486]]) View Code

3、numpy.random.randint(low,?high=None,?size=None,?dtype='l')

? ? ?返回隨機的整數(shù),位于半開區(qū)間。[low,high)

? ? low: 整數(shù),且當(dāng)high不為空時,low<high

? ? high:整數(shù)

? ? size: 可以是整數(shù),或者元組。? 默認(rèn)是空值,如果為空,則僅返回一個整數(shù)。

In [39]: np.random.randint(4,size=5) Out[39]: array([0, 2, 3, 2, 0])In [37]: np.random.randint(4,5) Out[37]: 4 View Code 1 In [19]: np.random.randint(2) 2 Out[19]: 1 3 4 In [21]: np.random.randint(2,size=3) 5 Out[21]: array([0, 0, 1]) 6 7 In [24]: np.random.randint(6,size=(4,2)) 8 Out[24]: 9 array([[0, 0], 10 [5, 5], 11 [3, 5], 12 [1, 3]]) View Code

4、numpy.random.random_integers(low,?high=None,?size=None)

? ? ?返回隨機的整數(shù),位于閉區(qū)間。[low,high]

? ? ?low:int

In [25]: np.random.random_integers(6,size=(4,2)) Out[25]: array([[6, 4],[1, 2],[5, 1],[3, 6]]) View Code

5、numpy.random.random_sample(size=None)

? ? 返回隨機的浮點數(shù),在半開區(qū)間[0.0,1.0)

1 In [26]: np.random.random_sample() 2 Out[26]: 0.2688857900293471 3 4 In [26]: np.random.random_sample() 5 Out[26]: 0.2688857900293471 6 7 In [29]: np.random.random_sample((2,4)) 8 Out[29]: 9 array([[0.30813872, 0.99138326, 0.44217795, 0.06136415], 10 [0.19094554, 0.70575706, 0.3388763 , 0.55259917]]) 11 12 In [30]: np.random.random_sample((5,)) 13 Out[30]: array([0.0560845 , 0.30932191, 0.78202398, 0.06572516, 0.57067096]) View Code

6、numpy.random.random(size=None)

? ??返回隨機的浮點數(shù),在半開區(qū)間[0.0,1.0),與random_sample一樣。

7、numpy.random.ranf(size=None)

? ??返回隨機的浮點數(shù),在半開區(qū)間 [0.0, 1.0)。?與random_sample完全一樣

8、numpy.random.choice(a,?size=None,?replace=True,?p=None)

? ? ?通過一個給定的一維數(shù)據(jù),產(chǎn)生隨機采樣

? ? ?a : int ,如果只有一個參數(shù),則返回小于參數(shù)的隨機整數(shù)。

? ? ?size: 是整數(shù) or 元組。

In [31]: np.random.choice(5) Out[31]: 0In [32]: np.random.choice(8) Out[32]: 3In [33]: np.random.choice(7,6) Out[33]: array([2, 4, 6, 6, 6, 5])In [34]: np.random.choice(2,size = 5) Out[34]: array([1, 1, 0, 0, 0])In [35]: np.random.choice(2,size=(2,4)) Out[35]: array([[0, 0, 0, 1],[0, 0, 0, 1]]) View Code

9、numpy.random.bytes(length)

? ? ?隨機返回字節(jié)。

? ? ?length: int

?

?

?

? ??

? ? ?

? ? ?

? ??

? ??

轉(zhuǎn)載于:https://www.cnblogs.com/meganchen/p/10287221.html

總結(jié)

以上是生活随笔為你收集整理的numpy.random详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。