question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allclose returns True when comparing an empty array to a scalar

See original GitHub issue

In 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:closed
  • Created 9 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Sep 18, 2017

Adding a note to the docstring may be helpful.

I agree - lets leave this open as a reminder to do that

0reactions
spencerahillcommented, Sep 18, 2017

As stated in the original post, this isn’t a bug

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:

>>>np.all(np.equal(np.ones([5, 1]), np.ones([2])))
True

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.

Thanks very much for your help.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found