In this tutorial, we will look at the numpy unique() function and its use-cases with the help of some examples.

## How to get unique values in a numpy array?

You can use the numpy unique() function to get the unique values of a numpy array. Pass the array for which you want the get the unique values as an argument. The following is the syntax:

import numpy as np # sytnax with all the default arguments ar_unique = np.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) # to just get the unique values use default parameters ar_unique = np.unique(ar)

By default, it returns a sorted array of the unique elements in the input array. It can also return three optional outputs in addition to the unique elements.

- The indices of the input array corresponding to the unique values. Pass
`True`

to the`return_index`

parameter which is`False`

by default. - The indices of the unique array that reconstruct the input array. Pass
`True`

to the`return_inverse`

parameter which is`False`

by default. - The number of times each unique value occurs in the input array. Pass
`True`

to the`return_counts`

parameter which is`False`

by default.

## Examples

Let’s look at the usage of the numpy unique function with the help of some examples.

### 1. Unique values in a numpy array

Let’s get all the unique values from a numpy array by passing just the array to the `np.unique()`

function with all the other parameters as their respective default values.

import numpy as np # create a 1d numpy array ar = np.array([3, 2, 2, 1, 0, 1, 3, 3, 3]) # get unique values in ar ar_unique = np.unique(ar) # display the returned array print(ar_unique)

Output:

[0 1 2 3]

We get an array with just the unique values in the input array. Also, note that the returned array with the unique values is sorted.

### 2. Unique values with their index in the array

Let’s also get the indices in the input array that give the unique values. Pass `True`

to the `return_index`

parameter.

# create a 1d numpy array ar = np.array([3, 2, 2, 1, 0, 1, 3, 3, 3]) # get unique values and indices ar_unique, i = np.unique(ar, return_index=True) # display the returned array print("Unique values:", ar_unique) # display the indices print("Indices:", i)

Output:

Unique values: [0 1 2 3] Indices: [4 3 1 0]

You can see that we also get the index of the first occurrence for each unique value in the input array.

### 3. Unique values with their counts in numpy array

You can also get the count for the number of times each unique value occurs in the input array. Pass `True`

to the `return_counts`

parameter.

# create a 1d numpy array ar = np.array([3, 2, 2, 1, 0, 1, 3, 3, 3]) # get unique values and counts ar_unique, i = np.unique(ar, return_counts=True) # display the returned array print("Unique values:", ar_unique) # display the counts print("Counts:", i)

Output:

Unique values: [0 1 2 3] Counts: [1 2 2 4]

We also get the counts for each of the unique values.

### 4. Unique values with reverse index

To get the indices of values in the unique array so as to reconstruct the input array from the unique values array, pass `True`

to the `return_inverse`

parameter.

# create a 1d numpy array ar = np.array([3, 2, 2, 1, 0, 1, 3, 3, 3]) # get unique values and inverse indices ar_unique, inverse_i = np.unique(ar, return_inverse=True) # display the returned array print("Unique values:", ar_unique) # display the inverse indices print("Inverse indices:", i)

Output:

Unique values: [0 1 2 3] Inverse indices: [3 2 2 1 0 1 3 3 3]

We get the reverse indices which are indices of values in the unique array that you can use to create the original input array. Let’s use these indices to reconstruct the original array from the unique values.

# reconstruct the input array print(ar_unique[i])

Output:

[3 2 2 1 0 1 3 3 3]

We get the original input array.

### 5. Get unique rows of a 2D array

You can use the `axis`

parameter to determine along which axis should the `np.unique()`

function operate. By default, it flattens the input array and returns the unique values. For example, if you use it on a 2D array with the default value for the axis parameter –

# create a 2d numpy array ar = np.array([[1, 1, 1], [0, 0, 0], [1, 1, 1]]) # get unique values in ar ar_unique = np.unique(ar) # display the returned array print(ar_unique)

Output:

[0 1]

We get a 1D array of all the unique values in the input array.

Now, if you want to get the unique rows in a 2D array, pass `axis=0`

. For example, let’s get the unique rows in the same 2D array used above:

# create a 2d numpy array ar = np.array([[1, 1, 1], [0, 0, 0], [1, 1, 1]]) # get unique rows in ar ar_unique = np.unique(ar, axis=0) # display the returned array print(ar_unique)

Output:

[[0 0 0] [1 1 1]]

You can see that now we get the unique rows in the array ar.

For more on the np.unique() function, refer to its documentation.

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**

Tutorials on numpy arrays –

- How to sort a Numpy Array?
- Create Pandas DataFrame from a Numpy Array
- Different ways to Create NumPy Arrays
- Convert Numpy array to a List – With Examples
- Append Values to a Numpy Array
- Find Index of Element in Numpy Array
- Read CSV file as NumPy Array
- Filter a Numpy Array – With Examples
- Python – Randomly select value from a list
- Numpy – Sum of Values in Array
- Numpy – Elementwise sum of two arrays
- Numpy – Elementwise multiplication of two arrays
- Using the numpy linspace() method
- Using numpy vstack() to vertically stack arrays
- Numpy logspace() – Usage and Examples
- Using the numpy arange() method
- Using numpy hstack() to horizontally stack arrays
- Trim zeros from a numpy array in Python
- Get unique values and counts in a numpy array
- Horizontally split numpy array with hsplit()