Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to Calculate the Mode of NumPy Array?

How to Calculate the Mode of NumPy Array?

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.

Method 1: Using scipy.stats package

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 packages
from scipy import stats as st
import numpy as np
 
# creating an array using array() method
abc = np.array([1, 1, 2, 2, 2, 3, 4, 5])
 
# applying mode operation on array and
# printing result
print(st.mode(abc))


Output :

ModeResult(mode=array([2]), count=array([3]))

Example 2:

Applying on a 2-D array

Python3




# importing required modules
import numpy as np
from scipy import stats as st
 
# creating a 2-D array using numpy package
arr = 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
# result
print(st.mode(arr))


Output :

ModeResult(mode=array([[1, 2, 2, 9, 2]]), count=array([[2, 2, 1, 2, 2]]))

Method 2: Using Statistics module

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 st
import numpy as np
 
# create an 1 d array
arr1 = np.array([9, 8, 7, 6, 6, 6, 6, 5, 5, 4,
                 3, 2, 1, 1, 1, 1, 1, 1])
 
# display the mode
print(st.mode(arr1))


Output :

1

Method 3: Using user-defined Function

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 list
lst = [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 5, 5, 5]
 
# defining a function to calculate mode. It
# takes list variable as argument
def 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 argument
print(mode(lst))


Output :

[5]

RELATED ARTICLES

Most Popular

Recent Comments