Numpy Vectorization of sliding-window operation

佐手、 提交于 2019-12-05 09:49:54

We can use NumPy broadcasting to create those sliding windowed indices in a vectorized manner. Then, we can simply index into arr_2 with those to create a 3D array and perform element-wise multiplication with 2D array arr_1, which in turn will bring on broadcasting again.

So, we would have a vectorized implementation like so -

W = arr_1.shape[0] # Window size
idx = np.arange(arr_2.shape[0]-W+1)[:,None] + np.arange(W)
out = arr_1*arr_2[idx]

Runtime test and verify results -

In [143]: # Input arrays
     ...: arr_1 = np.random.rand(3,2)
     ...: arr_2 = np.random.rand(10000,2)
     ...: 
     ...: def org_app(arr_1,arr_2):
     ...:     W = arr_1.shape[0] # Window size
     ...:     L = arr_2.shape[0]-W+1
     ...:     out = np.empty((L,W,arr_1.shape[1]))
     ...:     for i in range(L):
     ...:        out[i] = np.multiply(arr_1,arr_2[i:i+W,:])
     ...:     return out
     ...: 
     ...: def vectorized_app(arr_1,arr_2):
     ...:     W = arr_1.shape[0] # Window size
     ...:     idx = np.arange(arr_2.shape[0]-W+1)[:,None] + np.arange(W)
     ...:     return arr_1*arr_2[idx]
     ...: 

In [144]: np.allclose(org_app(arr_1,arr_2),vectorized_app(arr_1,arr_2))
Out[144]: True

In [145]: %timeit org_app(arr_1,arr_2)
10 loops, best of 3: 47.3 ms per loop

In [146]: %timeit vectorized_app(arr_1,arr_2)
1000 loops, best of 3: 1.21 ms per loop
hpaulj

This is a nice case to test the speed of as_strided and Divakar's broadcasting.

In [281]: %%timeit 
     ...: out=np.empty((L,W,arr1.shape[1]))
     ...: for i in range(L):
     ...:    out[i]=np.multiply(arr1,arr2[i:i+W,:])
     ...: 
10 loops, best of 3: 48.9 ms per loop
In [282]: %%timeit
     ...: idx=np.arange(L)[:,None]+np.arange(W)
     ...: out=arr1*arr2[idx]
     ...: 
100 loops, best of 3: 2.18 ms per loop
In [283]: %%timeit
     ...: arr3=as_strided(arr2, shape=(L,W,2), strides=(16,16,8))
     ...: out=arr1*arr3
     ...: 
1000 loops, best of 3: 805 µs per loop

Create Numpy array without enumerating array for more of a comparison of these methods.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!