Thursday, July 4, 2024
HomeLanguagesPythonHow to get the unique elements of an array using NumPy?

How to get the unique elements of an array using NumPy?

Let’s see How to get the unique elements of an array using NumPy. So for finding unique elements from the array we are using numpy.unique() function of NumPy library.

Syntax: np.unique(Array)
Return: Return the unique of an array. 

Now, Let’s see the examples:

Example 1:

Python3




# import library
import numpy as np            
 
# create 1d-array
arr = np.array([3, 3, 4,
                5, 6, 5,
                6, 4])
 
# find unique element
# from a array
rslt = np.unique(arr)
 
print(rslt)


Output:

[3 4 5 6]

Example 2:

Python3




# import library
import numpy as np
 
# create a numpy 2d-array
arr = np.array([[9, 9, 7, 7],
              [3, 4, 3, 4]])
 
# find unique element
# from a array
rslt = np.unique(arr)
 
print(rslt)


Output:

[3 4 7 9] 
Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments