You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
allclose returns True when comparing an empty array to a scalar #5654
I should think that np.allclose should return an error of some sort if the lengths of the arrays are not the same. There is certainly nothing in the docstring to suggest the kind of behavior you are seeing. Would a ValueError be the most appropriate if the two arrays are not the same shape?
Would a ValueError be the most appropriate if the two arrays are not the same shape?
Yes, I think so.
Any chance of a fix forthcoming? I'm unfamiliar with numpy's internals, but from the outside it seems like it would be relatively straightforward. If that's the case, I'd be willing to take a stab at it. Thanks!
In the form the problem is stated in the original post, this isn't a bug. Every element of np.array([]) is close to 1.
You'd expect np.allclose(a, 1) to have the same meaning as np.all(np.isclose(a, 1)). When a == []. then isclose also returns [], and all([]) is defined as `True. So that's why you get the result you do.
Would a ValueError be the most appropriate if the two arrays are not the same shape?
What's happening right now is broadcasting. This is useful to check something like if every point in a list of points is close to another point, ie np.allclose(points_2d, point_1d).
Can you give an argument for why broadcasting isn't desirable here?
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."
You'd expect np.allclose(a, 1) to have the same meaning as np.all(np.isclose(a, 1)). When a == []. then isclose also returns [], and all([]) is defined as `True. So that's why you get the result you do.
Thanks, that is a nice explanation of the logic.
This is useful to check something like if every point in a list of points is close to another point, ie np.allclose(points_2d, point_1d)
That's a reasonable use case that I hadn't considered.
Can you give an argument for why broadcasting isn't desirable here?
I'll walk back my statement. I see now that the existing behavior is also consistent with that of exact equality:
There is certainly nothing in the docstring to suggest the kind of behavior you are seeing.
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.
Activity
gmcastil commentedon Mar 18, 2015
I'll take a look at this - thanks.
I should think that
np.allcloseshould return an error of some sort if the lengths of the arrays are not the same. There is certainly nothing in the docstring to suggest the kind of behavior you are seeing. Would aValueErrorbe the most appropriate if the two arrays are not the same shape?spencerahill commentedon Sep 18, 2017
We have recently bumped into this odd behavior of
allclosewhen shapes don't match. It remains in 1.13:In contrast to:
Yes, I think so.
Any chance of a fix forthcoming? I'm unfamiliar with numpy's internals, but from the outside it seems like it would be relatively straightforward. If that's the case, I'd be willing to take a stab at it. Thanks!
eric-wieser commentedon Sep 18, 2017
In the form the problem is stated in the original post, this isn't a bug. Every element of
np.array([])is close to 1.You'd expect
np.allclose(a, 1)to have the same meaning asnp.all(np.isclose(a, 1)). Whena == []. thenisclosealso returns[], andall([])is defined as `True. So that's why you get the result you do.What's happening right now is broadcasting. This is useful to check something like if every point in a list of points is close to another point, ie
np.allclose(points_2d, point_1d).Can you give an argument for why broadcasting isn't desirable here?
spencerahill commentedon Sep 18, 2017
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.
eric-wieser commentedon Sep 18, 2017
I agree - lets leave this open as a reminder to do that
DOC allclose doesn't require matching shapes
DOC allclose doesn't require matching shapes