How to get data in a histogram bin

我与影子孤独终老i 提交于 2019-12-02 19:22:51
doug

digitize, from core NumPy, will give you the index of the bin to which each value in your histogram belongs:

import numpy as NP
A = NP.random.randint(0, 10, 100)

bins = NP.array([0., 20., 40., 60., 80., 100.])

# d is an index array holding the bin id for each point in A
d = NP.digitize(A, bins)     

how about something like:

In [1]: data = numpy.array([0, 0.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5, 3])
In [2]: hist, edges = numpy.histogram(data, bins=3)
In [3]: for l, r in zip(edges[:-1], edges[1:]):
    print(data[(data > l) & (data < r)])
   ....:     
   ....:     
[ 0.5]
[ 1.5  1.5  1.5]
[ 2.5  2.5  2.5]
In [4]: 

with a bit of code to handle the edge cases.

pyplot.hist in matplotlib creates a histogram (but also draws it to the screen, which you might not want). For just the bins, you can use numpy.histogram, as outlined in another answer.

Here is an example comparing pyploy.hist and numpy.histogram.

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