Skip to content Skip to sidebar Skip to footer

Numpy Histogram, How To Take The Maximum Value In Each Bin

I have a series of numbers that I bin with the code above. Is it possible to return the maximum number in each bin? Have a look at the example code: from numpy import * a=arr

Solution 1:

This will give you the maximum value of each element in a bin, and 0 if there are no elements in the bin:

print [max(a[(a>=(i))&(a<i+1)]) if a[(a>=(i))&(a<i+1)].size else 0 for i in bins]
[0, 1.0, 0, 3.5, 4.0, 5.0, 6.0, 7.7999999999999998, 8.0, 9.0, 10.0]

Change +1 to your bin size, to make it useful.

Solution 2:

You could use numpy.digitize. Note that it labels values smaller than the first bin with 0.

a[np.digitize(a,bins) == 4].max()

A masked array is useful here:

import numpy.ma as ma
a2 = ma.empty((len(bins),len(a)))
a2.data[...] = a
a2.mask = np.digitize(a,bins)-1 != bins[:,np.newaxis]
a2.max(axis=1).filled(np.nan)

array([  nan,   1. ,   nan,   3.5,   4. ,   5. ,   6. ,   7.8,   8. ,
         9. ,  10. ])

Post a Comment for "Numpy Histogram, How To Take The Maximum Value In Each Bin"