为了更快地进行一阶分析,我需要对一组数据进行采样。如果我想一次检查一个点,我可以通过切片来完成:
import numpy as np
array = np.arange(0,20000)
samplespace = 5000
sampledarray = array[::samplespace]
但是,我需要分析几个元素。我能想到的唯一方法是使用for循环:
import numpy as np
array = np.arange(0,20000)
samplespace = 5000
n = 3
sampledarray = array[0::samplespace]
for i in range(1,n):
arraysample_i = array[i::samplespace]
indices = np.linspace(i,len(sampledarray),len(arraysample_i)).astype(int)
sampledarray = np.insert(sampledarray,indices,arraysample_i)
print(sampledarray)
>>> [ 0 1 2 5000 5001 5002 10000 10001 10002 15000 15001 15002 ]
我担心如果我使用大数组并在多个维度中进行采样,那么循环运行会花费很多时间。有没有更简单、更快的方法
输出:
输出:
感谢Amird建议使用itertools;这是一个我以前没用过的软件包,但最终给了我所需要的
下面是我最后做的:
这使我能够相当轻松地将其扩展到更多维度,而不是使用我的大型数据集,我只需操作索引数组:
相关问题 更多 >
编程相关推荐