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)
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 percentile.
Exceptions : StatisticsError is raised when iterable passed is empty or when list is null.
Code #1 :
Python3
from statistics import median_grouped
data1 = [ 15 , 20 , 25 , 30 , 35 ]
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
Python3
from statistics import median_grouped
set1 = [ 2 , 5 , 3 , 4 , 8 , 9 ]
set2 = [ - 6 , - 2 , - 9 , - 12 ]
set3 = [ 2 , 4 , 8 , 9 , - 2 , - 3 , - 5 , - 6 ]
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
Python3
from statistics import median_grouped
set1 = ( 10 , 12 , 13 , 12 , 13 , 15 )
print ( "Grouped Median for Interval set as " \
"(default) 1 is % s" % (median_grouped(set1)))
print ( "Grouped Median for Interval set as " \
"2 is % s" % (median_grouped(set1, interval = 2 )))
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
Python3
import statistics
list1 = []
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 May, 2022
Like Article
Save Article