我的问题是:如何在numpy中生成非重复的随机数?
list = np.random.random_integers(20,size=(10))
Run Code Online (Sandbox Code Playgroud) I need a way to sample without replacement a certain array a
. I tried two approaches (see MCVE below), using random.sample()
and np.random.choice
.
I assumed the numpy
function would be faster, but it turns out it is not. In my tests random.sample
is ~15% faster than np.random.choice
.
Is this correct, or am I doing something wrong in my example below? If this is correct, why?
import numpy as np
import random
import time
from contextlib import contextmanager …
Run Code Online (Sandbox Code Playgroud) 如果我评估如下:
numpy.random.choice(2, size=100000, p=[0.01, 0.99])
使用一个均匀分布的随机数float
,例如r
,并决定是否r < 0.01
会浪费许多生成的随机位(熵)。我听说(二手)生成伪随机数的计算成本很高,所以我认为numpy
不会这样做,而是会在这种情况下使用算术编码之类的方案。
然而,乍一看似乎确实为它所要求的每个样本choice
生成了一个。float
此外,快速timeit
实验表明,生成均匀浮点数实际上比 中的样本n
更快。n
p=[0.01, 0.99]
>>> timeit.timeit(lambda : numpy.random.choice(2, size=100000, p=[0.01, 0.99]), number=1000)
1.74494537999999
>>> timeit.timeit(lambda : numpy.random.random(size=100000), number=1000)
0.8165735180009506
Run Code Online (Sandbox Code Playgroud)
真的会像看起来那样为每个样本choice
生成一个吗?在某些情况下(特别是当数据很大且分布不均匀时),float
使用某种压缩算法不会显着提高性能吗?如果没有,为什么不呢?size
p