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.
import statistics
li = [ 1 , 2 , 3 , 3 , 2 , 2 , 2 , 1 ]
print ( "The average of list values is : " ,end = "")
print (statistics.mean(li))
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.
import statistics
li = [ 1 , 2 , 2 , 3 , 3 , 3 ]
print ( "The median of list element is : " ,end = "")
print (statistics.median(li))
print ( "The lower median of list element is : " ,end = "")
print (statistics.median_low(li))
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.
import statistics
li = [ 1 , 2 , 2 , 3 , 3 , 3 ]
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...