Python中的 numpy.random.choice()
借助choice()方法,我们可以得到一维数组的随机样本,并返回numpy数组的随机样本。
Syntax : numpy.random.choice(a, size=None, replace=True, p=None)
Parameters:
1) a – 1-D array of numpy having random samples.
2) size – Output shape of random samples of numpy array.
3) replace – Whether the sample is with or without replacement.
4) p – The probability attach with every samples in a.
Output : Return the numpy array of random samples.
示例 #1:
在这个例子中我们可以看到,通过使用choice()方法,我们可以得到numpy数组的随机样本,使用这个方法可以生成均匀或非均匀的样本。
Python3
# import choice
import numpy as np
import matplotlib.pyplot as plt
# Using choice() method
gfg = np.random.choice(13, 5000)
count, bins, ignored = plt.hist(gfg, 25, density = True)
plt.show()
Python3
# import choice
import numpy as np
import matplotlib.pyplot as plt
# Using choice() method
gfg = np.random.choice(5, 1000, p =[0.2, 0.1, 0.3, 0.4, 0])
count, bins, ignored = plt.hist(gfg, 14, density = True)
plt.show()
输出 :
示例 #2:
Python3
# import choice
import numpy as np
import matplotlib.pyplot as plt
# Using choice() method
gfg = np.random.choice(5, 1000, p =[0.2, 0.1, 0.3, 0.4, 0])
count, bins, ignored = plt.hist(gfg, 14, density = True)
plt.show()
输出 :