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 its various types in Python. We will cover the following functions -
- mean()
- fmean()
- geometric_mean()
- harmonic_mean()
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.
1. mean() -->
Calculates the arithmetic mean (average) of the 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 positive integer numbers
dataset1 = [8, 3, 9, 5, 7, 1, 2, 3, 5]
a = statistics.mean(dataset1)
# list of negative integer numbers
dataset2 = [-2, -7, -3, -5, -9, -4, -1]
b = statistics.mean(dataset2)
# tuple of mixed integer numbers
dataset3 = (-6, -1, -9, 2, 8)
c = statistics.mean(dataset3)
# list of decimal numbers
dataset4 = [D("0.5"), D("0.75"),D("0.625"),D("0.375")]
d = statistics.mean(dataset4)
# tuple of a set of fractional numbers
dataset5 = [F(5,2), F(7,1),F(6,5),F(37,8),F(3,9)]
e = statistics.mean(dataset5)
# Printing the mean
print("Mean of dataset1 is :", a)
print("Mean of dataset2 is :", b)
print("Mean of dataset3 is :", c)
print("Mean of dataset4 is :", d)
print("Mean of dataset5 is :", e)
Mean of dataset1 is : 4.777777778
Mean of dataset2 is : -4.428571428571429
Mean of dataset3 is : -1.2
Mean of dataset4 is : 0.5625
Mean of dataset5 is : 1879/600
2. fmean() -->
Calculates the floating-point arithmetic mean of the data. It is faster than the mean() function and always returns a float 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.
# Importing the statistics module
import statistics
from fractions import Fraction as F
# list of floating-point numbers
data1 = [3.5, 4.0, 5.25]
m = statistics.fmean(data1)
# set of a set of fractional numbers
data2 = (F(5,2), F(7,1),F(6,5),F(37,8),F(3,9))
n = statistics.fmean(data2)
# Printing the fmean
print("Floating Mean of data1 is :", m)print("Floating Mean of data2 is :", n)
Floating Mean of data1 is : 4.25
Floating Mean of data2 is : 3.131666666666667
3. geometric_mean() -->
Calculates the geometric mean of the data. The function returns the geometric mean of the passed arguments using the product of the values (as opposed to the arithmetic mean which uses their sum).
In the case of an empty dataset, or if the dataset contains zero, or negative values is passed, StatisticsError will be raised. This function is applicable only with Python version 3.8 and above.
# Importing the statistics module
import statistics
from decimal import Decimal as D
# list of integer numbers
data3 = [3, 28, 5,12,49]
x = statistics.geometric_mean(data3)
# tuple of a set of decimal numbers
data4 = [D("0.5"), D("0.75"),D("0.625"),D("0.375")]
y = statistics.geometric_mean(data4)
# Printing the fmean
print("Geometric Mean of data3 is :", x)print("Geometric Mean of data4 is :", y)
Geometric Mean of data3 is : 11.981889864040753
Geometric Mean of data4 is : 0.5444846468366078
4. harmonic_mean() -->
Calculates the harmonic mean of the data. It is also known as Contrary Mean. The Harmonic Mean is the reciprocal of the arithmetic mean of the reciprocals of the data. In the case where an empty dataset or dataset with negative values is passed, StatisticsError will be raised.
The harmonic mean is generally used for the average of ratios or rates (like speeds). For example, if values are 'x', 'y' for the arithmetic mean, then the harmonic mean will be - 2/(1/x + 1/y). If one of the values is zero, the result will be zero. This function is applicable only with Python version 3.6 and above.
# Importing the statistics module
import statistics
# list of integer numbers
data5 = [8, 5,12,9,1,3]
s = statistics.harmonic_mean(data5)
# dictionary of a set of numbers
data6 = {1: "abc", 2: "def", 3: "ghijk", 4: "lm"}
t = statistics.harmonic_mean(data6)
# Printing the fmean
print("Harmonic Mean of data5 is :", s)print("Harmonic Mean of data6 is :", t)
Harmonic Mean of data5 is : 3.2383808095952022
Harmonic Mean of data6 is : 1.92
Comments
Post a Comment