Open In App

How to Count the Frequency of Unique Values in NumPy Array

Last Updated : 22 Sep, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

In NumPy, you can efficiently determine the unique elements of an array and count how many times each element appears. This is useful for analyzing the distribution of values in numerical or categorical datasets.

Let’s explore different methods to count the frequency of unique values in a NumPy array with examples.

Using np.unique()

np.unique() is widely used method to find unique elements in an array along with their counts. It works for any numeric or string array.

In this example, this code creates a NumPy array of integers and counts the unique values along with their frequency.

Python
import numpy as np
arr = np.array([3, 7, 3, 1, 7, 9, 3])
unique, counts = np.unique(arr, return_counts=True)
print("Unique Values:", unique)
print("Counts:", counts)

Output
Unique Values: [1 3 7 9]
Counts: [1 3 2 1]

Explanation:

  • code identifies [1, 3, 7, 9] as unique numbers.
  • counts shows how many times each unique value appears in arr.

Using np.bincount()

np.bincount() counts occurrences of non-negative integers efficiently. It is faster than np.unique() for large integer arrays but cannot handle negative numbers or floats.

Here, this code uses np.bincount() to calculate the frequency of elements in an integer array.

Python
import numpy as np
arr = np.array([0, 2, 2, 1, 4, 0, 2])
unique = np.unique(arr)
counts = np.bincount(arr)
print("Unique Values:", unique)
print("Counts:", counts[unique])

Output
Unique Values: [0 1 2 4]
Counts: [2 1 3 1]

Explanation:

  • np.bincount(arr) counts occurrences of all integers from 0 to max value in arr.
  • Indexing counts with unique ensures alignment of counts with the corresponding unique values.

Using np.unique() in Combined Array

You can combine the unique values and their counts into a single NumPy array for better visualization.

In this example, this program merges unique elements and their counts into a single NumPy array.

Python
import numpy as np
arr = np.array([5, 2, 5, 3, 2, 4])
unique, counts = np.unique(arr, return_counts=True)
result = np.asarray((unique, counts))
print("Values and Frequencies:\n", result)

Output
Values and Frequencies:
 [[2 3 4 5]
 [2 1 1 2]]

Explanation:

  • unique and counts are stacked together in one array.
  • The first row shows unique elements and the second row shows their frequency.

Using Transposed Array

You can transpose the combined array to present the value-count pairs in a more readable column format.

Here, this code transposes the combined array to show [value, count] pairs clearly.

Python
import numpy as np
arr = np.array([8, 1, 8, 2, 1, 9])
unique, counts = np.unique(arr, return_counts=True)
result = np.asarray((unique, counts)).T
print("Values and Frequencies in Transpose Form:\n", result)

Output
Values and Frequencies in Transpose Form:
 [[1 2]
 [2 1]
 [8 2]
 [9 1]]

Explanation: Each row shows [value, frequency], making it easy to read and understand.

Using collections.Counter()

Counter from Python's collections module counts element occurrences in any iterable. It is easy to use but slower for very large arrays compared to NumPy methods.

In this example, this program uses Counter to count unique elements and display the results.

Python
from collections import Counter
arr = [10, 15, 10, 5, 15, 20]
counts = Counter(arr)
print("Unique Values:", list(counts.keys()))
print("Counts:", list(counts.values()))

Output
Unique Values: [10, 15, 5, 20]
Counts: [2, 2, 1, 1]

Explanation:

  • Counter creates a dictionary-like object where keys are unique values.
  • Values represent how many times each unique element appears in arr.

Explore