Statistical Functions in Python | Set 1 (Average and Center Position Measure)

Important mean and measure of central location features :

1. mean () : — This function returns the average or average value of the data passed in its arguments. If the passed argument is empty, StatisticsError is thrown.

2. mode () : — this function returns a maximum number of occurrences . If the passed argument is empty, StatisticsError is raised.

# Python code to demonstrate how it works
# mean () and mode ()

 
# import statistics for processing statistical operations

import statistics

 
# initializing list

li = [ 1 , 2 , 3 , 3 , 2 , 2 , 2 , 1 ]

  
# using mean () to average the list items

print ( "The average of list values ​​is:" , end = "")

print (statistics.mean (li))

 
# using mode () to print the maximum number of list items

print ( "The maximum occurring element is :" , end = " ")

print (statistics.mode (li))

Exit:

 The average of list values ​​is: 2.0 The maximum occurring element is: 2 

3. median () : — this function is used to calculate the median, that is, the average item. If the passed argument is empty, StatisticsError is called.

4. median_low () : — This function returns the median of the data if the number of items is odd, but if the number of items is even, it returns the bottom of the middle two items. If the passed argument is empty, StatisticsError is thrown.

5. median_high () : — this function returns the median of the data for an odd number of elements, but for an even number of elements, returns the highest of the middle two elements. If the passed argument is empty, StatisticsError is raised.

# Python code to demonstrate how it works
# median (), median_low () and median_high ()

 
# import statistics for statistical processing

import statistics

 
# initializing list

li = [ 1 , 2 , 2 , 3 , 3 , 3 ]

 
# using median () to display the median of list items

print ( "The median of list element is:" , end = "")

print (statistics.median (li))

 
# using median_low () to print the low median of list items

print ( "The lower median of list element is:" , end = " ")

print (statistics.median_low (li))

  
# using median_high () to print the average median of list items

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 calculate the median of a group, that is, the 50th percentile of the data. If the passed argument is empty, StatisticsError is raised.

# Python code to demonstrate how it works
# median_grouped ()

 
# import statistics for statistical processing

import statistics

 
# initializing list

li = [ 1 , 2 , 2 , 3 , 3 , 3 ]

 
# using median_grouped () to calculate the 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 (Distribution Measure)

This article courtesy of Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.