In this tutorial, we will look at how to count unique values in a Numpy array with the help of some examples.

There’s no direct function (like the Pandas `nunique()`

function for a pandas series) to get the count of unique values in a Numpy array. You can, however, use the `numpy.unique()`

function to get the unique values in an array and then use the `len()`

function on the resulting unique values array to get the unique values count in the original array.

The following is the syntax –

import numpy as np # count of unique values in array ar len(np.unique(ar))

Let’s now look at some examples of using the above syntax –

## Example 1 – Count unique values in a 1d array

Let’s create a one-dimensional numpy array and get its distinct value count.

import numpy as np # create a numpy array ar = np.array([1, 2, 2, 3, 4, 5, 5, 5, 6]) # count of unique values print(len(np.unique(ar)))

Output:

6

Here, the `np.unique()`

function returns an array with only the unique values from the original array and we get the count of unique values by calculating the length of this resulting array.

You can even get the count of each unique value in the array with the `numpy.unique()`

function, refer to this tutorial.

## Example 2 – Count unique values in a 2d array

You can similarly use the above method to count unique values in a 2d array.

import numpy as np # create a numpy array arr = np.array([ [1, 1, 1], [2, 2, 3], [3, 3, 3] ]) # count of unique values print(len(np.unique(arr)))

Output:

3

We get the number of unique values in the entire 2d array.

## Example 3 – Count unique values in each row of a 2d array

You can apply the above method separately on each row (for example, using a list comprehension) to get the unique values in each row.

Let’s take the same array as above.

import numpy as np # create a numpy array arr = np.array([ [1, 1, 1], [2, 2, 3], [3, 3, 3] ]) # count of unique values in each row print([len(np.unique(row)) for row in arr])

Output:

[1, 2, 1]

We get a list with unique values in each row.

## Example 4 – Count unique values in each column of a 2d array

You can similarly use the above method at a column level as well. The idea is to transpose the original matrix, this will make the columns in the original matrix the rows in the transposed matrix and then use the same syntax as above to get the unique values in each row (column of the original matrix).

import numpy as np # create a numpy array arr = np.array([ [1, 1, 1], [2, 2, 3], [3, 3, 3] ]) # take transpose of the matrix arr_transposed = np.transpose(arr) # count of unique values in each column print([len(np.unique(row)) for row in arr_transposed])

Output:

[3, 3, 2]

We get the distinct values in each column of the original array `arr`

.

You might also be interested in –

- Numpy – Check If Matrix is an Identity Matrix
- Get unique values and counts in a numpy array
- Numpy – Check If Array has any Duplicates

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