Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Numpy unique() Function



The Numpy unique() function is used to return the sorted unique elements of an array. It can also optionally return the indices of the input array that give the unique values and the counts of each unique value.

This function is useful for removing duplicates from an array and understanding the frequency of elements.

Syntax

Following is the syntax of Numpy unique() function −

numpy.unique(arr, return_index, return_inverse, return_counts)

Parameters

Following are the parameters of the Numpy unique() function −

  • arr: The input array. Will be flattened if not 1-D array.
  • return_index: If True, returns the indices of elements in the input array.
  • return_inverse: If True, returns the indices of unique array, which can be used to reconstruct the input array.
  • return_counts: If True, returns the number of times the element in unique array appears in the original array.

Example 1

Following is the example of Numpy unique() function in which creating an array with the unique values of the given input array −

import numpy as np 

# Create a 1D array
a = np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9]) 

print('First array:') 
print(a) 
print('\n')  

# Get unique values in the array
print('Unique values of first array:') 
u = np.unique(a) 
print(u) 
print('\n')  

Output

First array:
[5 2 6 2 7 5 6 8 2 9]


Unique values of first array:
[2 5 6 7 8 9]

Example 2

Here in this example we are getting the unique values and their indices with the help of unique() function −

import numpy as np 

# Create a 1D array
a = np.array([5, 2, 6, 2, 1, 7, 5, 6, 8, 2, 9]) 

print('First array:') 
print(a) 
print('\n')   

# Get unique values and their indices in the original array
print('Unique array and Indices array:') 
u, indices = np.unique(a, return_index=True) 
print(indices) 
print('\n')  

print('We can see each number corresponds to index in original array:') 
print(a) 
print('\n')   

Output

First array:
[5 2 6 2 1 7 5 6 8 2 9]


Unique array and Indices array:
[ 4  1  0  2  5  8 10]


We can see each number corresponds to index in original array:
[5 2 6 2 1 7 5 6 8 2 9]

Example 3

Below is the example of reconstructing the original array with the unique elements and their indices −

import numpy as np 

# Create a 1D array
a = np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9]) 

print('First array:') 
print(a) 
print('\n')  

# Get unique values and indices to reconstruct the original array
print('Indices of unique array:') 
u, indices = np.unique(a, return_inverse=True) 
print(u) 
print('\n') 

print('Indices are:') 
print(indices) 
print('\n')  

print('Reconstruct the original array using indices:') 
print(u[indices]) 
print('\n')  

# Get counts of unique values
print('Return the count of repetitions of unique elements:') 
u, indices = np.unique(a, return_counts=True) 
print(u) 
print(indices)

Output

First array:
[5 2 6 2 7 5 6 8 2 9]


Indices of unique array:
[2 5 6 7 8 9]


Indices are:
[1 0 2 0 3 1 2 4 0 5]


Reconstruct the original array using indices:
[5 2 6 2 7 5 6 8 2 9]


Return the count of repetitions of unique elements:
[2 5 6 7 8 9]
[3 2 2 1 1 1]
numpy_array_manipulation.htm
Advertisements