我想生成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)
如果迭代变得非常大,则整个采样将非常耗时。有什么方法可以加快采样速度?
答案 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
这在我的计算机上速度更快
感兴趣