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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
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.