Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unique rows of numpy array while preserving their order

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[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]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading