How to count the frequency of unique values in NumPy array?
Last Updated :
02 Jan, 2024
Let's see How to count the frequency of unique values in the NumPy array. Python’s Numpy library provides a numpy.unique() function to find the unique elements and their corresponding frequency in a NumPy array.
numpy.unique() Syntax
Syntax: numpy.unique(arr, return_counts=False)
Return: Sorted unique elements of an array with their corresponding frequency counts NumPy array.
Get Unique Items and Counts in Numpy Array
There are various ways to get unique items and counts in the Numpy array here we explain some generally used methods for getting unique items and counts in the Numpy array those are following.
- Using the np.unique() Function
- Using NumPy Unique Frequency
- Using NumPy in Transpose Form
- Using
numpy.bincount()
- Using the collections.Counter() Function
Using the np.unique() Function
In this example code uses NumPy to create an array (`ini_array`) and then utilizes `np.unique()` to obtain unique values and their frequencies. The unique values are printed with "Unique Values:", and their corresponding frequencies are printed with "Frequency Values:".
Python3
# import library
import numpy as np
ini_array = np.array([10, 20, 5,
10, 8, 20,
8, 9])
# Get a tuple of unique values
# and their frequency in
# numpy array
unique, frequency = np.unique(ini_array,
return_counts = True)
# print unique values array
print("Unique Values:",
unique)
# print frequency array
print("Frequency Values:",
frequency)
Output:
Unique Values: [ 5 8 9 10 20]
Frequency Values: [1 2 1 2 2]
Using NumPy Unique Frequency
In this example code, utilizing NumPy, generates a 1D array (`ini_array`) and then employs `np.unique()` to obtain unique values and their frequencies. The results are combined into a single NumPy array (`count`) and printed, displaying the values and their frequencies.
Python3
# import library
import numpy as np
# create a 1d-array
ini_array = np.array([10, 20, 5,
10, 8, 20,
8, 9])
# Get a tuple of unique values
# and their frequency
# in numpy array
unique, frequency = np.unique(ini_array,
return_counts = True)
# convert both into one numpy array
count = np.asarray((unique, frequency ))
print("The values and their frequency are:\n",
count)
Output:
The values and their frequency are:
[[ 5 8 9 10 20]
[ 1 2 1 2 2]]
Using NumPy in Transporse Form
In this example code uses NumPy to create a 1D array (`ini_array`) and employs `np.unique()` to obtain unique values and their frequencies. The results are combined into a NumPy array (`count`), which is then transposed for a display of values and frequencies in a transposed form.
Python3
# import library
import numpy as np
# create a 1d-array
ini_array = np.array([10, 20, 5,
10, 8, 20,
8, 9])
# Get a tuple of unique values
# and their frequency in
# numpy array
unique, frequency = np.unique(ini_array,
return_counts = True)
# convert both into one numpy array
# and then transpose it
count = np.asarray((unique,frequency )).T
print("The values and their frequency are in transpose form:\n",
count)
Output:
The values and their frequency are in transpose form:
[[ 5 1]
[ 8 2]
[ 9 1]
[10 2]
[20 2]]
Using numpy.bincount()
Function
In this example, a 1D NumPy array `arr` is created with integer values. Using `numpy.unique()`, unique values and their counts are obtained. The results are displayed, showcasing the unique values and their corresponding counts using the `numpy.bincount()` method, ensuring proper alignment with the unique values.
Python3
import numpy as np
# Create a 1D array
arr = np.array([10, 20, 5, 10, 8, 20, 8, 9])
# Get unique values and their counts using bincount
unique_values = np.unique(arr)
counts = np.bincount(arr)
# Display the results
print("\nMethod 2:")
print("Unique Values:", unique_values)
print("Counts:", counts[unique_values])
Output:
Unique Values: [ 5 8 9 10 20]
Counts: [1 2 1 2 2]
Using the collections.Counter() Function
In this example The code employs `Counter` from the `collections` module to count occurrences of elements in the list `my_list`. It then prints the unique values and their respective counts, offering a concise summary of the element frequencies in the list.
Python3
from collections import Counter
# Create a list
my_list = [10, 20, 5, 10, 8, 20, 8, 9]
# Use Counter to count occurrences
counts = Counter(my_list)
# Display the results
print("Unique Values:", list(counts.keys()))
print("Counts:", list(counts.values()))
Output:
Unique Values: [10, 20, 5, 8, 9]
Counts: [2, 2, 1, 2, 1]
Similar Reads
NumPy| How to get the unique elements of an Array To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4
2 min read
How to count unique values in a Pandas Groupby object? Here, we can count the unique values in Pandas groupby object using different methods. This article depicts how the count of unique values of some attribute in a data frame can be retrieved using Pandas. Method 1: Count unique values using nunique() The Pandas dataframe.nunique() function returns a
2 min read
Find the most frequent value in a NumPy array In this article, let's discuss how to find the most frequent value in the NumPy array. Steps to find the most frequency value in a NumPy array: Create a NumPy array.Apply bincount() method of NumPy to get the count of occurrences of each element in the array.The n, apply argmax() method to get the
1 min read
How to Create Frequency Tables in Python? In this article, we are going to see how to Create Frequency Tables in Python Frequency is a count of the number of occurrences a particular value occurs or appears in our data. A frequency table displays a set of values along with the frequency with which they appear. They allow us to better unders
3 min read
Calculate the frequency counts of each unique value of a Pandas series Let us see how to find the frequency counts of each unique value of a Pandas series. We will use these methods to calculate the frequency counts of each unique value of a Pandas series. Using values_counts() to calculate the frequency of unique value Here values_counts() function is used to find the
2 min read