np.random.choice

np.random.choice

>>> N =21
>>> indices = np.random.choice(N, 1, replace=False)
>>> indices
array([5])
>>> indices = np.random.choice(N, 1, replace=False)
>>> indices
array([17])
>>> indices = np.random.choice(N)
>>> indices
7


第一个参数表示从N中选择,第二个参数表示选几个default是1个返回数字,,replace具体用途还不知道,p表示概率

>>> np.random.choice(5, 3)
array([0, 3, 4])

>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])

>>> np.random.choice(5, 3, replace=False)
array([3,1,0])

>>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0])

>>> 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'],