This post lists some solution options -
def grouped_mean(data,M2,N1,N2):
Now grouped_mean can be calculated using np.einsum instead of np.sum , like this -
stage1_sum = np.einsum('ij->i',np.array(data).reshape(-1,N2)) grouped_mean = np.einsum('ijk->ik',stage1_sum.reshape(-1,N1,M2/N2))/(N1*N2)
Or you can enter with the splitting of the 2D input array into a 4D array, as suggested by the @Warren Weckesser solution , and then use np.einsum in the same way -
split_data = np.array(data).reshape(-1, N1, M2/N2, N2) grouped_mean = np.einsum('ijkl->ik',split_data)/(N1*N2)
Run Example -
In [182]: data = np.array([[1,5,9,13], ...: [2,6,10,14], ...: [3,7,11,15], ...: [4,8,12,16]]) In [183]: grouped_mean(data,4,2,2) Out[183]: array([ 3.5, 5.5, 11.5, 13.5])
Runtime tests
Computing grouped_mean seems to be the most computationally intensive piece of code. So here are some run-time tests for calculating with these three approaches -
In [174]: import numpy as np ...: