Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate Numpy Array By Summing

I have a Numpy array of shape (4320,8640). I would like to have an array of shape (2160,4320).

You'll notice that each cell of the new array maps to a 2x2 set of cells in the old array. I would like a cell's value in the new array to be the sum of the values in this block in the old array.

I can achieve this as follows:

import numpy

#Generate an example array
arr = numpy.random.randint(10,size=(4320,8640))

#Perform the transformation
arrtrans = numpy.array([ [ arr[y][x]+arr[y+1][x]+arr[y][x+1]+arr[y+1][x+1] for x in range(0,8640,2)] for y in range(0,4320,2)])

But this is slow and more than a little ugly.

Is there a way to do this using Numpy (or an interoperable package)?

like image 888
Richard Avatar asked Dec 26 '22 05:12

Richard


1 Answers

When the window fits exactly into the array, reshaping to more dimensions and collapsing the extra dimensions with np.sum is sort of the canonical way of doing this with numpy:

>>> a = np.random.rand(4320,8640)
>>> a.shape
(4320, 8640)
>>> a_small = a.reshape(2160, 2, 4320, 2).sum(axis=(1, 3))
>>> a_small.shape
(2160, 4320)
>>> np.allclose(a_small[100, 203], a[200:202, 406:408].sum())
True
like image 129
Jaime Avatar answered Dec 27 '22 18:12

Jaime