AppDividend
Latest Code Tutorials

np.unique: How to Get Unique Values in NumPy Array Python

The np.unique() is a numpy library function that returns all the unique elements in the array. The returned array does not contain duplicate elements. The np.unique() function can be used to remove duplicates. It returns all the elements only one time. The unique() function has other parameters like the return index. It returns the index of the unique elements.

Syntax

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

Arguments

The np.unique() function takes one required argument as a parameter and four arguments as optional arguments:

  1. arr: The array is passed in this argument. From this array, the function returns the unique elements. This array is the required argument for returning the unique elements. This is because we cannot return the unique elements without an array.
  2. return_index: This argument takes only Boolean values as parameters. It has two Boolean statements: True and the other is False. If the value is True, then the np.unique() function returns two values: the unique array and the indices of that unique element. If False, it returns the unique array alone.
  3. return_inverse: This argument takes only Boolean values as parameters. This has two Boolean statements: True and the other is False. If the value is True, then this function returns two values: the unique array and the indices of the original array elements in the unique array elements. And if false, it returns the array alone.
  4. return_counts: This argument takes only Boolean values as parameters. This has two Boolean statements: True and the other is False. If the value is True, this function returns two values: the unique array and the other is the count of each unique element in the original array. If false, it returns the array alone.
  5. axis: This specifies the axis in which the unique elements are returned. By default, this is set to None.

Return value

It returns an array.  This array consists of all the unique elements from the original array. If return_index, return_inverse, or return_counts are passed True in the arguments, then the np.unique() function returns an original array, and the array for the arguments is passed as True.

Python program for finding unique elements from array using np.unique

# Importing numpy as np
import numpy as np

# Creating an numpy array
arr = np.array([5, 6, 7, 5, 7, 8, 3, 4, 3, 3])

# Finding the unique values using the unique function
res = np.unique(arr)
print(res)

Output

[3 4 5 6 7 8]

We imported a numpy package consisting of functions for numerical calculations into this program.

We created a numpy array using the np.array() function in the next step. This created array consists of duplicate values. Then, we passed an array into the np.unique() function.

The np.unique() function returns all the unique elements from the array. It returns all the elements from the array only once. In this example, 5 is present in two places, but the np.array() function returns only once.

The np.unique() function returns the unique values in the ascending order. We have passed the array as the argument to the function.

Program for finding unique elements from the array by passing the arguments inside the np.unique() function

# Importing numpy as np
import numpy as np

# Creating an numpy array
arr = np.array([5, 6, 7, 5, 7, 8, 3, 4, 3, 3])

# Finding the unique values using the unique function
uni, index = np.unique(arr, return_index=True)
print(" The unique array is: ", uni, " and the indices are ", index)
uni, inver = np.unique(arr, return_inverse=True)
print(" The unique array is: ", uni, " and the inverses are ", inver)
uni, index, inver, count = np.unique(arr, return_index=True, return_inverse=True, return_counts=True)
print(" The unique array is: ", uni, " and the indices are ", index, " and the inverses are ", inver, " counts are: ", count)

Output

The unique array is: [3 4 5 6 7 8] and the indices are [6 7 0 1 2 5]
The unique array is: [3 4 5 6 7 8] and the inverses are [2 3 4 2 4 5 0 1 0 0]
The unique array is: [3 4 5 6 7 8] and the indices are [6 7 0 1 2 5] and the inverses are [2 3 4 2 4 5 0 1 0 0] counts are: [3 1 2 1 2 1]

This program passed the parameters for return_index, return_inverse, and return_counts. The return_index argument returns the index of the unique elements.

In the return_inverse argument, the elements from the original array are compared with the unique array index. For example, the first element of the original array is 5. This 5 is present in the 2 indexes of the unique elements; hence 2 is returned as the index.

That’s it for this np.unique() function.

Related posts

np.all

np.sin

np.hstack

np.arange

np.zeros

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.