Numpy grouping every N continuous element?

I would like to extract groups from each N continuous elements from an array. For a numpy array like this:

a = numpy.array([1,2,3,4,5,6,7,8]) 

I want to have (N = 5):

 array([[1,2,3,4,5], [2,3,4,5,6], [3,4,5,6,7], [4,5,6,7,8]]) 

so that I can perform additional functions such as average and amount. How to create such an array?

+5
source share
2 answers

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 
+6
source

One approach with broadcasting is

 import numpy as np out = a[np.arange(a.size - N + 1)[:,None] + np.arange(N)] 

Run Example -

 In [31]: a Out[31]: array([4, 2, 5, 4, 1, 6, 7, 3]) In [32]: N Out[32]: 5 In [33]: out Out[33]: array([[4, 2, 5, 4, 1], [2, 5, 4, 1, 6], [5, 4, 1, 6, 7], [4, 1, 6, 7, 3]]) 
+7
source

All Articles