處理數(shù)據(jù)時(shí)經(jīng)常需要從數(shù)組中隨機(jī)抽取元素,這時(shí)候就需要用到np.random.choice()。然而choice用法的官方解釋并不詳細(xì),尤其是對(duì)replace參數(shù)的解釋,例子也不是很全面。因此經(jīng)過(guò)反復(fù)實(shí)驗(yàn),我較為詳細(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都可以,但必須是一維的)中隨機(jī)抽取數(shù)字,并組成指定大小(size)的數(shù)組
#replace:True表示可以取相同數(shù)字,F(xiàn)alse表示不可以取相同數(shù)字
#數(shù)組p:與數(shù)組a相對(duì)應(yīng),表示取數(shù)組a中每個(gè)元素的概率,默認(rèn)為選取每個(gè)元素的概率相同。
除了numpy中的數(shù)組,python內(nèi)建的list(列表)、tuple(元組)也可以使用。
詳解及代碼舉例
- 產(chǎn)生隨機(jī)數(shù)
>>>np.random.choice(5)#從[0, 5)中隨機(jī)輸出一個(gè)隨機(jī)數(shù)
#相當(dāng)于np.random.randint(0, 5)
2
>>>np.random.choice(5, 3)#在[0, 5)內(nèi)輸出五個(gè)數(shù)字并組成一維數(shù)組(ndarray)
#相當(dāng)于np.random.randint(0, 5, 3)
array([1, 4, 1])
- 從數(shù)組、列表或元組中隨機(jī)抽取
注意:不管是什么,它必須是 一維 的!
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ù)組會(huì)報(bào)錯(cuò)
>>>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ù)組,會(huì)報(bào)錯(cuò)
ValueError: 'a' must be 1-dimensional
-
參數(shù)replace
用來(lái)設(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)#會(huì)報(bào)錯(cuò),因?yàn)槲鍌€(gè)數(shù)字中取六個(gè),不可能不取到重復(fù)的數(shù)字
ValueError: Cannot take a larger sample than population when 'replace=False'
- 參數(shù)p
p實(shí)際是個(gè)數(shù)組,大小(size)應(yīng)該與指定的a相同,用來(lái)規(guī)定選取a中每個(gè)元素的概率,默認(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’被選取的概率明顯比其他幾個(gè)高很多
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
