You can use rolling_window from this.
def rolling_window(a, window): shape = a.shape[:-1] + (a.shape[-1] - window + 1, window) strides = a.strides + (a.strides[-1],) return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides) In [37]: a = np.array([1,2,3,4,5,6,7,8]) In [38]: rolling_window(a, 5) Out[38]: array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8]])
I liked the @Divkar solution. However, for large arrays and windows, can you use rolling_window ?
In [55]: a = np.arange(1000) In [56]: %timeit rolling_window(a, 5) 100000 loops, best of 3: 9.02 µs per loop In [57]: %timeit broadcast_f(a, 5) 10000 loops, best of 3: 87.7 µs per loop In [58]: %timeit rolling_window(a, 100) 100000 loops, best of 3: 8.93 µs per loop In [59]: %timeit broadcast_f(a, 100) 1000 loops, best of 3: 1.04 ms per loop