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

系统 10143 0

处理数据时经常需要从数组中随机抽取元素,这时候就需要用到np.random.choice()。然而choice用法的官方解释并不详细,尤其是对replace参数的解释,例子也不是很全面。因此经过反复实验,我较为详细的总结出了他的用法,并给出了较为详细的使用代码例子。

官方解释 :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.

            
          

下面是我自己的总结

            
              #numpy.random.choice(a, size=None, replace=True, p=None)
#从a(只要是ndarray都可以,但必须是一维的)中随机抽取数字,并组成指定大小(size)的数组
#replace:True表示可以取相同数字,False表示不可以取相同数字
#数组p:与数组a相对应,表示取数组a中每个元素的概率,默认为选取每个元素的概率相同。

            
          

除了numpy中的数组,python内建的list(列表)、tuple(元组)也可以使用。

详解及代码举例

- 产生随机数

            
              >>>np.random.choice(5)#从[0, 5)中随机输出一个随机数
#相当于np.random.randint(0, 5)
	2

>>>np.random.choice(5, 3)#在[0, 5)内输出五个数字并组成一维数组(ndarray)
#相当于np.random.randint(0, 5, 3)
	array([1, 4, 1])

            
          
  • 从数组、列表或元组中随机抽取

注意:不管是什么,它必须是 一维 的!

            
              L = [1, 2, 3, 4, 5]#list列表
T = (2, 4, 6, 2)#tuple元组
A = np.array([4, 2, 1])#numpy,array数组,必须是一维的
A0 = np.arange(10).reshape(2, 5)#二维数组会报错

>>>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)#如果是二维数组,会报错
	ValueError: 'a' must be 1-dimensional

            
          
  • 参数replace
    用来设置是否可以取相同元素:
    True表示可以取相同数字;
    False表示不可以取相同数字。
    默认是True
            
              np.random.choice(5, 6, replace=True)#可以看到有相同元素
	array([3, 4, 1, 1, 0, 3])
np.random.choice(5, 6, replace=False)#会报错,因为五个数字中取六个,不可能不取到重复的数字
	ValueError: Cannot take a larger sample than population when 'replace=False'

            
          
  • 参数p

p实际是个数组,大小(size)应该与指定的a相同,用来规定选取a中每个元素的概率,默认为概率相同

            
              >>> 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’被选取的概率明显比其他几个高很多

            
          

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论