[Numpy-discussion] histogram using decending range -- what do the results mean?

Robert Kern robert.kern@gmail....
Fri Oct 5 16:41:04 CDT 2007


Stuart Brorson wrote:
> Guys --
> 
> I'm a little puzzled by a NumPy behavior.  Perhaps the gurus on this
> list can enlighten me, please!
> 
> I am working with numpy.histogram.  I have a decent understanding of
> how it works when given an ascending range to bin into.  However, when
> I give it a *decending* range, I can't figure out what the results
> mean.  Here's an example:
> 
> ------------------------  <session log>  --------------------
> 
>>>> A = numpy.array([1, 2, 3, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1])
>>>> (x, y) = numpy.histogram(A, range=(0, 7))
>>>> x
> array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0])
>>>> (x, y) = numpy.histogram(A, range=(7, 0))
>>>> x
> array([ 0, -1, -3,  0, -3, -2,  0, -2, -2, 13])
> --------------------  </session log>  ------------------------
> 
> Please set aside the natural response "the user shouldn't bin into 
> a decending range!" since I am trying to figure out what computation
> NumPy actually does in this case and I don't want a work-around.  And
> yes, I have looked at the source.  It's nicely vectorized, so I find
> the source rather opaque.
> 
> Therefore, I would appreciate it if if some kind soul could answer a
> couple of questions:
> 
> *  What does the return mean for range=(7, 0)?

Nothing.

> *  Why should the return histogram have negative elements?

Because there are subtractions involved that depend on the bins being increasing
which they are not if the given range is incorrect.

> *  If it truely isn't meaningful, why not catch the case and reject
> input?  Maybe this is a bug....  ???

Patches are welcome.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list