[numpy]random.choice()随机选取内容

本文详细介绍了numpy库中的random.choice函数的使用方法,包括参数解析、示例代码及官方文档链接。该函数可用于从指定数组中随机抽取元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读代码看到这句

    np.random.choice([i for i in images[:200] if not is_empty(i)], size = 8)

https://www.kaggle.com/rackovic1994/convolutional-neural-network

概述:

可以从一个int数字或1array里随机选取内容,并将选取结果放入n维array中返回。

   
  • 1
  • 2

说明:

numpy.random.choice(a, size=None, replace=True, p=None)

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 was np.arange(n)

size : int or tuple of ints, optional 

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.

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

示例

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

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

官方介绍

http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html

        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>

感谢 https://blog.youkuaiyun.com/autoliuweijie/article/details/51982514

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值