Repository navigation
Open
Description
The following call to histogram fails:
In [5]: np.histogram([2**53])
/Users/tom/miniconda3/envs/dev/lib/python3.6/site-packages/numpy/lib/function_base.py:566: RuntimeWarning: divide by zero encountered in double_scalars
norm = bins / (mx - mn)
/Users/tom/miniconda3/envs/dev/lib/python3.6/site-packages/numpy/lib/function_base.py:591: RuntimeWarning: invalid value encountered in multiply
tmp_a *= norm
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-5-41f77d7d86af> in <module>()
----> 1 np.histogram([2**53])
/Users/tom/miniconda3/envs/dev/lib/python3.6/site-packages/numpy/lib/function_base.py in histogram(a, bins, range, normed, weights, density)
598 # The index computation is not guaranteed to give exactly
599 # consistent results within ~1 ULP of the bin edges.
--> 600 decrement = tmp_a_data < bin_edges[indices]
601 indices[decrement] -= 1
602 # The last bin includes the right edge. The other bins do not.
IndexError: index -9223372036854775808 is out of bounds for axis 1 with size 11whereas this succeeds:
In [6]: np.histogram([2**53-1])
Out[6]:
(array([0, 0, 0, 0, 0, 0, 1, 0, 0, 0]),
array([ 9.00719925e+15, 9.00719925e+15, 9.00719925e+15,
9.00719925e+15, 9.00719925e+15, 9.00719925e+15,
9.00719925e+15, 9.00719925e+15, 9.00719925e+15,
9.00719925e+15, 9.00719925e+15]))The threshold for triggering the bug appears to be 2**53, but this only happens when the min of the array is the same as the max:
In [7]: np.histogram([2**53, 2**53])
/Users/tom/miniconda3/envs/dev/lib/python3.6/site-packages/numpy/lib/function_base.py:566: RuntimeWarning: divide by zero encountered in double_scalars
norm = bins / (mx - mn)
/Users/tom/miniconda3/envs/dev/lib/python3.6/site-packages/numpy/lib/function_base.py:591: RuntimeWarning: invalid value encountered in multiply
tmp_a *= norm
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-7-f74f7a6cf401> in <module>()
----> 1 np.histogram([2**53, 2**53])
/Users/tom/miniconda3/envs/dev/lib/python3.6/site-packages/numpy/lib/function_base.py in histogram(a, bins, range, normed, weights, density)
598 # The index computation is not guaranteed to give exactly
599 # consistent results within ~1 ULP of the bin edges.
--> 600 decrement = tmp_a_data < bin_edges[indices]
601 indices[decrement] -= 1
602 # The last bin includes the right edge. The other bins do not.
IndexError: index -9223372036854775808 is out of bounds for axis 1 with size 11When the min and the max are different, things are fine:
In [8]: np.histogram([2**53, 2**54])
Out[8]:
(array([1, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
array([ 9.00719925e+15, 9.90791918e+15, 1.08086391e+16,
1.17093590e+16, 1.26100790e+16, 1.35107989e+16,
1.44115188e+16, 1.53122387e+16, 1.62129587e+16,
1.71136786e+16, 1.80143985e+16]))Reacted by Oleksandr Zaitsev and Simon Sotak Gregor
Metadata
Metadata
Assignees
Labels
No labels
Activity
charris commentedon Feb 17, 2017
Is this a regression? I'm going to make a list of reported histogram issues and see if they can get fixed before the 1.12.1 release.
astrofrog commentedon Feb 17, 2017
This is present in Numpy 1.11 too and there is also an issue in 1.10 but the error is different
charris commentedon Feb 17, 2017
OK, thanks for checking. I rather expected that as the problem seems to be floating point granularity.
mirca commentedon Feb 18, 2017
Looks like
mn == mxevaluates toTrueeven after https://github.com/numpy/numpy/blob/master/numpy/lib/function_base.py#L670 which raisesRuntimeWarning: divide by zero encountered in double_scalarswhile attempting https://github.com/numpy/numpy/blob/master/numpy/lib/function_base.py#L730mirca commentedon Feb 18, 2017
A possible fix maybe would involve casting
mnandmxtonp.float128?njsmith commentedon Feb 18, 2017
polwel commentedon May 30, 2018
I get a similar error message for different input.
Since my example requires a number only representable in double precision, I thought that some unsafe cast was to blame. Not sure how its related to the original issue.NVM, the error message says right there what's going on:norm = n_equal_bins / (last_edge - first_edge)evaluates toinf, and it goes downhill from there.eric-wieser commentedon Aug 19, 2018
I think the fix here is just to disable the uniform-bins fast-path for large integers.
I don't know how you can lerp 64-bit integers without overflow - the formula we're using of
either:
I suspect
interphas similar problems.Some interesting related remarks about
lerpheresanjok-bless commentedon Nov 1, 2018
I have the same issue with a big double number, reproducible code:
I get an error:
I use numpy 1.15.1 and see the bug exists at least from 1.10 version. Are there any plans to fix this?
deal with numpy issue #8627
Better handling of the numpy issue #8627.
collinmccarthy commentedon May 14, 2020
I'm getting this same error with the following:
np.histogram(np.nan_to_num(np.inf))This makes it difficult to create a histogram when some values are +/- infinity, which happens a lot when logging gradient information in deep learning frameworks like PyTorch and Tensorflow.
Are there still plans to fix this issue? If not what should I set these large values to (sometimes they're in 32 or 64-bit arrays) to avoid this? Thank you.