Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Approach #1

We could use 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 -

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])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...