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

Mark.Miller mpmusu at cc.usu.edu
Fri Oct 5 17:54:50 EDT 2007


Check how you're implementing the histogram function with respect to 
that range statement.  It seems to make a difference, desirable or not.

 >>> import numpy
 >>> numpy.__version__
'1.0.4.dev3982'
 >>> 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, 2, 3, 3, 1])
 >>> y
[0, 1, 2, 3, 4, 5, 6]
 >>>
 >>> (x, y) = numpy.histogram(A, range=(0, 7))
 >>> x
array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0])
 >>> y
array([ 0. ,  0.7,  1.4,  2.1,  2.8,  3.5,  4.2,  4.9,  5.6,  6.3])
 >>>
 >>>
 >>> (x, y) = numpy.histogram(A, range(7,0))
 >>> x
array([], dtype=int32)
 >>> y
[]
 >>>

Note that in the last case, the histogram function isn't returning 
anything for a descending range.

Also notice that you're overwriting a python function with the way 
you're assigning things....

 >>> range(0,7)
[0, 1, 2, 3, 4, 5, 6]
 >>> range=(0,7)
 >>> range
(0, 7)

I'll leave it to others to decide if this is problematic.

-Mark

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)?
> 
> *  Why should the return histogram have negative elements?
> 
> *  If it truely isn't meaningful, why not catch the case and reject
> input?  Maybe this is a bug....  ???
> 
> Thanks!
> 
> Stuart
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list