numpy.unique() in Python

The numpy.unique() function finds the unique elements of an array and returns the sorted unique elements for the specified array. 

Syntax

Parameter

ar : This parameter returns an Input array.If the aaray is not 1-D, this will be flattened.

return_index : This is an optional parameter which takes Boolean values. If Boolean value ‘True’ is passed, it returns the indices of the parameter ‘ar’ (along the specified axis, if provided, or in the flattened array) that result in the unique array.

return_inverse : If Boolean value ‘True’ is passed, it returns the indices of the unique array that can be used to reconstruct the parameter ‘ar’.

return_counts : If Boolean value ‘True’ is passed, it returns the number of times each unique item appears in ar.

axis : The axis parameter is used to operate on.

Return

This function returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements:

  • It returns the indices of the input array that give the unique values
  • the indices of the unique array that reconstructs the input array
  • the number of times each unique value derives up in the input array

Example 1

Output

Pin It on Pinterest

Share This