Python | Statistics Module | Mode Functions

 

Python | Statistics Module | Mode Functions


What is the Statistics module in Python?

Python provides a built-in module to facilitate the working of mathematical statistics functions for real-valued (numeric) data. This module came into existence with Python 3.4 version. The input values in the statistics functions are not required to be sorted. The module is classified as - 
  • Averages and Measures of the Central Location 
  • Measures of Spread

Today's Agenda

In this post, we will learn about the Averages and Measures of the Central Location function and their various types in Python. We will cover the following functions -
  • mode()
  • multimode()
These functions calculate the average for the entire population or sample data. You can also read about the Mean FunctionsMedian Functions and Spread Functions.

Prerequisite

This post has been prepared for the audience who : 
  1. Have access to a Linux-based system or a Windows-based system.
  2. Have Python 3 installed over their systems that can be used to run the code. Check for python version using: python --version
  3. And finally, who are eager to learn and try such useful functions.

Let's get started

1. mode() -->
Calculates the single-mode (most common value) of the discrete or nominal data. Mode serves as the measure of the central location for a dataset. In the case where there are multiple modes, the output is the first mode of the dataset. 

The mode can also be applied to non-numeric data. In the case where an empty dataset is passed, StatisticsError will be raised.

# Importing the statistics module
import statistics
from fractions import Fraction as F
from decimal import Decimal as D
 
# list of integer numbers
data1 = [1, 4, 9, 2, 7, 2,
4, 6, 3, 2, 4, 8, 5, 2, 4
a = statistics.mode(data1)

# list of float numbers
data2 = [0.25, 6.5
, 0.25, 12.25, 9.75, 0.25, 14.25
b = statistics.mode(data2)

# tuple of non-numeric data
data3 = (
"sky", "blue", "black", "clouds", "sky", "sun", "moon", "blue")
c = statistics.mode(data3)

# list of decimal numbers with an even count
data4 =
[D("0.5"), D("0.75"),D("0.5"),D("0.375")]
d = statistics.mode(data4)

# tuple of a set of fractional numbers with an even count
data5 =
[F(5,2), F(7,1), F(6,5), F(3,8), F(3,8), F(
8,4)
, F(3,8), F(7,1)]
e = statistics.mode(data5)
 
# Printing the mode
print("Mode of data1 is :", a)
print("Mode of data2 is :", b)
print("Mode of data3 is :", c)
print("Mode of data4 is :", d)
print("Mode of data5 is :", e)
OUTPUT

Mode of data1 is : 4 Mode of data2 is : 0.25 Mode of data3 is : sky Mode of data4 is : 0.5 Mode of data5 is : 3/8


2. multimode() -->
Calculates the list of modes (most frequently occurring values) of the discrete data in the order of occurrence in the dataset. As the name suggests, it will return more than one mode value. 
This function is applicable only with Python version 3.8 and above. 

In the case where an empty dataset is passed, StatisticsError will be raised. We can also use min(multimode(data)) and max(multimode(data)) to return the minimum and maximum. The syntax will be the same as the mode() function, only the function multimode() is used in place of mode().

# Importing the statistics module
import statistics
 
# list of integer numbers
data1 = [1, 4, 9, 2, 7, 2,
4, 6, 3, 2, 4, 8, 5, 2, 4
a = statistics.multimode(data1)

# list of float numbers
data2 = [0.25, 6.5
, 0.25, 12.25, 9.75, 6.5, 14.25
b = statistics.multimode(data2)

# tuple of non-numeric data
data3 = (
"sky", "blue", "black", "clouds", "sky", "sun", "moon", "blue")
c = statistics.multimode(data3)

# tuple of non-numeric data
data4 = (
'
therevisedcontextstatisticsmodulemode')
d = statistics.multimode(data4)
 
# Printing the multimode
print("Multimode of data1 is :", a)
print("Multimode of data2 is :", b)
print("Multimode of data3 is :", c)
print("Multimode of data4 is :", d)

OUTPUT

Multimode of data1 is : [4, 2] Multimode of data2 is : [0.25, 6.5] Multimode of data3 is : ['sky', 'blue'] Multimode of data4 is : ['t', 'e']




For more reference, you can visit the official documentYou can also read about the Mean Functions, Median Functions and Spread Functions.






Comments