What does replacement mean in numpy.random.choice?

谁都会走 提交于 2019-11-27 11:04:48

问题


Here explains the function numpy.random.choice. However, I am confused about the third parameter replace. What is it? And in which case will it be useful? Thanks!


回答1:


It controls whether the sample is returned to the sample pool. If you want only unique samples then this should be false.




回答2:


You can use it when you want sample some elements from a list, and meanwhile you want the elements no repeat, then you can set the "replace=False".
eg.

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


来源:https://stackoverflow.com/questions/40689152/what-does-replacement-mean-in-numpy-random-choice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!