numpy.unique() – The NumPy unique Python Function

Introduction to NumPy unique

Using numpy.unique() function We can get unique values from an array given as an input in the numpy.unique() function.

Syntax:

numpy.unique(arr, return_index=False, return_inverse=False, return_counts=False, axis=None)

Parameters:

  • arr: The array you want to operate on
  • return_index (optional): Boolean that specifies if you want to return indexes of unique value.
  • return_counts (optional): Boolean that specifies if you want to return counts of unique value.
  • axis (optional): The axis along which to use the function

Return: Return the unique of an array.

Code Examples of NumPy unique

Example 01: Get unique values from a 1D Numpy array

# welcome to softhunt.net
import numpy as np

duplicates = np.array([2,3,3,4,5,5,1,5,4,6,7,5,1,5,3,5,1,3])

# GET UNIQUE VALUES
ans = np.unique(duplicates)
print(ans)

Output:

[1 2 3 4 5 6 7]

Example 02: Identify the index of the first occurrence of unique values

# welcome to softhunt.net
import numpy as np

duplicates = np.array([2,3,3,4,5,5,1,5,4,6,7,5,1,5,3,5,1,3])

# GET UNIQUE VALUES
ans = np.unique(duplicates, return_index = True)
print(ans)

Output:

(array([1, 2, 3, 4, 5, 6, 7]), array([ 6,  0,  1,  3,  4,  9, 10]))

Example 03: Get the counts of each unique value

# welcome to softhunt.net
import numpy as np

duplicates = np.array([2,3,3,4,5,5,1,5,4,6,7,5,1,5,3,5,1,3])

# GET UNIQUE VALUES
ans = np.unique(duplicates, return_counts = True)
print(ans)

Output:

(array([1, 2, 3, 4, 5, 6, 7]), array([3, 1, 4, 2, 6, 1, 1]))

Example 04: Get the unique rows and columns

# welcome to softhunt.net
import numpy as np

# Creating 2D Array
duplicate_array_2d = np.array([[2,2,1],[4,5,5],[2,5,5]])

print('2D array:\n', duplicate_array_2d)

# GET UNIQUE ROWS
unique_rows  = np.unique(duplicate_array_2d, axis = 0)
print('Unique rows: \n', unique_rows)

# GET UNIQUE COLUMNS
unique_columns  = np.unique(duplicate_array_2d, axis = 1)
print('Unique columns: \n', unique_columns)

Output:

2D array:
 [[2 2 1]
 [4 5 5]
 [2 5 5]]
Unique rows: 
 [[2 2 1]
 [2 5 5]
 [4 5 5]]
Unique columns: 
 [[1 2 2]
 [5 4 5]
 [5 2 5]]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

Does NumPy unique sort?

We can’t do this directly with a unique function. Instead as a Numpythonic approach, you can use the return_index keyword to get the indices of the unique items then use np.argsort to get the indices of the sorted count items and use the result to find the items based on their frequency.

what is NumPy

NumPy is the most important Python module for scientific computing. It’s a Python library that includes a multidimensional array object, derived objects (such as masked arrays and matrices), and a variety of routines for performing fast array operations, such as mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation, and more.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.trim_zeros() – The NumPy trim_zeros Python Function

Leave a Comment