numpy.lib.stride_tricks.as_strided 再次击中(缩写双关语)!
说到花哨的索引技巧,臭名昭著的 - np.lib.stride_tricks.as_strided。想法/技巧是从第一列开始到倒数第二列得到一个切片部分,并在最后连接。这确保我们可以根据需要向前迈进,以利用np.lib.stride_tricks.as_strided,从而避免实际回滚的需要。这就是全部的想法!
现在,就实际实现而言,我们将使用scikit-image's view_as_windows 来优雅地使用np.lib.stride_tricks.as_strided。因此,最终的实现将是 -
from skimage.util.shape import view_as_windows as viewW
def strided_indexing_roll(a, r):
# Concatenate with sliced to cover all rolls
a_ext = np.concatenate((a,a[:,:-1]),axis=1)
# Get sliding windows; use advanced-indexing to select appropriate ones
n = a.shape[1]
return viewW(a_ext,(1,n))[np.arange(len(r)), (n-r)%n,0]
这是一个示例运行 -
In [327]: A = np.array([[4, 0, 0],
...: [1, 2, 3],
...: [0, 0, 5]])
In [328]: r = np.array([2, 0, -1])
In [329]: strided_indexing_roll(A, r)
Out[329]:
array([[0, 0, 4],
[1, 2, 3],
[0, 5, 0]])
基准测试
# @seberg's solution
def advindexing_roll(A, r):
rows, column_indices = np.ogrid[:A.shape[0], :A.shape[1]]
r[r < 0] += A.shape[1]
column_indices = column_indices - r[:,np.newaxis]
return A[rows, column_indices]
让我们对具有大量行和列的数组进行一些基准测试 -
In [324]: np.random.seed(0)
...: a = np.random.rand(10000,1000)
...: r = np.random.randint(-1000,1000,(10000))
# @seberg's solution
In [325]: %timeit advindexing_roll(a, r)
10 loops, best of 3: 71.3 ms per loop
# Solution from this post
In [326]: %timeit strided_indexing_roll(a, r)
10 loops, best of 3: 44 ms per loop