More elegant solution for binning lookup?
I've browsed the numpy and scipy lists and available docs and haven't found an answer to this. SciPy v0.5.1, numpy v1.0rc3. I've got an array of samples through a region and a value 'R' where I want to find the proper bin in 'radius'. 'R' will generally not match any value in 'radius'. Here's the cleanest solution I found:
import scipy import numpy radius = scipy.arange(0.0, 1.5, 1.5/1000) r=0.7274 bin = numpy.nonzero(numpy.where(radius > r, 0, 1))[0][-1] bin 484 radius[484:486] array([ 0.726 , 0.7275])
But even this looks rather ugly and not very intuitive as to what I'm doing. Is there a function built-in to scipy or numpy for this? Thanks, Tom -- W.T. Bridgman, Ph.D. Physics & Astronomy
Hi Tom On Sun, Feb 18, 2007 at 05:22:08PM -0500, Tom Bridgman wrote:
I've got an array of samples through a region and a value 'R' where I want to find the proper bin in 'radius'. 'R' will generally not match any value in 'radius'.
Would digitize do the job? digitize(x,bins) Return the index of the bin to which each value of x belongs. Each index i returned is such that bins[i-1] <= x < bins[i] if bins is monotonically increasing, or bins [i-1] > x >= bins[i] if bins is monotonically decreasing. Beyond the bounds of the bins 0 or len(bins) is returned as appropriate. Cheers Stéfan
participants (2)
-
Stefan van der Walt -
Tom Bridgman