首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

np.random.choice用法中文和样例详解

  • 25-03-03 22:02
  • 3803
  • 5786
blog.csdn.net
官方介绍

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

numpy.random.choice

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

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.

Returns:

samples : 1-D ndarray, shape (size,)

The generated random samples

Raises:

ValueError

If a is an int and less than zero, if a or p are not 1-dimensional, if a is an array-like of size 0, if p is not a vector of probabilities, if a and p have different lengths, or if replace=False and the sample size is greater than the population size


np.random.choice
RandomState.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. 
    如果是ndarray数组,随机样本在该数组获取(取数据元素),    If an int, the random sample is generated as if a was np.arange(n)
如果是整型数据随机样本生成类似np.arange(n)    
size : int or tuple of ints, optional 
    大小:整型或 整型元组中元素个数,可选
replace : boolean, optional
    替换:布尔型,可选    Whether the sample is with or without replacement
    样本是否有重复值(False,没有;True,有;默认:True)
见例子3
p : 1-D array-like, optional
    1维数组,可选
    The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.
和a里的每个输入联系,如果没有该参数,默认 假设 a里的每个输入等概率出现。(和a中元素一一对应,表示该元素出现的概率,概率大的,出现较多)
    见例子2

实例
例子1: np.arange(5) 中产生一个size为3的随机采样:
>>> np.random.choice( 5 , 3 )
array([ 0 , 3 , 4 ])
>>> #This is equivalent to np.random.randint(0,5,3)
例子2:从 np.arange(5) 中产生一个非标准的 size为 3的随机采样:
>>> np.random.choice( 5 , 3 , p = [ 0.1 , 0 , 0.3 , 0.6 , 0 ])
array([ 3 , 3 , 0 ])
说明:3出现概率最大,所以array([3, 3, 0])中,3最多

a:01234
p:0.100.30.60
例子3: np.arange(5) 产生一个标准分布、size为 3、没有重复替换的随机采样:
>>> np.random.choice( 5 , 3 , replace = False )
array([ 3 , 1 , 0 ])
>>> #This is equivalent to np.random.permutation(np.arange(5))[:3]
例子4:也可以这样,不必一定是整型数字:
>>> 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' )
例子5:实际使用中,首先创建一个mask变量,然后通过mask来对需要采样的数据进行采样:
... mask = np.random.choice(split_size, batch_size)
captions = data[ '%s_captions' % split][mask]
image_idxs = data[ '%s_image_idxs' % split][mask]...

参考:
https://oldpan.me/archives/python-numpy-choice
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树科学计算基础软件包NumPyNumPy概述416686 人正在系统学习中
注:本文转载自blog.csdn.net的wyx100的文章"https://blog.csdn.net/wyx100/article/details/80639653"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top