Table of Contents
The mode of a NumPy array is the most frequently occurring value in the array. It is calculated by finding the value that appears the most number of times in the array. If there is more than one value that occurs the most number of times, then all of those values are considered the mode of the array.
For example, consider the array [1, 2, 3, 4, 3, 2, 1]. Here, the value 1 appears twice, while 2 and 3 appear twice as well. Therefore, the mode of this array is 1, 2, and 3.
In another example, if we have the array [5, 2, 7, 9, 5, 7, 3], the value 5 appears twice, while 7 also appears twice. Hence, the mode of this array is 5 and 7.
The mode is a useful measure of central tendency, particularly for discrete or categorical data. It can be easily calculated using the NumPy library in Python, making it a valuable tool for data analysis and statistics.
Calculate the Mode of NumPy Array (With Examples)
You can use the following basic syntax to find the mode of a NumPy array:
#find unique values in array along with their counts vals, counts = np.unique(array_name, return_counts=True) #find mode mode_value = np.argwhere(counts == np.max(counts))
Recall that the mode is the value that occurs most often in an array.
Note that it’s possible for an array to have one mode or multiple modes.
The following examples show how to use this syntax in practice.
Example 1: Calculating Mode of NumPy Array with Only One Mode
The following code shows how to find the mode of a NumPy array in which there is only one mode:
import numpy as np #create NumPy array of values with only one mode x = np.array([2, 2, 2, 3, 4, 4, 5, 5, 5, 5, 7]) #find unique values in array along with their counts vals, counts = np.unique(x, return_counts=True) #find mode mode_value = np.argwhere(counts == np.max(counts)) #print list of modes print(vals[mode_value].flatten().tolist()) [5] #find how often mode occurs print(np.max(counts)) 4
From the output we can see that the mode is 5 and it occurs 4 times in the NumPy array.
Example 2: Calculating Mode of NumPy Array with Multiple Modes
The following code shows how to find the mode of a NumPy array in which there are multiple modes:
import numpy as np #create NumPy array of values with multiple modes x = np.array([2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 7]) #find unique values in array along with their counts vals, counts = np.unique(x, return_counts=True) #find mode mode_value = np.argwhere(counts == np.max(counts)) #print list of modes print(vals[mode_value].flatten().tolist()) [2, 4, 5] #find how often mode occurs print(np.max(counts)) 3
From the output we can see that this NumPy array has three modes: 2, 4, and 5.
We can also see that each of these values occurs 3 times in the array.
Additional Resources
The following tutorials explain how to perform other common operations in NumPy:
Cite this article
stats writer (2024). What is the mode of a NumPy array and how is it calculated?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-is-the-mode-of-a-numpy-array-and-how-is-it-calculated-provide-examples/
stats writer. "What is the mode of a NumPy array and how is it calculated?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/what-is-the-mode-of-a-numpy-array-and-how-is-it-calculated-provide-examples/.
stats writer. "What is the mode of a NumPy array and how is it calculated?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-is-the-mode-of-a-numpy-array-and-how-is-it-calculated-provide-examples/.
stats writer (2024) 'What is the mode of a NumPy array and how is it calculated?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-is-the-mode-of-a-numpy-array-and-how-is-it-calculated-provide-examples/.
[1] stats writer, "What is the mode of a NumPy array and how is it calculated?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. What is the mode of a NumPy array and how is it calculated?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
