Unique rows of numpy array while preserving their order

Advertisements

How can I get the unique rows of an array while preserving the order (of first appearance) of the rows in the result?

The below code tried with variations resulting in a single array.
array_equal compares with element positions.

import numpy as np
unique = np.array([])
arr = np.array([[1,2,3,4,5],[3,4,5,6,7],[5,6,7,8,9],[7,8,9,0,1],[9,0,1,2,3],[1,2,3,4,5],[-8,-7,-6,-5,-4]])

u = 0
for idx, i in enumerate(arr):
       if np.array_equal(unique, i) == False:
        unique = np.append(unique, i, axis=None)
        u += 1

print (unique)
print(u)

>>> print (unique)
[ 1.  2.  3.  4.  5.  3.  4.  5.  6.  7.  5.  6.  7.  8.  9.  7.  8.  9.
  0.  1.  9.  0.  1.  2.  3.  1.  2.  3.  4.  5. -8. -7. -6. -5. -4.]
>>> print(u)
7
>>>

For this example, the expected result is an array with 6 unique rows.

[[1,2,3,4,5],[3,4,5,6,7],[5,6,7,8,9],[7,8,9,0,1],[9,0,1,2,3],[-8,-7,-6,-5,-4]]

>Solution :

This can be done by simply passing the axis=0 argument to np.unique, telling numpy to compare the rows to each other. To preserve the order you can follow this answer, whereby you get the unique indices and use those (once sorted) to select the unique rows of arr.

import numpy as np

arr = np.array([[ 1,  2,  3,  4,  5],
                [ 3,  4,  5,  6,  7],
                [ 5,  6,  7,  8,  9],
                [ 7,  8,  9,  0,  1],
                [ 9,  0,  1,  2,  3],
                [ 1,  2,  3,  4,  5],
                [-8, -7, -6, -5, -4]])
_, idx = np.unique(arr, axis=0, return_index=True)
unique = arr[np.sort(idx)]
print(unique)

Output:

[[ 1  2  3  4  5]
 [ 3  4  5  6  7]
 [ 5  6  7  8  9]
 [ 7  8  9  0  1]
 [ 9  0  1  2  3]
 [-8 -7 -6 -5 -4]]

Leave a ReplyCancel reply