Unique values in a numpy array

Get unique values and counts in a numpy array

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

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.

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

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.

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

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# 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.

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.

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.

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.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top