[Numpy-discussion] np.histogram: upper range bin

Peter Butterworth butterw at gmail.com
Thu Jun 2 04:44:34 EDT 2011


in np.histogram the top-most bin edge is inclusive of the upper range
limit. As documented in the docstring (see below) this is actually the
expected behavior, but this can lead to some weird enough results:

In [72]: x=[1, 2, 3, 4]; np.histogram(x, bins=3)
Out[72]: (array([1, 1, 2]), array([ 1.,  2.,  3., 4.]))

Is there any way round this or an alternative implementation without
this issue ?

Currently it seems you have to explicitly specify a range with an
extra bin at the high end:
In [77]: x=[1, 2, 3, 4]; np.histogram(x, bins=4, range=[1, 5])
Out[77]: (array([1, 1, 1, 1]), array([ 1.,  2.,  3.,  4.,  5.]))


'''
Notes
-----
All but the last (righthand-most) bin is half-open.  In other words, if
`bins` is::

  [1, 2, 3, 4]

then the first bin is ``[1, 2)`` (including 1, but excluding 2) and the
second ``[2, 3)``.  The last bin, however, is ``[3, 4]``, which *includes*
4.
'''

-- 
thanks,
peter butterworth



More information about the NumPy-Discussion mailing list