SciPy scipy.stats.mode Function

Bhuwan Bhatt Jan 30, 2023
  1. Syntax of scipy.stats.mode():
  2. Example Codes : scipy.stats.mode() Method to Find Mode With No axis Set
  3. Example Codes: Set axis=None in scipy.stats.mode() Function
  4. Example Codes: Set axis=1 in scipy.stats.mode() Function
SciPy scipy.stats.mode Function

Python Scipy scipy.stats.mode() function calculates the mode of array elements along specified axis. Mode is the most frequently observed value in the data set. If more than one item has the highest frequency in the dataset, we get the smallest value as mode.

Syntax of scipy.stats.mode():

scipy.stats.mode(a, axis=0)

Parameters

a It is the n-dimensional array whose mode is to be calculated.
axis It is an optional parameter. It is the axis along which mode is to be calculated. By default, axis=0

Return

It returns two values:

  1. An array of mode values for the n-dimensional array elements according to the axis set onto them.
  2. An array of the count of each mode value present in the n-dimensional array elements.

Example Codes : scipy.stats.mode() Method to Find Mode With No axis Set

import numpy as np
import scipy
from scipy import stats

arr = np.array([[2, 5, 6, 8], [3, 7, 3, 0], [1, 1, 4, 4], [9, 5, 0, 5], [6, 4, 2, 2]])

result = scipy.stats.mode(arr)
print("The mode of given data is:\n", result[0])
print("The frequency of mode items is:\n", result[1])

Output:

The mode of given data is:
 [[1 5 0 0]]
The frequency of mode items is:
 [[1 2 1 1]]

Here, a multi-dimensional array arr is created with 2 dimensions. The array is passed as an argument into the stats.mode function, which produces the output stored in variable result.

Since no axis parameter is defined in this condition, the mode operation takes place in the horizontal axis as default.

As we can see in the first column elements, all the elements have equal count and 1 being the smallest value, we get the mode of first column 1 with count 1. In the second column elements, 5 occurs twice, thus being the mode with count 2 and similar for the rest column elements.

The output shows two values. The first one is the ModeResult that shows an array having mode value elements, whereas the second one, count is an array that shows the count of respective mode values in given multi-dimensional data.

Example Codes: Set axis=None in scipy.stats.mode() Function

If we set axis=None in the scipy.stats.mode() function, the function calculates a single mode from the entire array.

import numpy as np
import scipy
from scipy import stats

arr = np.array([[2, 5, 6, 8], [3, 7, 3, 0], [1, 1, 4, 4], [9, 5, 0, 5], [6, 4, 2, 2]])

result = scipy.stats.mode(arr, axis=None)
print("The mode of given data is :", result[0][0])
print("The frequency of mode is :", result[1][0])

Output:

The mode of given data is : 2
The frequency of mode is : 3

Here, the output shows a single array element 2 because whenever the axis is set to None, the mode operation takes place in the entire array element, and the most frequently observed data is considered.

Example Codes: Set axis=1 in scipy.stats.mode() Function

If we set axis=1 in the scipy.stats.mode() function, the function calculates a mode for each row in the array.

import numpy as np
import scipy
from scipy import stats

arr = np.array([[2, 5, 6, 8], [3, 7, 3, 0], [1, 1, 4, 4], [9, 5, 0, 5], [6, 4, 2, 2]])

result = scipy.stats.mode(arr, axis=1)
print("The mode of given data is:\n", result[0])
print("The frequency of mode items is:\n", result[1])

Output:

The mode of given data is:
 [[2]
 [3]
 [1]
 [5]
 [2]]
The frequency of mode items is:
 [[1]
 [2]
 [2]
 [2]
 [2]]

Here, the axis is set to 1, meaning the mode operation will take on the vertical axis. Thus in the first row elements, we can see that no element repeats, and 2 being the smallest is the mode. In the second row of elements, 3 are repeated the most, thus being the mode and similar for other elements.

Related Article - SciPy Stats