w3resource
ankara escort
malatya escort
NumPy Exercises

NumPy: Get the unique elements of an array

NumPy: Array Object Exercise-19 with Solution

Write a NumPy program to get the unique elements of an array.

Pictorial Presentation:

Python NumPy: Get the unique elements of an array

Sample Solution:-

Python Code:

import numpy as np
x = np.array([10, 10, 20, 20, 30, 30])
print("Original array:")
print(x)
print("Unique elements of the above array:")
print(np.unique(x))
x = np.array([[1, 1], [2, 3]])
print("Original array:")
print(x)
print("Unique elements of the above array:")
print(np.unique(x))

Sample Output:

Original array:                                                        
[10 10 20 20 30 30]                                                    
Unique elements of the above array:                                    
[10 20 30]                                                             
Original array:                                                        
[[1 1]                                                                 
 [2 3]]                                                                
Unique elements of the above array:                                    
[1 2 3]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to find common values between two arrays.
Next: Write a NumPy program to find the set difference of two arrays. The set difference will return the sorted, unique values in array1 that are not in array2.

What is the difficulty level of this exercise?



New Exercises:R Programming