Reputation: 14836
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=array([1,4,5,6,7.8,9,3.4,5.,6,3.5,6,8,9,10])
bins=arange(0,11,1)
h=hist(a,bins=bins)
h=hist(a,bins=bins,weights=a)
This is what it is return
(array([ 0. , 1. , 0. , 6.9, 4. , 10. , 18. , 7.8, 8. , 28. ]), array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
I was wondering if it was possible to get 3.5 (which is the maximum number between 3 and 4) instead of 6.9 in the 4th bin.
Upvotes: 5
Views: 5519
Reputation: 25197
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. ])
Upvotes: 2
Reputation: 35269
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.
Upvotes: 4