Python has a built-in library called statistics. This module provides functions for calculating mathematical statistics of numeric (Real-valued) data.
Unless explicitly noted, these functions support
Some of the most common methods supported by this module are,
Let us look at the above methods one by one with an example for each.
The first method is the mean(). This method returns the sample arithmetic mean of data which can be a sequence or iterable.
The arithmetic mean is the sum of the data divided by the number of data points. If we find are finding the average without this module we have to do some additional tasks like finding the number of elements in the iterable, finding the sum of the elements in the iterable, and then dividing them both.
The following code demonstrates how to find the mean without the statistics module.
Let us see how to find the average using the mean() method from the statistics module. We have to import the mean() method from the statistics module and pass the iterable to this method.
Note that the output is an integer. In case if the result of the mean is a float value the output will also be a float value.
This method converts data to floats and then computes the mean. This runs faster than the
mean() function and it always returns a
float. The data may be a sequence or iterable. If the input dataset is empty, raises a
This method is available in python 3.8+
Note that the same example from above yields a float value with the fmean() method.
This method will convert data to floats and compute the geometric mean.
In mathematics, the geometric mean is a mean or average, which indicates the central tendency or typical value of a set of numbers by using the product of their values (as opposed to the arithmetic mean which uses their sum).
For example, the geometric mean calculation can be easily understood with simple numbers, such as 2 and 8. If you multiply 2 and 8, then take the square root (the ½ power since there are only 2 numbers), the answer is 4.
If data is empty,
StatisticsError will be raised.
The median() method will return the median (middle value) of numeric data, using the common “mean of the middle two” method.
When the number of values is odd, the middle value is returned.
When the number of values is even, the median is interpolated by taking the average of the two middle values.
If data is empty,
StatisticsError is raised. data can be a sequence or iterable.
The median_low() will not affect the data with an odd number of values. The middle element will be returned as usual.
But, if the values are even then instead of finding the average of the middle two values it will return the smallest of the two values.
The two middle values 3 and 4 are considered. 3, which is the smaller among these two is returned.
This method is similar to the median_low() method. But, instead of returning the smallest of two, this method will return the largest of two.
The mode() method will return the most frequent data point from discrete or nominal data. The mode (when it exists) is the most typical value and serves as a measure of central location.
If there are multiple modes with the same frequency, returns the first one encountered in the data. If the input data is empty,
StatisticsError is raised.
3 is the most frequent element from the above list. The mode method also supports non-numerical data such as strings.
‘a’ is the most frequent element in the above list.
Unlike the mode() method if the list has multiple elements of the same frequency, the multimode() method will return them all as a list.
The element which is encountered first is added to the list first, the second element next, and so on. In the above example, 1 and 2 both have a frequency of 2 and they both are returned in the list.
This method also works with a list of strings.
Hope this article is helpful. Happy coding!