Binning a numpy array Binning a numpy array arrays arrays

Binning a numpy array


Just use reshape and then mean(axis=1).

As the simplest possible example:

import numpy as npdata = np.array([4,2,5,6,7,5,4,3,5,7])print data.reshape(-1, 2).mean(axis=1)

More generally, we'd need to do something like this to drop the last bin when it's not an even multiple:

import numpy as npwidth=3data = np.array([4,2,5,6,7,5,4,3,5,7])result = data[:(data.size // width) * width].reshape(-1, width).mean(axis=1)print result


Since you already have a numpy array, to avoid for loops, you can use reshape and consider the new dimension to be the bin:

In [33]: data.reshape(2, -1)Out[33]: array([[4, 2, 5, 6, 7],       [5, 4, 3, 5, 7]])In [34]: data.reshape(2, -1).mean(0)Out[34]: array([ 4.5,  3. ,  4. ,  5.5,  7. ])

Actually this will just work if the size of data is divisible by n. I'll edit a fix.

Looks like Joe Kington has an answer that handles that.


Try this, using standard Python (NumPy isn't necessary for this). Assuming Python 2.x is in use:

data = [ 4, 2, 5, 6, 7, 5, 4, 3, 5, 7 ]# example: for n == 2n=2partitions = [data[i:i+n] for i in xrange(0, len(data), n)]partitions = partitions if len(partitions[-1]) == n else partitions[:-1]# the above produces a list of listspartitions=> [[4, 2], [5, 6], [7, 5], [4, 3], [5, 7]]# now the mean[sum(x)/float(n) for x in partitions]=> [3.0, 5.5, 6.0, 3.5, 6.0]