numpy.random.choice 中replace 的含义

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

用人话简单解释一下:

一维 数组 a 中选出 size 个元素,a中每个元素被选中的概率由一维数组p定义。

replace是什么鬼?(每个官方解释单词都能看懂,就是不懂在说什么。。)
查了一下,原文见stackoverflow.

简单解释一下:

replace=True: 可以从a 中反复选取同一个元素。
replace=False: a 中同一个元素只能被选取一次。

搬运栗子

from numpy import random as rd

ary = list(range(10))
# usage
In[18]: rd.choice(ary, size=8, replace=False)
Out[18]: array([0, 5, 9, 8, 2, 1, 6, 3])  # no repeated elements
In[19]: rd.choice(ary, size=8, replace=True)
Out[19]: array([4, 9, 8, 5, 4, 1, 1, 9])  # elements may be repeated
  • 17
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
numpy.random.choice是一个用于从给定的一维数组随机抽取元素的函数。它可以根据指定的参数生成一个随机样本。参数a可以是一个一维数组,也可以是一个整数,如果是一个数组,那么从数组的元素随机抽取样本,如果是一个整数,那么相当于从np.arange(a)随机抽取样本。参数size用于指定输出的形状,如果不指定,默认返回一个单个值。参数replace用于指定是否可以重复抽取相同的元素,如果replace=True,表示可以重复抽取,如果replace=False,表示不可以重复抽取。参数p用于指定每个元素被抽取的概率,默认情况下,每个元素的概率相同。\[1\] 举个例子,如果我们使用np.random.choice(5, 3, replace=False),表示从大小为5的np.arange(5)生成一个均匀的随机样本,没有替换(重复),结果可能是array(\[3,1,0\])。这个结果等价于np.random.permutation(np.arange(5))\[:3\]。\[2\] 另外,如果我们使用np.random.choice(5, 3, replace=False, p=\[0.1, 0, 0.3, 0.6, 0\]),表示从大小为5的np.arange(5)生成一个非均匀的随机样本,没有替换(重复),其每个元素被抽取的概率分别为\[0.1, 0, 0.3, 0.6, 0\],结果可能是array(\[2, 3, 0\])。\[3\] #### 引用[.reference_title] - *1* [numpynp.random.choice()的用法详解及其参考代码](https://blog.csdn.net/woxiangxinwang/article/details/123175674)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [numpyrandom.choice()函数](https://blog.csdn.net/qq_25436597/article/details/79815800)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值