Open In App
Related Articles

Find unique rows in a NumPy array

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will discuss how to find unique rows in a NumPy array. To find unique rows in a NumPy array we are using numpy.unique() function of NumPy library.

Syntax of np.unique() in Python

Syntax: numpy.unique()

Parameter:

  • ar: array
  • return_index: Bool, if True return the indices of the input array
  • return_inverse: Bool, if True return the indices of the input array
  • return_counts: Bool, if True return the number of times each unique item appeared in the input array
  • axis: int or none, defines the axis to operate on

Examples to get unique rows in a NumPy array

Example 1:

Get unique rows from complete 2D-array.

Python3

# import library
import numpy as np
 
# Create a 2D numpy array
arr2D = np.array([[11, 11, 12, 11],
                     [13, 11, 12, 11],
                     [16, 11, 12, 11],
                     [11, 11, 12, 11]])
 
print('Original Array :' ,
      arr2D, sep = '\n')
 
uniqueRows = np.unique(arr2D)
                        
 
# print the output result
print('Unique Rows:',
      uniqueRows, sep = '\n')

                    

Output:

Original Array :
[[11 11 12 11]
 [13 11 12 11]
 [16 11 12 11]
 [11 11 12 11]]
Unique Rows:
[11 12 13 16]

Example 2:

Get unique rows from complete 2D-array by passing axis = 0 in unique function along with 2D-array. You will notice that rows 1 and 4 are the same hence one of the columns is excluded.

Python3

# import library
import numpy as np
 
# Create a 2D numpy array
arr2D = np.array([[11, 11, 12, 11],
                     [13, 11, 12, 11],
                     [16, 11, 12, 11],
                     [11, 11, 12, 11]])
 
uniqueRows = np.unique(arr2D, axis=0)
                        
 
# print the output result
print('Unique Rows:',
      uniqueRows, sep = '\n')

                    

Output:

Unique Rows:
[[11 11 12 11]
 [13 11 12 11]
 [16 11 12 11]]

Example 3:

Get unique rows from complete 2D-array by passing axis=1 in unique function along with 2D-array. You will notice that columns 1 and 4 are the same hence one of the columns is excluded.

Python3

# import library
import numpy as np
 
# Create a 2D numpy array
arr2D = np.array([[11, 11, 12, 11],
                     [13, 11, 12, 11],
                     [16, 11, 12, 11],
                     [11, 11, 12, 11]])
 
uniqueRows = np.unique(arr2D, axis=1)
                        
 
# print the output result
print('Unique Rows:',
      uniqueRows, sep = '\n')

                    

Output:

Unique Rows:
[[11 11 12]
 [11 13 12]
 [11 16 12]
 [11 11 12]]

Example 4:

Returning the index of unique elements.

Python3

# import library
import numpy as np
 
# Create a 2D numpy array
arr2D = np.array([[11, 11, 12, 11],
                     [13, 11, 12, 11],
                     [16, 11, 12, 11],
                     [11, 11, 12, 11]])
 
uniqueRows = np.unique(arr2D, return_index=True)
                        
 
# print the output result
print('Unique Rows:',
      uniqueRows, sep = '\n')

                    

Output:

Unique Rows:
(array([11, 12, 13, 16]), array([0, 2, 4, 8], dtype=int64))

Example 5:

In this output, a ndarray will be shown that contains the indices to reconstruct the original array from the unique array.

Python3

# import library
import numpy as np
 
# Create a 2D numpy array
arr2D = np.array([[11, 11, 12, 11],
                     [13, 11, 12, 11],
                     [16, 11, 12, 11],
                     [11, 11, 12, 11]])
 
uniqueRows = np.unique(arr2D, return_inverse=True)
                        
 
# print the output result
print('Unique Rows:',
      uniqueRows, sep = '\n')

                    

Output:

Unique Rows:
(array([11, 12, 13, 16]), array([0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 0, 0, 1, 0], dtype=int64))


Last Updated : 03 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads