w3resource

NumPy: Find unique rows in a NumPy array

NumPy: Array Object Exercise-87 with Solution

Write a NumPy program to find distinct rows in a NumPy array.

Pictorial Presentation:

Python NumPy: Find unique rows in a NumPy array

Sample Solution:

Python Code:

# Importing the NumPy library and aliasing it as 'np'
import numpy as np

# Creating a NumPy array 'x' with multiple rows and columns
x = np.array([[20, 20, 20, 0],
              [0, 20, 20, 20],
              [0, 20, 20, 20],
              [20, 20, 20, 0],
              [10, 20, 20, 20]])

# Printing a message indicating the original array will be displayed
print("Original array:")

# Printing the original array 'x' with its elements
print(x)

# Converting the array 'x' into a contiguous array and then viewing it as an array of void types
y = np.ascontiguousarray(x).view(np.dtype((np.void, x.dtype.itemsize * x.shape[1])))

# Finding unique rows and their indices in the viewed array 'y'
# The indices are used to retrieve the unique rows from the original array 'x'
_, idx = np.unique(y, return_index=True)
unique_result = x[idx]

# Printing a message indicating the display of unique rows of the array
print("Unique rows of the above array:")

# Printing the resulting array 'unique_result' containing unique rows of the original array 'x'
print(unique_result)

Sample Output:

Original array:                                                        
[[20 20 20  0]                                                         
 [ 0 20 20 20]                                                         
 [ 0 20 20 20]                                                         
 [20 20 20  0]                                                         
 [10 20 20 20]]                                                        
Unique rows of the above array:                                        
[[ 0 20 20 20]                                                         
 [10 20 20 20]                                                         
 [20 20 20  0]]

Explanation:

In the above code -

x = np.array(...): This line creates a 5x4 NumPy array 'x' with given elements.

y = np.ascontiguousarray(x).view(np.dtype((np.void, x.dtype.itemsize * x.shape[1]))): Create a new view of 'x' where each row is treated as a single item of np.void type. This is done by creating a contiguous array of 'x' and then changing the dtype to np.void with the size equal to the size of a row (number of columns * itemsize).

_, idx = np.unique(y, return_index=True): Call np.unique() with the parameter return_index=True on the 'y' array. This returns the unique elements of 'y' (unique rows of 'x') and their indices in the original 'x' array. We only need the indices, so we store them in the variable 'idx' and ignore the unique elements with the underscore '_'.

unique_result = x[idx]: Use the 'idx' indices to get the unique rows from the 'x' array.

print(unique_result): This line prints the unique rows of the 'x' array.

Python-Numpy Code Editor:

Previous: Write a NumPy program to how to add an extra column to an NumPy array.
Next: Write a NumPy program to replace all elements of NumPy array that are greater than specified array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.