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

后端 未结 1 1326
不知归路
不知归路 2020-12-18 11:36

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

相关标签:
1条回答
  • 2020-12-18 12:02

    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])
    
    0 讨论(0)
提交回复
热议问题