Q:

Write a NumPy program to find unique rows in a NumPy array

0

Write a NumPy program to find unique rows in a NumPy array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
x = np.array([[20, 20, 20, 0],
              [0, 20, 20, 20],
              [0, 20, 20, 20],
              [20, 20, 20, 0],
              [10, 20, 20,20]])
print("Original array:")
print(x)
y = np.ascontiguousarray(x).view(np.dtype((np.void, x.dtype.itemsize * x.shape[1])))
_, idx = np.unique(y, return_index=True)

unique_result = x[idx]
print("Unique rows of the above array:")
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]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now