How to Calculate the Mode of NumPy Array? - GeeksforGeeks

12 Jan, 2022
In this article, we will discuss how to calculate the mode of the Numpy Array.
Mode refers to the most repeating element in the array. We can find the mode from the NumPy array by using the following methods.
Let us see the syntax of the mode() function 
Syntax :
variable = stats.mode(array_variable)
Note : To apply mode we need to create an array. In python, we can create an array using numpy package. So first we need to create an array using numpy package and apply mode() function on that array. Let us see examples for better understanding.
Example 1:
Applying on 1-D array
Python3
# importing required packagesfrom scipy import stats as stimport numpy as np # creating an array using array() methodabc = np.array([1, 1, 2, 2, 2, 3, 4, 5]) # applying mode operation on array and# printing resultprint(st.mode(abc))
Output :
ModeResult(mode=array([2]), count=array([3]))
Example 2:
Applying on a 2-D array
Python3
# importing required modulesimport numpy as npfrom scipy import stats as st # creating a 2-D array using numpy packagearr = np.array([[1, 2, 3, 4, 5],                [1, 2, 2, 2, 2],                [4, 5, 7, 9, 4],                [6, 7, 8, 9, 2],                [2, 3, 4, 8, 6]]) # applying mode operation and printing the# resultprint(st.mode(arr))
Output :
ModeResult(mode=array([[1, 2, 2, 9, 2]]), count=array([[2, 2, 1, 2, 2]]))
Like NumPy module, the statistics module also contains statistical functions like mean , median , mode....etc . So let us see an example of a mode using the statistics module.
Example :
Python3
import statistics as stimport numpy as np # create an 1 d arrayarr1 = np.array([9, 8, 7, 6, 6, 6, 6, 5, 5, 4,                 3, 2, 1, 1, 1, 1, 1, 1]) # display the modeprint(st.mode(arr1))
Output :
1
Here we are not using any predefines functions for getting mode of a series. Let us see an example with demonstrates how to calculate mode without predefined functions.
Example :
Python3
# creating a listlst = [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 5, 5, 5] # defining a function to calculate mode. It# takes list variable as argumentdef mode(lst):         # creating a dictionary    freq = {}    for i in lst:               # mapping each value of list to a        # dictionary        freq.setdefault(i, 0)        freq[i] += 1             # finding maximum value of dictionary    hf = max(freq.values())         # creating an empty list    hflst = []         # using for loop we are checking for most    # repeated value    for i, j in freq.items():        if j == hf:            hflst.append(i)                 # returning the result    return hflst # calling mode() function and passing list# as argumentprint(mode(lst))
Output :
[5]
germanshephered48
simmytarika5
Picked
Python numpy-Linear Algebra
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Defaultdict in Python
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()