我想从同一个1D numpy数组中提取多个切片,其中切片索引是从随机分布中提取的。基本上,我想实现以下目标:
import numpy as np
import numpy.random
# generate some 1D data
data = np.random.randn(500)
# window size (slices are 2*winsize long)
winsize = 60
# number of slices to take from the data
inds_size = (100, 200)
# get random integers that function as indices into the data
inds = np.random.randint(low=winsize, high=len(data)-winsize, size=inds_size)
# now I want to extract slices of data, running from inds[0,0]-60 to inds[0,0]+60
sliced_data = np.zeros( (winsize*2,) + inds_size )
for k in range(inds_size[0]):
for l in range(inds_size[1]):
sliced_data[:,k,l] = data[inds[k,l]-winsize:inds[k,l]+winsize]
# sliced_data.shape is now (120, 100, 200)
上面的嵌套循环工作正常,但速度很慢。在我的真实代码中,我需要做数千次,因为数据阵列要比这些大得多。有没有办法更有效地做到这一点?
请注意inds
在我的情况下总是2D,但在获得切片之后,我将总是在这两个维度中的一个上求和,因此只在一维上累积总和的方法会很好
我发现this question和this answer似乎几乎相同。然而,问题只是关于1D索引向量(与我的2D相反)。此外,答案缺少一些上下文,因为我不太明白建议的as_strided
是如何工作的。由于我的问题似乎并不少见,我想我会再次提出问题,希望得到更具解释性的答案,而不仅仅是代码。
答案 0 :(得分:6)
以这种方式使用as_strided
似乎比Divakar的方法(20 ms vs 35 ms)更快,尽管内存使用可能是一个问题。
data_wins = as_strided(data, shape=(data.size - 2*winsize + 1, 2*winsize), strides=(8, 8))
inds = np.random.randint(low=0, high=data.size - 2*winsize, size=inds_size)
sliced = data_wins[inds]
sliced = sliced.transpose((2, 0, 1)) # to use the same index order as before
Strides是每个维度中索引的步骤(以字节为单位)。例如,使用形状(x, y, z)
的数组和大小为d
的数据类型(float64为8),步幅通常为(y*z*d, z*d, d)
,以便第二个索引跨越整行of z 项目。将这两个值设置为8,data_wins[i, j]
和data_wins[j, i]
将指向相同的内存位置。
>>> import numpy as np
>>> from numpy.lib.stride_tricks import as_strided
>>> a = np.arange(10, dtype=np.int8)
>>> as_strided(a, shape=(3, 10 - 2), strides=(1, 1))
array([[0, 1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7, 8],
[2, 3, 4, 5, 6, 7, 8, 9]], dtype=int8)
答案 1 :(得分:2)
这是使用broadcasting
-
# Get 3D offsetting array and add to inds for all indices
allinds = inds + np.arange(-60,60)[:,None,None]
# Index into data with all indices for desired output
sliced_dataout = data[allinds]
运行时测试 -
In [20]: # generate some 1D data
...: data = np.random.randn(500)
...:
...: # window size (slices are 2*winsize long)
...: winsize = 60
...:
...: # number of slices to take from the data
...: inds_size = (100, 200)
...:
...: # get random integers that function as indices into the data
...: inds=np.random.randint(low=winsize,high=len(data)-winsize, size=inds_size)
...:
In [21]: %%timeit
...: sliced_data = np.zeros( (winsize*2,) + inds_size )
...: for k in range(inds_size[0]):
...: for l in range(inds_size[1]):
...: sliced_data[:,k,l] = data[inds[k,l]-winsize:inds[k,l]+winsize]
...:
10 loops, best of 3: 66.9 ms per loop
In [22]: %%timeit
...: allinds = inds + np.arange(-60,60)[:,None,None]
...: sliced_dataout = data[allinds]
...:
10 loops, best of 3: 24.1 ms per loop
内存消耗:妥协解决方案
如果内存消耗是一个问题,这里是一个折衷的解决方案,有一个循环 -
sliced_dataout = np.zeros( (winsize*2,) + inds_size )
for k in range(sliced_data.shape[0]):
sliced_dataout[k] = data[inds-winsize+k]