3

I have a set of discretized coordinates in a Nx2 numpy.ndarray.

I would like to get the counts and indices of each of these unique coordinate sets. numpy.unique does exactly this, but for scalar elements.

Is there some clean way to do this using numpy?

Example:

#input coor = np.array([[10,10],[12,9],[10,5],[12,9]]) #output unique_count = np.array([1,2,1]) unique_index = np.array([0,1,2]) #1 could also be 3 

EDIT: unique count, would give the counts of each of the unique values, ie: 1 of [10,10], 2 of [12,9] and 1 of [10,5]. One would then find the values these correspond to with coor[unique_index]

3
  • Can you explain in more detail the content of unique_count and unique_index? I don't understand it. Commented Jul 19, 2016 at 14:04
  • @Ohumeronen Because I want the indices of the coordinate pairs, not the count if each of the X/Y- coordinates themselves Commented Jul 19, 2016 at 14:05
  • I begin to understand... Commented Jul 19, 2016 at 14:07

1 Answer 1

1

You can use .count() and .index() list's methods

coor = np.array([[10, 10], [12, 9], [10, 5], [12, 9]]) coor_tuple = [tuple(x) for x in coor] unique_coor = sorted(set(coor_tuple), key=lambda x: coor_tuple.index(x)) unique_count = [coor_tuple.count(x) for x in unique_coor] unique_index = [coor_tuple.index(x) for x in unique_coor] 
Sign up to request clarification or add additional context in comments.

2 Comments

I tried it but got the following output (array([ 5, 9, 10, 12]), array([5, 3, 0, 2]), array([1, 2, 3, 2])), => 3 10's, 2 12 and 9's and 1 5. I think it might flatten the array before it computes the unique value as the indices also suggest.
Yes you are right, see my edit. I don't think that it is the fastest way to do what you want, but it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.