uniquify an array/list with a tolerance in python (uniquetol equivalent)

前端 未结 2 1293
迷失自我
迷失自我 2020-12-20 17:03

I want to find the unique elements of an array in a certain range of tolerance

For instance, for an array/list

[1.1 , 1.3 , 1.9 , 2.0 , 2.5 , 2.9]
         


        
2条回答
  •  半阙折子戏
    2020-12-20 17:37

    With A as the input array and tol as the tolerance value, we could have a vectorized approach with NumPy broadcasting, like so -

    A[~(np.triu(np.abs(A[:,None] - A) <= tol,1)).any(0)]
    

    Sample run -

    In [20]: A = np.array([2.1,  1.3 , 1.9 , 1.1 , 2.0 , 2.5 , 2.9])
    
    In [21]: tol = 0.3
    
    In [22]: A[~(np.triu(np.abs(A[:,None] - A) <= tol,1)).any(0)]
    Out[22]: array([ 2.1,  1.3,  2.5,  2.9])
    

    Notice 1.9 being gone because we had 2.1 within the tolerance of 0.3. Then, 1.1 gone for 1.3 and 2.0 for 2.1.

    Please note that this would create a unique array with "chained-closeness" check. As an example :

    In [91]: A = np.array([ 1.1,  1.3,  1.5,  2. ,  2.1,  2.2, 2.35, 2.5,  2.9])
    
    In [92]: A[~(np.triu(np.abs(A[:,None] - A) <= tol,1)).any(0)]
    Out[92]: array([ 1.1,  2. ,  2.9])
    

    Thus, 1.3 is gone because of 1.1 and 1.5 is gone because of 1.3.

提交回复
热议问题