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]

M.T
  • 4,917
  • 4
  • 33
  • 52

1 Answers1

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]
Ghilas BELHADJ
  • 13,412
  • 10
  • 59
  • 99
  • 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. – M.T Jul 19 '16 at 14:19
  • 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. – Ghilas BELHADJ Jul 19 '16 at 14:47