np.in1d support tolerance parameter for float comparisons
See original GitHub issueI would like to add a tolerance parameter to np.in1d
to support robust float comparisons.
Original function: https://github.com/numpy/numpy/blob/master/numpy/lib/arraysetops.py#L305
Here is one example that would require an error-tolerant comparison:
a = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7 ], dtype=np.float32)
b = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7 ], dtype=np.float64)
print(np.in1d(a.astype(np.float64), b))
#array([False, False, False, False, True, False, False], dtype=bool)
What do you guys think of this suggestion?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:14 (13 by maintainers)
Top Results From Across the Web
Python: Change format of np.array or allow tolerance in in1d ...
For that I can use np.in1d . However, the precision on the larger array is larger as well. My problem is illustrated in...
Read more >numpy.in1d — NumPy v1.24 Manual
Returns a boolean array the same length as ar1 that is True where an element of ar1 is in ar2 and False otherwise....
Read more >Numpy_Review - Data 100
As you learned in homework one the np.array is the key data structure in ... to compare. rtol : float The relative tolerance...
Read more >Source code for pyuvdata.uvdata - Read the Docs
add the UVParameters to the class # standard angle tolerance: 10 mas in radians. ... description='Width of frequency channels (Hz)', expected_type=np.float, ...
Read more >The Right Way to Compare Floats in Python - davidamos.dev
Floating -point numbers are a fast and efficient way to store and ... can specify the relative tolerance with the rel_tol keyword argument...
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
This seems to work:
It defines inclusion as being in
[a[i] - tol, a[i] + tol)
, to get the interval to be closed (or open) on both ends you would need to either make two calls tonp.searchsorted
with differentside
kwargs or do something (untested!!!) like:I’m not sure the other functions are all that ambiguous. One more general approach might be to have the ability to pass in a function that is used to assert equality. It might be
__eq__
by default, but could then be set tonp.isclose
(possibly withpartial
with different absolute and relative tolerances).