2

I'd like to find what values exist in an array 'A' that also exist in array 'B'. However, the arrays are of different size and I'd like to introduce a tolerance as there is likely to be a systematic error between the two datasets.

I'm aware of 'np.isclose', but this is for arrays of the same size.

jpp
  • 159,742
  • 34
  • 281
  • 339
PhysGuy
  • 23
  • 3

3 Answers3

1

Nested for loops will work, but I suggest you wait to see if there's a numpy solution.

import numpy as np

A = np.array([0.3141, 1.234, 4.1341, -34.112])
B = np.array([0.3142, 2.234, 4.1340, -34.113])

res = {x for x in A for y in B if np.isclose(x, y, atol=0.1)}

print(res)

# {-34.112000000000002, 0.31409999999999999, 4.1341000000000001}
jpp
  • 159,742
  • 34
  • 281
  • 339
1

You could improve on nested loops by using a slightly more builtin numpy solution:

import numpy as np

A = np.array([0, 0.3141, 1.234, 4.1341, -34.112])
B = np.array([0.3142, 2.234, 4.1340, -34.113, 40])
res = {i for i in A if np.isclose(B, i, 0.1).any()}
print(res)

Output:

{0.3141, 4.1341, -34.112}

Timings:

In [2]: %timeit {i for i in A if np.isclose(B, i, 0.1).any()}
276 µs ± 16.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [3]: %timeit {x for x in A for y in B if np.isclose(x, y, atol=0.1)}
1.08 ms ± 12 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
user3483203
  • 50,081
  • 9
  • 65
  • 94
-3

It's not clear if the arrays are of different sizes of different dimensions. If they have the same dimension but different sizes, it is easy to compute their common elements with Numpy's builtin intersect1d: https://docs.scipy.org/doc/numpy/reference/generated/numpy.intersect1d.html

>>> import numpy as np
>>> A = np.array([1, 2, 3, 4, 5, 6])
>>> B = np.array([4, 5, 6, 7])
>>> C = np.intersect1d(A, B)
>>> print C
[4 5 6]

If they have different dimensions, you would probably have to follow the approach described here: Find common elements in 2D numpy arrays