What is a faster way of finding unique x,y points (removing duplicates) in a numpy array like:
points = numpy.random.randint(0, 5, (10,2))
I thought of converting points to a complex numbers and then checking for unique, but that seems rather convoluted:
b = numpy.unique(points[:,0] + 1j * points[:,1])
points = numpy.column_stack((b.real, b.imag))
The unique() function is used to find the unique elements of an array. Returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements: the indices of the input array that give the unique values.
You can find the distinct values in an array using the Distinct function. The Distinct function takes the array as an input parameter and returns another array that consists only of the unique, or non-duplicate, elements.
To find unique rows in a NumPy array we are using numpy. unique() function of NumPy library.
Numpy with Python This function returns an array of unique elements in the input array. The function can be able to return a tuple of array of unique vales and an array of associated indices.
I would do it like this:
numpy.array(list(set(tuple(p) for p in points)))
For the fast solution in the most general case, maybe this recipe would interest you: http://code.activestate.com/recipes/52560-remove-duplicates-from-a-sequence/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With