日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python,numpy中np.random.choice()的用法詳解及其

系統(tǒng) 11426 0

處理數(shù)據(jù)時經(jīng)常需要從數(shù)組中隨機抽取元素,這時候就需要用到np.random.choice()。然而choice用法的官方解釋并不詳細(xì),尤其是對replace參數(shù)的解釋,例子也不是很全面。因此經(jīng)過反復(fù)實驗,我較為詳細(xì)的總結(jié)出了他的用法,并給出了較為詳細(xì)的使用代碼例子。

官方解釋 :https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html

            
              官方解釋:
numpy.random.choice(a, size=None, replace=True, p=None)
Generates a random sample from a given 1-D array

New in version 1.7.0.

Parameters:	
a : 1-D array-like or int
If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a were np.arange(a)

size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

replace : boolean, optional
Whether the sample is with or without replacement

p : 1-D array-like, optional
The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.

            
          

下面是我自己的總結(jié)

            
              #numpy.random.choice(a, size=None, replace=True, p=None)
#從a(只要是ndarray都可以,但必須是一維的)中隨機抽取數(shù)字,并組成指定大小(size)的數(shù)組
#replace:True表示可以取相同數(shù)字,F(xiàn)alse表示不可以取相同數(shù)字
#數(shù)組p:與數(shù)組a相對應(yīng),表示取數(shù)組a中每個元素的概率,默認(rèn)為選取每個元素的概率相同。

            
          

除了numpy中的數(shù)組,python內(nèi)建的list(列表)、tuple(元組)也可以使用。

詳解及代碼舉例

- 產(chǎn)生隨機數(shù)

            
              >>>np.random.choice(5)#從[0, 5)中隨機輸出一個隨機數(shù)
#相當(dāng)于np.random.randint(0, 5)
	2

>>>np.random.choice(5, 3)#在[0, 5)內(nèi)輸出五個數(shù)字并組成一維數(shù)組(ndarray)
#相當(dāng)于np.random.randint(0, 5, 3)
	array([1, 4, 1])

            
          
  • 從數(shù)組、列表或元組中隨機抽取

注意:不管是什么,它必須是 一維 的!

            
              L = [1, 2, 3, 4, 5]#list列表
T = (2, 4, 6, 2)#tuple元組
A = np.array([4, 2, 1])#numpy,array數(shù)組,必須是一維的
A0 = np.arange(10).reshape(2, 5)#二維數(shù)組會報錯

>>>np.random.choice(L, 5)
	array([3, 5, 2, 1, 5])
	
>>>np.random.choice(T, 5)
	array([2, 2, 2, 4, 2])
 
>>>np.random.choice(A, 5)
	array([1, 4, 2, 2, 1])

>>>np.random.choice(A0, 5)#如果是二維數(shù)組,會報錯
	ValueError: 'a' must be 1-dimensional

            
          
  • 參數(shù)replace
    用來設(shè)置是否可以取相同元素:
    True表示可以取相同數(shù)字;
    False表示不可以取相同數(shù)字。
    默認(rèn)是True
            
              np.random.choice(5, 6, replace=True)#可以看到有相同元素
	array([3, 4, 1, 1, 0, 3])
np.random.choice(5, 6, replace=False)#會報錯,因為五個數(shù)字中取六個,不可能不取到重復(fù)的數(shù)字
	ValueError: Cannot take a larger sample than population when 'replace=False'

            
          
  • 參數(shù)p

p實際是個數(shù)組,大小(size)應(yīng)該與指定的a相同,用來規(guī)定選取a中每個元素的概率,默認(rèn)為概率相同

            
              >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
	array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], dtype='|S11')
#可以看到,‘pooh’被選取的概率明顯比其他幾個高很多

            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 沙田区| 北流市| 五寨县| 瓮安县| 昆山市| 柯坪县| 渭源县| 华蓥市| 龙泉市| 花莲市| 宜章县| 扎赉特旗| 勐海县| 武宁县| 新宾| 佳木斯市| 重庆市| 清水河县| 巴塘县| 中山市| 修文县| 禄劝| 长宁区| 七台河市| 玉山县| 大足县| 宁明县| 柳州市| 巩义市| 新宁县| 罗田县| 洞头县| 长岭县| 图木舒克市| 高陵县| 广水市| 无锡市| 益阳市| 屏山县| 武川县| 晴隆县|