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 numpy as np ini_array = np.array([ 10 , 20 , 5 , 10 , 8 , 20 , 8 , 9 ]) unique, frequency = np.unique(ini_array, return_counts = True ) print ( "Unique Values:" , unique) 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 numpy as np ini_array = np.array([ 10 , 20 , 5 , 10 , 8 , 20 , 8 , 9 ]) unique, frequency = np.unique(ini_array, return_counts = True ) 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 numpy as np ini_array = np.array([ 10 , 20 , 5 , 10 , 8 , 20 , 8 , 9 ]) unique, frequency = np.unique(ini_array, return_counts = True ) 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 arr = np.array([ 10 , 20 , 5 , 10 , 8 , 20 , 8 , 9 ]) unique_values = np.unique(arr) counts = np.bincount(arr) 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 my_list = [ 10 , 20 , 5 , 10 , 8 , 20 , 8 , 9 ] counts = Counter(my_list) 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
How to find the Index of value in Numpy Array ?
In this article, we are going to find the index of the elements present in a Numpy array. Using where() Methodwhere() method is used to specify the index of a particular element specified in the condition. Syntax: numpy.where(condition[, x, y]) Example 1: Get index positions of a given valueHere, we
5 min read
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: C/C++ Code 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
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
3 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 v
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
How to Calculate the Mode of NumPy Array?
The goal here is to calculate the mode of a NumPy array, which refers to identifying the most frequent value in the array. For example, given the array [1, 1, 2, 2, 2, 3, 4, 5], the mode is 2, as it appears most frequently. Let's explore different approaches to accomplish this. Using scipy.stats.mod
2 min read
Python - Frequencies of Values in a Dictionary
Sometimes, while working with python dictionaries, we can have a problem in which we need to extract the frequency of values in the dictionary. This is quite a common problem and has applications in many domains including web development and day-day programming. Let's discuss certain ways in which t
4 min read
Counting Frequency of Values by Date in Pandas
Counting the frequency of values by date is a common task in time-series analysis, where we need to analyze how often certain events occur within specific time frames. Understanding these frequencies can provide valuable insights if we analyze sales data, website traffic, or any other date-related d
3 min read
Find common values between two NumPy arrays
In this article, we are going to discuss how to find out the common values between 2 arrays. To find the common values, we can use the numpy.intersect1d(), which will do the intersection operation and return the common values between the 2 arrays in sorted order. Syntax: numpy.intersect1d(arr1, arr2
2 min read