How to Calculate the Mode of NumPy Array?
Last Updated :
29 May, 2025
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.mode
scipy.stats.mode function is designed specifically to find the mode of an array or dataset. It handles edge cases and is optimized for performance. This method returns not just the mode but also the count of its occurrences.
Python
from scipy import stats as st
import numpy as np
a = np.array([1, 1, 2, 2, 2, 3, 4, 5])
res = st.mode(a)
print(res.mode)
Output
2
Explanation: st.mode(a) computes the mode of the array a using scipy.stats.mode. The result is stored in res as an object.
Using np.bincount and np.argmax
np.bincount counts the occurrences of non-negative integers in an array and np.argmax helps in finding the index of the maximum value, which corresponds to the mode.
Python
import numpy as np
a = np.array([1, 1, 2, 2, 2, 3, 4, 5])
c = np.bincount(a)
m = np.argmax(c)
print(m)
Explanation:
- np.bincount(a) counts occurrences of each integer in a, returning [0, 2, 3, 1, 1, 1].
- np.argmax(c) returns the index of the highest count, which is 2, the mode.
Using collections.Counter
Counter class from the collections module counts the occurrences of elements in an iterable. By using the most_common method, we can directly retrieve the most frequent element, i.e., the mode.
Python
from collections import Counter
import numpy as np
a = np.array([1, 1, 2, 2, 2, 3, 4, 5])
c = Counter(a)
m = c.most_common(1)[0][0]
print(m)
Explanation:
- Counter(a) counts the occurrences of each element in a.
- c.most_common(1)[0][0] extracts the most frequent element (mode) from the Counter object.
Using np.unique with return_counts
np.unique returns the unique elements in the array along with their counts, which can then be used to find the mode by selecting the element with the highest count.
Python
import numpy as np
a = np.array([1, 1, 2, 2, 2, 3, 4, 5])
u, c = np.unique(a, return_counts=True)
m = u[np.argmax(c)]
print(m)
Explanation:
- np.unique(a, return_counts=True) returns unique elements and their counts.
- u[np.argmax(c)] retrieves the element with the highest count, the mode.
Related Articles:
Similar Reads
How to calculate the element-wise absolute value of NumPy array? Let's see the program for finding the element-wise absolute value of NumPy array. For doing this task we are using numpy.absolute() function of NumPy library. This mathematical function helps to calculate the absolute value of each element in the array. Syntax: numpy.absolute(arr, out = None, ufunc
2 min read
How to Convert images to NumPy array? Pictures on a computer are made of tiny dots called pixels. To work with them in Python, we convert them into numbers using a NumPy array is a table of numbers showing each pixelâs color. In this article, weâll learn how to do this using popular Python tools.Loading the images via Pillow LibraryLet
5 min read
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 fi
5 min read
How to Create Array of zeros using Numpy in Python numpy.zeros() function is the primary method for creating an array of zeros in NumPy. It requires the shape of the array as an argument, which can be a single integer for a one-dimensional array or a tuple for multi-dimensional arrays. This method is significant because it provides a fast and memory
4 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read