Let's say I have the following array:
a = np.array([[1,2,3,4,5,6],
[7,8,9,10,11,12],
[3,5,6,7,8,9]])
I want to sum the first two values of the first row: 1+2 = 3, then next two values: 3+4 = 7, and then 5+6 = 11, and so on for every row. My desired output is this:
array([[ 3, 7, 11],
[15, 19, 23],
[ 8, 13, 17]])
I have the following solution:
def sum_chunks(x, chunk_size):
rows, cols = x.shape
x = x.reshape(x.size / chunk_size, chunk_size)
return x.sum(axis=1).reshape(rows, cols/chunk_size)
But it feels unnecessarily complicated, is there a better way? Perhaps a built-in?