def reverseHistogram(data,bins=None):
"""
Bins data using numpy.histogram and calculates the
reverse indices for the entries like IDL.
Parameters:
data : data to pass to numpy.histogram
bins : bins to pass to numpy.histogram
Returns:
hist : bin content output by numpy.histogram
edges : edges output from numpy.histogram
rev : reverse indices of entries in each bin
Using Reverse Indices:
h,e,rev = histogram(data, bins=bins)
for i in range(h.size):
if rev[i] != rev[i+1]:
# data points were found in this bin, get their indices
indices = rev[ rev[i]:rev[i+1] ]
# do calculations with data[indices] ...
"""
if bins is None: bins = numpy.arange(data.max()+2)
hist, edges = numpy.histogram(data, bins=bins)
digi = numpy.digitize(data.flat,bins=numpy.unique(data)).argsort()
rev = numpy.hstack( (len(edges), len(edges) + numpy.cumsum(hist), digi) )
return hist,edges,rev
评论列表
文章目录