Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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]

Function will return

[1.1 , 1.9 , 2.5 , 2.9]

If the tolerance is 0.3

at bit like the MATLAB function http://mathworks.com/help/matlab/ref/uniquetol.html (but this function uses a relative tolerance, an absolute one can be sufficient) What is the pythonic way to implement it ? (numpy is privilegied)

like image 629
Covich Avatar asked Jun 15 '16 22:06

Covich


1 Answers

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.

like image 64
Divakar Avatar answered Oct 01 '22 20:10

Divakar