[Numpy-discussion] Bin selection

Magnus Lie Hetland magnus at hetland.org
Mon Dec 30 15:58:02 EST 2002

I have a set of limits, e.g. array([0, 35, 45, 55, 75]) and I want to
use these to "classify" a set of numbers (another one-dimensional
array). The "class" is the number of the first limit that is lower
than or equal to the number I want to classify. E.g. I'd classify 17
as 0 and 42 as 1.

My current approach is:

  sum(nums[:,NewAxis] >= lims, dim=-1)

It seems a bit unnecessary to compare each number with all the limits
when O(log(n)) would suffice (the limits are ordered); or even with
O(n) running time, a smarter implementation could get an average of
n/2 comparisons...


Magnus Lie Hetland

More information about the NumPy-Discussion mailing list