Coming to Statistical functions, median of a data-set is the measure of robust central tendency, which is less affected by the presence of outliers in data. As seen previously, medians of an ungrouped data-set using median(), median_high(), median_low() functions.
Python gives the option to calculate the median of grouped and continuous data function as well and this is the best part about this robust and convenient language.
median_grouped() function under the Statistics module, helps to calculate median value from a set of continuous data.
The data are assumed to be grouped into intervals of width intervals. Each data point in the array is the midpoint of the interval containing the true value. The median is calculated by interpolation within the median interval (the interval containing the median value), assuming that the true values within that interval are distributed uniformly :
median = L + interval * (N / 2 - CF) / FL = lower limit of the median interval N = total number of data points CF = number of data points below the median interval F = number of data points in the median interval
Syntax :median_grouped( [data-set], interval)
[data-set] : List or tuple or an iterable with a set of numeric values.
interval (1 by default) : Determines the width of grouped data and changing. It will also change the interpolation of calculated median.
Returntype :Return the median of grouped continuous data, calculated as 50th perecentile.
Exceptions :StatisticsError is raised when iterable passed is empty or when list is null.
Code #1 :
Grouped Median of the median is 25.0
Code #2 : Working of median_grouped on a range of varying data
Grouped Median of set 1 is 4.5 Grouped Median of set 2 is -6.5 Grouped Median of set 3 is 1.5
Code #3 : Working of interval
Grouped Median for Interval set as (default) 1 is 12.5 Grouped Median for Interval set as 2 is 12.0 Grouped Median for Interval set as 5 is 10.5 Grouped Median for Interval set as 10 is 8.0
Note : Observe a pattern that as the interval value is increased the median value decreases.
Code #4 : Demonstrates StatisticsError
Traceback (most recent call last): File "/home/0990a4a3f5206c7cd12a596cf82a1587.py", line 10, in print(statistics.median_grouped(list1)) File "/usr/lib/python3.5/statistics.py", line 431, in median_grouped raise StatisticsError("no median for empty data") statistics.StatisticsError: no median for empty data
Grouped Median has all the same applications as median. It is commonly used in calculations where large number of data is involved like banking and finance. It is an essential part of statistics which is the most powerful tool in data calculation.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.