使用np.random.choice()生成样本的快速方法?

时间:2019-04-27 12:48:03

标签: python numpy random

我想生成N次而不替换的随机样本,如下所示:

import numpy as np

sample = np.zeros([100000, 4], int)
for i in range(100000):
    sample[i] = np.random.choice(128, 4, replace=False)

如果迭代变得非常大,则整个采样将非常耗时。有什么方法可以加快采样速度?

3 个答案:

答案 0 :(得分:0)

这将为您提供一个随机整数范围(0,128),其形状为(100000,4)

j

答案 1 :(得分:0)

使用random.sample代替np.random.choice

In [16]: import time
    ...: start_time = time.time()
    ...: sample = np.zeros([100000, 4], int)
    ...: for i in range(100000):
    ...:     sample[i] = random.sample(range(128), 4)
    ...: print("--- %s seconds ---" % (time.time() - start_time))
    ...: 
--- 0.7096474170684814 seconds ---

In [17]: import time
    ...: start_time = time.time()
    ...: sample = np.zeros([100000, 4], int)
    ...: for i in range(100000):
    ...:     sample[i] = np.random.choice(128, 4, replace=False)
    ...: print("--- %s seconds ---" % (time.time() - start_time))
    ...: 
--- 5.2036824226379395 seconds ---

答案 2 :(得分:0)

您的方法

In [16]: sample = np.zeros([100000, 4], int)

In [17]: %timeit for i in range(100000):sample[i] = np.random.choice(128, 4, rep
    ...: lace=False)
1 loop, best of 3: 2.5 s per loop

您可以写:

In [149]: %timeit d=np.random.choice(128,100000);sample1=np.array([(d+x)%128 for x in np.random.choice(128,4)])
The slowest run took 4.63 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 4.11 ms per loop

这在我的计算机上速度更快

这可能不太随机,但这取决于您的应用程序。 毕竟for循环在香草python中非常慢。您可能对CythonNumba

感兴趣