I have a 4d numpy array (these are stacks of imaging data) and would like to perform mean binning along all but one of the axes.
starting with say
x=np.random.random((3,100,100,100))
I want to apply binning to axes 1,2,3 with bin size 10 and average the values in each bin.
expected result would be an array of shape (3,10,10,10)
I have looked into np.reshape like so:
result=x.reshape(3,-1,10,100,100).mean(axis=1)
result=result.reshape(3,10,-1,10,100).mean(axis=2)
and so on, but this messes up the structure of the image arrays
is there a more straightforward way to do this?