# Python statistics | median_grouped()

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 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 interval. Each data point in 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)

Parameters :
[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 :

 # Python3 code to demonstrate median_grouped()    # importing median_grouped from # the statistics module from statistics import median_grouped    # creating an simple data-set data1 = [15, 20, 25, 30, 35]    # printing median_grouped for the set print("Grouped Median of the median is %s"                 %(median_grouped(data1)))

Output :

Grouped Median of the median is 25.0

Code #2 : Working of median_grouped on a range of varying data

 # Python code to demonsrate the # working of median_grouped()    # importing statistics module from statistics import median_grouped    # tuple of a set of positive integers set1 = [2, 5, 3, 4, 8, 9]    # tuple of a set of negative integers set2 = [-6, -2, -9, -12]    # tuple of a set of positive # and negative integers set3 = [2, 4, 8, 9, -2, -3, -5, -6]    # Printing grouped median for # the given set of data print("Grouped Median of set 1 is % s" % (median_grouped(set1))) print("Grouped Median of set 2 is % s" % (median_grouped(set2))) print("Grouped Median of set 3 is % s" % (median_grouped(set3)))

Output :

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

 # Python code to demonsrate the working of # interval in median_grouped() function    # importing statistics module from statistics import median_grouped    # creating a tuple of simple data set1 = (10, 12, 13, 12, 13, 15)    # Printing median_grouped() # keeping default interval at 1 print("Grouped Median for Interval set as "\       "(default) 1 is % s" %(median_grouped(set1)))    # For interval value of 2 print("Grouped Median for Interval set as "\       "2 is % s" %(median_grouped(set1, interval = 2)))    # Now for interval value of 5 print("Grouped Median for Interval set as "\       "5 is % s" %(median_grouped(set1, interval = 5)))

Output :

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

 # Python code to demonstrate StatisticsError    # importing the statistics module import statistics    # creating an empty dataset list1 = []    # Will raise StatisticsError print(statistics.median_grouped(list1))

Output :

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

Applications :
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.

My Personal Notes arrow_drop_up

Its lonely at the top

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.