NumPy Tutorial NumPy Statistics NumPy References

NumPy - unique() function



The NumPy unique() function finds the unique elements of an array and returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements:

  • the indices of the input array that give the unique values
  • the indices of the unique array that reconstruct the input array
  • the number of times each unique value comes up in the input array

Syntax

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

Parameters

ar Required. Specify the input array (array_like). Unless axis is specified, this will be flattened if it is not already 1-D.
return_index Optional. If True, also return the indices of ar (along the specified axis, if provided, or in the flattened array) that result in the unique array.
return_inverse Optional. If True, also return the indices of the unique array (for the specified axis, if provided) that can be used to reconstruct ar.
return_counts Optional. If True, also return the number of times each unique item appears in ar.
axis Optional. Specify axis to operate on. If None, ar will be flattened. If an integer, the subarrays indexed by the given axis will be flattened and treated as the elements of a 1-D array with the dimension of the given axis.

Return Value

Returns the sorted unique values. Along with this:

  • If return_index is True, also returns indices of the first occurrences of the unique values in the original array.
  • If return_inverse is True, also returns indices to reconstruct the original array from the unique array.
  • If return_counts is True, also returns the number of times each of the unique values comes up in the original array.

Example:

In the example below, unique() function is used to unique two given arrays.

import numpy as np

Arr = np.array([1, 2, 6, 4, 2, 3, 2])
print("The original array:")
print(Arr)

#getting unique values
u = np.unique(Arr)
print("\nThe unique values are:")
print(u)

#getting indices of first occurrences 
#of the unique values
u, indices = np.unique(Arr, return_index=True)
print("\nIndices of first occurrence of unique values:")
print(indices)

#getting indices to reconstruct the original
#array from unique array
u, indices = np.unique(Arr, return_inverse=True)
print("\nIndices to reconstruct the")
print("original array from unique array:")
print(indices)
print("\nReconstruct the original array:")
print(u[indices])

#getting the count of unique values in the original array
u, indices = np.unique(Arr, return_counts=True)
print("\nCount of unique values in the original array:")
print(indices)

The output of the above code will be:

The original array:
[1 2 6 4 2 3 2]

The unique values are:
[1 2 3 4 6]

Indices of first occurrence of unique values:
[0 1 5 3 2]

Indices to reconstruct the
original array from unique array:
[0 1 4 3 1 2 1]

Reconstruct the original array:
[1 2 6 4 2 3 2]

Count of unique values in the original array:
[1 3 1 1 1]

❮ NumPy - Array Manipulation

5