How to Count the Frequency of Unique Values in NumPy Array?

Analyzing the frequency of unique values within a NumPy array is a common task in data analysis. It provides valuable insights into the distribution and occurrence of elements, enabling effective data exploration and preprocessing. In this article, we will explore various methods to count the frequency of unique values in NumPy arrays using built-in NumPy functions and external libraries.

Method 1: Using np.unique() Function

NumPy provides the np.unique() function, which returns the sorted unique elements of an array. By specifying the return_counts=True parameter, it also returns the count of each unique element ?

import numpy as np

# Create a NumPy array
arr = np.array([1, 2, 3, 2, 4, 1, 3, 4, 4, 4])

# Get the unique values and their counts
unique_values, counts = np.unique(arr, return_counts=True)

# Print the results
for value, count in zip(unique_values, counts):
    print(f"{value} occurs {count} times")

The output of the above code is ?

1 occurs 2 times
2 occurs 2 times
3 occurs 2 times
4 occurs 4 times

By using the np.unique() function, we obtain two arrays: unique_values containing the unique elements and counts containing the corresponding frequencies. We iterate over these arrays simultaneously using the zip() function.

Method 2: Using collections.Counter() Function

The collections.Counter() function from Python's standard library provides an efficient way to count the occurrences of elements in a collection ?

import numpy as np
from collections import Counter

# Create a NumPy array
arr = np.array([1, 2, 3, 2, 4, 1, 3, 4, 4, 4])

# Count the frequency of unique values
counts = Counter(arr)

# Print the results
for value, count in counts.items():
    print(f"{value} occurs {count} times")

The output of the above code is ?

1 occurs 2 times
2 occurs 2 times
3 occurs 2 times
4 occurs 4 times

The Counter(arr) creates a dictionary-like object with unique values as keys and their frequencies as values.

Method 3: Using np.bincount() Function

The np.bincount() function counts the frequency of non-negative integers in a NumPy array. It creates a new array where indices represent unique values and elements represent their frequencies ?

import numpy as np

# Create a NumPy array
arr = np.array([1, 2, 3, 2, 4, 1, 3, 4, 4, 4])

# Get the frequency of unique values
counts = np.bincount(arr)

# Print the results
for value, count in enumerate(counts):
    if count > 0:
        print(f"{value} occurs {count} times")

The output of the above code is ?

1 occurs 2 times
2 occurs 2 times
3 occurs 2 times
4 occurs 4 times

The np.bincount() function creates an array where the index corresponds to the value and the element at that index is the count. We use enumerate() to iterate through indices and print values with non-zero counts.

Method 4: Using pandas.value_counts() Function

The pandas.value_counts() function returns a Series object containing unique values as indices and their corresponding frequencies as values ?

import numpy as np
import pandas as pd

# Create a NumPy array
arr = np.array([1, 2, 3, 2, 4, 1, 3, 4, 4, 4])

# Convert the array to Series
series = pd.Series(arr)

# Get the frequency of unique values
counts = series.value_counts()

# Print the results
for value, count in counts.items():
    print(f"{value} occurs {count} times")

The output of the above code is ?

4 occurs 4 times
1 occurs 2 times
3 occurs 2 times
2 occurs 2 times

Converting the NumPy array to a pandas Series allows us to use the value_counts() function. Note that pandas.value_counts() returns results sorted by frequency in descending order.

Comparison

Method Best For Output Order Dependencies
np.unique() General purpose Sorted by value NumPy only
collections.Counter() Dictionary-like access Insertion order Standard library
np.bincount() Non-negative integers By value index NumPy only
pandas.value_counts() Integration with pandas By frequency (desc) Pandas required

Conclusion

We explored four methods for counting unique value frequencies in NumPy arrays: np.unique(), collections.Counter(), np.bincount(), and pandas.value_counts(). Choose np.unique() for general use, np.bincount() for non-negative integers, and pandas.value_counts() when working with pandas workflows.

Updated on: 2026-03-27T09:38:31+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements