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.

np.in1d support tolerance parameter for float comparisons

See original GitHub issue

I 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:open
  • Created 7 years ago
  • Reactions:2
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
jaimefriocommented, Jul 1, 2016

This seems to work:

import numpy as np

def in1d(a, b, tol=1e-6):
    a = np.unique(a)
    intervals = np.empty(2*a.size, float)
    intervals[::2] = a - tol
    intervals[1::2] = a + tol
    overlaps = intervals[:-1] >= intervals[1:]
    overlaps[1:] = overlaps[1:] | overlaps[:-1]
    keep = np.concatenate((~overlaps, [True]))
    intervals = intervals[keep]
    return np.searchsorted(intervals, b, side='right') & 1 == 1

a = np.array([1, 1.1, 1.3, 1.6, 2.0, 2.5, 3, 3.4, 3.8])
b = np.linspace(0, 10, 101)
c = (((b >= 0.8) & (b < 2.2)) |
     ((b >= 2.3) & (b < 2.7)) |
     ((b >= 2.8) & (b < 4.0)))
assert np.all(in1d(a, b, tol=0.2) == c)

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 to np.searchsorted with different side kwargs or do something (untested!!!) like:

indices = np.searchsorted(intervals, b)
ret = indices & 1 == 1
missed_ret = b[~ret] == a[indices[~ret]]
ret[~ret] |= missed_ret
return ret
1reaction
mhvkcommented, Jul 2, 2016

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 to np.isclose (possibly with partial with different absolute and relative tolerances).

Read more comments on GitHub >

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

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