NumPy| How to get the unique elements of an Array Last Updated : 09 Feb, 2024 Comments Improve Suggest changes Like Article Like Report To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4 5]SyntaxSyntax: np.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) Parameters ar: Input array. return_index: If True, also return the indices of ar that result in the unique array.return_inverse: If True, also return the indices of the unique array that can be used to reconstruct ar.return_counts: If True, also return the number of times each unique item appears in ar.axis: The axis to operate on. If None, ar will be flattened.Returns: Return the unique of an array. Let us look at some examples of how to get the unique elements of an array using NumPy library: More ExamplesHere we will see examples of how to find unique elements in 1D and 2D NumPy arrays. Example 1: Finding Unique Elements in a 1D Numpy Array 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: Finding Unique Elements in a 2D Numpy Array 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] Comment More infoAdvertise with us Next Article NumPy| How to get the unique elements of an Array Y ysachin2314 Follow Improve Article Tags : Python Python-numpy Python numpy-program Python numpy-arrayManipulation Practice Tags : python Similar Reads How to count the frequency of unique values in NumPy array? Let's see How to count the frequency of unique values in the NumPy array. Pythonâs Numpy library provides a numpy.unique() function to find the unique elements and their corresponding frequency in a NumPy array. numpy.unique() Syntax Syntax: numpy.unique(arr, return_counts=False) Return: Sorted uniq 4 min read How to Remove Duplicate Elements from NumPy Array In this article, we will see how to remove duplicate elements from NumPy Array. Here we will learn how to Remove Duplicate Elements from a 1-D NumPy Array and 2-D NumPy Array.Input1: [1 2 3 4 5 1 2 3 1 2 9] Output1: [1 2 3 4 5 9] Explanation: In this example, we have removed duplicate elements from 7 min read Find unique rows in a NumPy array Finding unique rows means removing duplicate rows from a 2D array so that each row appears only once. For example, given [[1, 2], [3, 4], [1, 2], [5, 6]], the unique rows would be [[1, 2], [3, 4], [5, 6]]. Letâs explore efficient ways to achieve this using NumPy.Using np.unique(axis=0)np.unique(axis 3 min read NumPy ndarray.size() Method | Get Number of Elements in NumPy Array The ndarray.size() method returns the number of elements in the NumPy array. It works the same as np.prod(a.shape), i.e., the product of the dimensions of the array. Example Python3 import numpy as np arr = np.zeros((3, 4, 2), dtype = np.complex128) gfg = arr.size print (gfg) Output : 24Syntax Synta 1 min read Python | Numpy np.unique() method numpy.unique() finds the unique elements of an array. It is often used in data analysis to eliminate duplicate values and return only the distinct values in sorted order. Example:Pythonimport numpy as np a = np.array([1, 2, 2, 3, 4, 4, 4]) res = np.unique(a) print(res)Output[1 2 3 4] Explanation: nu 2 min read Like