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]