Open In App

median_high() function in Python statistics module

Last Updated : 30 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 median is always a member of the data set. When the number of data points is odd, the middle value is returned. When it is even, the larger of the two middle values is returned. Let’s see how median_high() function works.

Syntax : median_high( [data – set] ) 

Parameters : [data-set] : Takes in a list, or an iterable set of numeric data. 

Returntype : Returns the high median of the numeric data (always in actual data-set). 

Exceptions : StatisticsError is raised when data-set is empty.

Code #1 : Working 

Python




# Python code to demonstrate working of
# median_high() on a data-set
 
# importing the statistics module
import statistics
 
# simple list of a set of integers
set1 = [1, 3, 2, 8, 5, 4]
 
# Print high median of the data-set
print("High median of the data-set is %s "
        % (statistics.median_high(set1)))


Output :

High median of the data-set is 4 

  Code #2 : Working of median_high() and median() to demonstrate the difference between them. 

Python3




# Working of median_high() and median() to
# demonstrate the difference between them.
 
# importing the statistics module
import statistics
 
# simple list of a set of integers
set1 = [1, 3, 3, 4, 5, 7]
 
# Print median of the data-set
 
# Median value may or may not
# lie within the data-set
print("Median of the set is %s"
      % (statistics.median(set1)))
 
# Print high median of the data-set
print("High Median of the set is %s "
      % (statistics.median_high(set1)))


Output :

Median of the set is 3.5
High Median of the set is 4 

  Code #3 : Working of median_high() on a varying range of data-values. 

Python3




# Python code to demonstrate the
# working of median_high()
 
# importing statistics module
from statistics import median_high
 
# Importing fractions module as fr
from fractions import Fraction as fr
 
# tuple of positive integer numbers
data1 = (2, 3, 4, 5, 7, 9, 11)
 
# tuple of a set of floating-point values
data2 = (2.4, 5.1, 6.7, 8.9)
 
# tuple of a set of fractional numbers
data3 = (fr(1, 2), fr(44, 12),
         fr(10, 3), fr(2, 3))
 
# tuple of a set of negative integers
data4 = (-5, -1, -12, -19, -3)
 
# tuple of set of positive
# and negative integers
data5 = (-1, -2, -3, -4, 4, 3, 2, 1)
 
# Print the high_median() of the given data-sets
print("High Median of data-set 1 is %s" % (median_high(data1)))
print("High Median of data-set 2 is %s" % (median_high(data2)))
print("High Median of data-set 3 is %s" % (median_high(data3)))
print("High Median of data-set 4 is %s" % (median_high(data4)))
print("High Median of data-set 5 is %s" % (median_high(data5)))


Output :

High Median of data-set 1 is 5
High Median of data-set 2 is 6.7
High Median of data-set 3 is 10/3
High Median of data-set 4 is -5
High Median of data-set 5 is 1

  Code #4 : Demonstration of StatisticsError 

Python3




# Python code to demonstrate
# StatisticsError of median_high()
 
# importing the statistics module
from statistics import median_high
 
# creating an empty data-set
empty = []
 
# will raise StatisticsError
print(median_high(empty))


Output :

Traceback (most recent call last):
  File "/home/fc2eae1616bfaa0987b261d9d40f4602.py", line 10, in 
    print(median_high(empty))
  File "/usr/lib/python3.5/statistics.py", line 398, in median_high
    raise StatisticsError("no median for empty data")
statistics.StatisticsError: no median for empty data

  Applications : The high median is used only when the data is discrete and the prefer the median to be an actual median rather than an interpolated set of data.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads