NumPy setdiff1d with tolerance - Comparing a numpy array to another and saving only the unique values - outside of a tolerance

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



NumPy setdiff1d with tolerance - Comparing a numpy array to another and saving only the unique values - outside of a tolerance



I have two numpy arrays:


A= [ 3.8357 3.2450]

B= [ 5.6132 3.2415 3.6086 3.5666 3.8769 4.3587]



I want to compare A to B and only keep the value in A that is unique - outside of a +/-0.04 tolerance (i.e. A=[3.8357]).



Any ideas as to how I can do this?




1 Answer
1



Approach #1



We could use broadcasting -


broadcasting


A[(np.abs(np.subtract.outer(A,B)) > 0.04).all(1)]



Approach #2



We could leverage searchsorted to have a generic numpy.isin with tolerance specifier for use in generic problems, like so -


searchsorted


numpy.isin


def isin_tolerance(A, B, tol):
A = np.asarray(A)
B = np.asarray(B)

Bs = np.sort(B) # skip if already sorted
idx = np.searchsorted(Bs, A)

linvalid_mask = idx==len(B)
idx[linvalid_mask] = len(B)-1
lval = Bs[idx] - A
lval[linvalid_mask] *=-1

rinvalid_mask = idx==0
idx1 = idx-1
idx1[rinvalid_mask] = 0
rval = A - Bs[idx1]
rval[rinvalid_mask] *=-1
return np.minimum(lval, rval) <= tol



Hence, to solve our case -


out = A[~isin_tolerance(A, B, tol=0.04)]



Sample run -


In [294]: A
Out[294]: array([13.8357, 3.245 , 3.8357])

In [295]: B
Out[295]: array([5.6132, 3.2415, 3.6086, 3.5666, 3.8769, 4.3587])

In [296]: A[~isin_tolerance(A, B, tol=0.04)]
Out[296]: array([13.8357, 3.8357])





The second approach works perfectly, thank you so much ^^!
– Neko
Aug 8 at 19:49






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS