allclose returns True when comparing an empty array to a scalar
See original GitHub issueIn Numpy v 1.9.1
import numpy as np
np.allclose(np.array([]), 1)
returns True
. The value of the scalar doesn’t matter. It also returns True
when comparing an empty array to an array with only one value, e.g.
np.allclose(np.array([]), np.array([123])
also returns True
. It should return False
unless comparing two empty arrays if you ask me.
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
numpy.allclose — NumPy v1.24 Manual
Returns True if two arrays are element-wise equal within a tolerance. The tolerance values are positive, typically very small numbers. The relative difference...
Read more >numpy: is there an `allclose(np.array, scalar)`? - Stack Overflow
Is there a function that combines both functionalities?? That is, can compare an array and a scalar for approximate element-wise equality??
Read more >NumPy: Compare ndarray element by element - nkmk note
In NumPy, you can compare two arrays element by element with comparison operators such as > and ==. A boolean ndarray is returned....
Read more >cf.Data — Documentation - GitHub Pages
May be any scalar or array-like object, including another Data instance. ... allclose, Returns True if two broadcastable arrays have equal values, ...
Read more >Supported NumPy features - Numba
Numba supports the following Numpy scalar types: ... The real attribute returns a view of the real part of the complex array and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree - lets leave this open as a reminder to do that
I was agreeing with the sentiment by @gmcastil that “np.allclose should return an error of some sort if the lengths of the arrays are not the same.”
Thanks, that is a nice explanation of the logic.
That’s a reasonable use case that I hadn’t considered.
I’ll walk back my statement. I see now that the existing behavior is also consistent with that of exact equality:
Adding a note to the docstring may be helpful. The crux of my confusion was that I had incorrectly assumed that these required matching array shapes. Closing this Issue also seems appropriate – the combination of nothing in the docstring about this and an open Issue on it led me to believe that it was indeed a bug.
Thanks very much for your help.