Find unique rows in NumPy.array

The following code can find the unique rows in the NumPy array. If you want to learn Python, I highly recommend reading This Book.

Find unique rows in NumPy.array
Find unique rows in NumPy.array

In this example, there are 5 rows in the array. 2nd and 3rd rows are duplicates of the 1st row. It will be removed from the array when we find unique rows.

# Import the NumPy library
import numpy as np

# Make a NumPy array
a = np.array([[1, 2, 3, 4, 5],
              [1, 2, 3, 4, 5],
              [1, 2, 3, 4, 5],
              [2, 4, 6, 8, 10],
              [3, 4, 5, 6, 7]])

# Find the unique rows
unique_rows = np.unique(a, axis=0)

# Display the unique rows
print(unique_rows)

Output:

[[ 1  2  3  4  5]
 [ 2  4  6  8 10]
 [ 3  4  5  6  7]]

People are also reading:

Best Python Books

What is Computer Vision? Examples, Applications, Techniques

Books for Machine Learning (ML)

Free Learning Resources

Leave a Comment

Your email address will not be published.