numpy.random.choice 中的替换是什么意思?

What does replacement mean in numpy.random.choice?

Here 解释函数 numpy.random.choice。但是,我对第三个参数replace感到困惑。它是什么?在什么情况下它会有用?谢谢!

控制样本是否返回样本池。如果您只想要独特的样本,那么这应该是错误的。

当你想从一个列表中采样一些元素,同时又想让元素不重复时,你可以使用它,然后你可以设置“replace =假".
例如

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