– The indices of the input array that give the unique values,
– The indices of a new numpy object that reconstructs all numpy objects from this one, which is not repeated in order
– And finally, how many times each npy object appears in order.
Normally numpy.unique() operates on ndarrays, but it can also be applied to other iterables such as lists and tuples by passing the list keyword argument to numpy.unique().
Table of Contents
What does NumPy unique do?
Have you ever wondered what the NumPy unique function does? The NumPy u function returns an array of unique elements in the input array. It can also return a tuple of arrays that contain both values and indices for those values, depending on your needs.
Syntax
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
Parameters
ar: array_like
Array of inputs. If it isn’t already 1-D, it will be flattened until axis is given.
return_indexbool, optional
If True, also return the indices of ar that result in the unique array (along the specified axis, if provided, or in the flattened array).
return_inversebool, optional
If True, also return the indices of the unique array that can be used to reconstruct ar (for the chosen axis, if provided).
return_countsbool, optional
Return the number of times each unique item appears in ar if True.
In version 1.9.0, there is a new feature.
axisint or None, optional
The axis on which to work. If none is specified, ar will be flattened. If the value is an integer, the subarrays indexed by the provided axis will be flattened and handled as elements of a 1-D array with the given axis’ dimension; see the notes for additional information.
If the axis kwarg is used, object arrays or structured arrays containing objects are not supported. None is the default value.
In version 1.13.0, there is a new feature.
Returns
unique: ndarray
The unique values that have been sorted.
unique_indices: ndarray, optional
The indices of the unique values’ initial occurrences in the original array. If return index is True, this option is only available.
unique_inverse: ndarray, optional
From the unique array, the indices are used to recreate the original array. Return inverse is only available if return inverse is True.
unique_counts: ndarray, optional
In the original array, the number of times each of the unique values appears. Return counts is only offered if return counts is True.
In version 1.9.0, there is a new feature.
Let’s explore how to use this function by looking at some examples!
Example 01: Code
import numpy as np
array = ['a', 'a', 1, 1, 'b', 'b', 2, 2, 'c', 3]
new_array = np.unique(array)
print(new_array)
Output

Example 02: Code
import numpy as np
array = ['a', 'a', 1, 1, 'b', 'b', 2, 2, 'c', 3]
new_array = np.unique(array, return_inverse = True)
print(new_array)
Output

Example 03: Code
import numpy as np
array = ['a', 'a', 1, 1, 'b', 'b', 2, 2, 'c', 3]
new_array = np.unique(array, return_counts = True)
print(new_array)
Output

Conclusion
Numpy Unique is a great function to use when you want to find the unique elements of an array and return them in order. It can also be applied to other iterables like lists and tuples by passing the list keyword argument.