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
- 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 Functions, Median Functions and Spread Functions.
- mode()
- multimode()
Prerequisite
This post has been prepared for the audience who :
- Have access to a Linux-based system or a Windows-based system.
- Have Python 3 installed over their systems that can be used to run the code. Check for python version using: python --version
- And finally, who are eager to learn and try such useful functions.
- Have access to a Linux-based system or a Windows-based system.
- Have Python 3 installed over their systems that can be used to run the code. Check for python version using: python --version
- 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.
import statistics
# list of integer numbers
data1 = [1, 4, 9, 2, 7, 2, 4, 6, 3, 2, 4, 8, 5, 2, 4]
data2 = [0.25, 6.5, 0.25, 12.25, 9.75, 0.25, 14.25]
data3 = ("sky", "blue", "black", "clouds", "sky", "sun", "moon", "blue")
data4 = [D("0.5"), D("0.75"),D("0.5"),D("0.375")]
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)]
# Printing the mode
print("Mode of data1 is :", a)
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().
import statistics
# list of integer numbers
data1 = [1, 4, 9, 2, 7, 2, 4, 6, 3, 2, 4, 8, 5, 2, 4]
data2 = [0.25, 6.5, 0.25, 12.25, 9.75, 6.5, 14.25]
data3 = ("sky", "blue", "black", "clouds", "sky", "sun", "moon", "blue")
data4 = ('therevisedcontextstatisticsmodulemode')
# Printing the multimode
print("Multimode of data1 is :", a)
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 document. You can also read about the Mean Functions, Median Functions and Spread Functions.
Comments
Post a Comment