numpy中np.random.choice()的用法,你清楚了吗

处理数据时经常需要从数组中随机抽取元素,这时候就需要用到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中每个元素的概率,默认为概率相同

      οnclick="mdcp.copyCode(event)" style="position: unset;">>>> 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’被选取的概率明显比其他几个高很多
      
    • 1
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值