median_grouped() function in Python statistics module
Last Updated :
30 May, 2022
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
# 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
Python3
# Python code to demonstrate 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
Python3
# Python code to demonstrate 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
Python3
# 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.
Similar Reads
median_high() function in Python statistics module
Median is often referred to as the robust measure of the central location and is less affected by the presence of outliers in data. statistics module in Python allows three options to deal with median / middle elements in a data set, which are median(), median_low() and median_high(). The high media
4 min read
median_low() function in Python statistics module
Median is often referred to as the robust measure of the central location and is less affected by the presence of outliers in data. statistics module in Python allows three options to deal with median / middle elements in a data set, which are median(), median_low() and median_high(). The low median
4 min read
mode() function in Python statistics module
The mode of a set of data values is the value that appears most frequently. In statistics, itâs often used to understand the most common or popular value within a set. A mode of a continuous probability distribution is often considered to be any value 'x' at which its probability density function ha
3 min read
statistics mean() function - Python
The mean() function from Pythonâs statistics module is used to calculate the average of a set of numeric values. It adds up all the values in a list and divides the total by the number of elements. For example, if we have a list [2, 4, 6, 8], the mean would be (2 + 4 + 6 + 8) / 4 = 5.0. This functio
4 min read
stdev() method in Python statistics module
The stdev() function in Python's statistics module is used to calculate the standard deviation of a dataset. It helps to measure the spread or variation of values in a sample. Standard deviation (SD) measures the spread of data points around the mean. A low SD indicates data points are close to the
2 min read
fmean() function in Python statistics
Statistics module of Python 3.8 provides a function fmean() that converts all the data into float data-type and then computes the arithmetic mean or average of data that is provided in the form of a sequence or an iterable. The output of this function is always a float. The only difference in comput
3 min read
sciPy stats.nanmedian() function | Python
scipy.stats.nanmedian(array, axis=0) function calculates the median by ignoring the Nan (not a number) values of the array elements along the specified axis of the array. Parameters : array : Input array or object having the elements, including Nan values, to calculate the median. axis : Axis along
2 min read
Statistical Functions in Python | Set 2 ( Measure of Spread)
Statistical Functions in Python | Set 1(Averages and Measure of Central Location) Measure of spread functions of statistics are discussed in this article. 1. variance() :- This function calculates the variance i.e measure of deviation of data, more the value of variance, more the data values are spr
2 min read
sciPy stats.gmean() function | Python
scipy.stats.gmean(array, axis=0, dtype=None) calculates the geometric mean of the array elements along the specified axis of the array (list in python). It's formula -   Parameters : array: Input array or object having the elements to calculate the geometric mean. axis: Axis along which the mean is
1 min read
sciPy stats.mean() function | Python
scipy.stats.mean(array, axis=0) function calculates the arithmetic mean of the array elements along the specified axis of the array (list in python). It's formula - Parameters : array: Input array or object having the elements to calculate the arithmetic mean. axis: Axis along which the mean is to b
1 min read