Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding unique points in numpy array

Tags:

python

numpy

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))
like image 492
Benjamin Avatar asked Nov 03 '11 02:11

Benjamin


People also ask

How do you find the unique elements of a Numpy array?

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.

How do I find unique values in an array?

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.

How do I find unique rows in Numpy?

To find unique rows in a NumPy array we are using numpy. unique() function of NumPy library.

What does Numpy unique return?

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.


1 Answers

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/

like image 142
wim Avatar answered Oct 01 '22 07:10

wim