I'm building from this question. I'm re-binning a numpy array using the solution posted there, with a small addition for the extra:
from numpy import arange,append
x = arange(20)
x = x[:(x.shape[0]/bin)*bin].reshape((x.shape[0]//bin,-1)).mean(1)
x= append(x,x[(x.shape[0]/bin)*bin:].mean())
This is to handle non divisor bins of x.shape[0]
. The append
adds the average of the remaining cells. The thing is I'm making a lot of arrays here, and beyond memory that can't be runtime efficient. Is there a better way? I'm even considering transferring to lists, re-binning, and finally using array(result) and return that.
To be clear for bin=6
, the first line yields:
array([ 2.5, 8.5, 14.5])
and the second will append:
18.5
Before the mean
operator the resulting matrices are:
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17]])
and the second:
array([18, 19])
The final result is of course:
array([ 2.5, 8.5, 14.5, 18.5])