Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
Last Updated :
10 Feb, 2020
Python has the ability to manipulate some statistical data and calculate results of various statistical operations using the file "
statistics", useful in domain of mathematics.
Important Average and measure of central location functions :
1. mean() :- This function returns the
mean or average of the data passed in its arguments. If passed argument is empty,
StatisticsError is raised.
2. mode() :- This function returns the
number with maximum number of occurrences. If passed argument is empty,
StatisticsError is raised.
Python
# Python code to demonstrate the working of
# mean() and mode()
# importing statistics to handle statistical operations
import statistics
# initializing list
li = [1, 2, 3, 3, 2, 2, 2, 1]
# using mean() to calculate average of list elements
print ("The average of list values is : ",end="")
print (statistics.mean(li))
# using mode() to print maximum occurring of list elements
print ("The maximum occurring element is : ",end="")
print (statistics.mode(li))
Output:
The average of list values is : 2.0
The maximum occurring element is : 2
3. median() :- This function is used to calculate the median, i.e
middle element of data. If passed argument is empty,
StatisticsError is raised.
4. median_low() :- This function returns the median of data in case of odd number of elements, but in case of even number of elements, returns the
lower of two middle elements. If passed argument is empty,
StatisticsError is raised.
5. median_high() :- This function returns the median of data in case of odd number of elements, but in case of even number of elements,
returns the higher of two middle elements. If passed argument is empty,
StatisticsError is raised.
Python
# Python code to demonstrate the working of
# median(), median_low() and median_high()
# importing statistics to handle statistical operations
import statistics
# initializing list
li = [1, 2, 2, 3, 3, 3]
# using median() to print median of list elements
print ("The median of list element is : ",end="")
print (statistics.median(li))
# using median_low() to print low median of list elements
print ("The lower median of list element is : ",end="")
print (statistics.median_low(li))
# using median_high() to print high median of list elements
print ("The higher median of list element is : ",end="")
print (statistics.median_high(li))
Output:
The median of list element is : 2.5
The lower median of list element is : 2
The higher median of list element is : 3
6. median_grouped() :- This function is used to compute group median, i.e
50th percentile of the data. If passed argument is empty,
StatisticsError is raised.
Python
# Python code to demonstrate the working of
# median_grouped()
# importing statistics to handle statistical operations
import statistics
# initializing list
li = [1, 2, 2, 3, 3, 3]
# using median_grouped() to calculate 50th percentile
print ("The 50th percentile of data is : ",end="")
print (statistics.median_grouped(li))
Output:
The 50th percentile of data is : 2.5
Statistical Functions in Python | Set 2 ( Measure of Spread)
Similar Reads
Statistical Functions in Python | Set 2 ( Measure of Spread) Statistical Functions in Python | Set 1(Averages and Measure of Central Location) Measure of spread functions of statistics are discussed in this article. 1. variance() :- This function calculates the variance i.e measure of deviation of data, more the value of variance, more the data values are spr
2 min read
median_low() function in Python statistics module Median is often referred to as the robust measure of the central location and is less affected by the presence of outliers in data. statistics module in Python allows three options to deal with median / middle elements in a data set, which are median(), median_low() and median_high(). The low median
4 min read
median_high() function in Python statistics module Median is often referred to as the robust measure of the central location and is less affected by the presence of outliers in data. statistics module in Python allows three options to deal with median / middle elements in a data set, which are median(), median_low() and median_high(). The high media
4 min read
fmean() function in Python statistics Statistics module of Python 3.8 provides a function fmean() that converts all the data into float data-type and then computes the arithmetic mean or average of data that is provided in the form of a sequence or an iterable. The output of this function is always a float. The only difference in comput
3 min read
mode() function in Python statistics module The mode of a set of data values is the value that appears most frequently. In statistics, itâs often used to understand the most common or popular value within a set. A mode of a continuous probability distribution is often considered to be any value 'x' at which its probability density function ha
3 min read
Find average of a list in python We are given a list of numbers and the task is to find the average (or mean) value of its elements. For example, if we have a list nums = [10, 20, 30, 40], the sum of the elements is 10 + 20 + 30 + 40, which equals 100. The number of elements in the list is 4. So, the average is calculated as 100 /
2 min read