Numpy histogram, how to take the maximum value in each bin

一曲冷凌霜 提交于 2019-12-23 17:07:03

问题


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.


回答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.




回答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. ])


来源:https://stackoverflow.com/questions/10750894/numpy-histogram-how-to-take-the-maximum-value-in-each-bin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!