median_low() function in Python statistics module
Last Updated :
30 May, 2022
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 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 smaller of the two middle values is returned. Let's see how median_low() works.
Syntax : median_low( [data-set] )
Parameters : [data-set] : Takes in a list, tuple or an iterable set of numeric data.
Returntype : Returns the low median of numeric data. Low median is a member of actual data-set.
Exceptions : StatisticsError is raised when data-set is empty.
Code #1 : Working
Python3
# Python code to demonstrate the
# working of median_low()
# importing the statistics module
import statistics
# simple list of a set of integers
set1 = [1, 3, 3, 4, 5, 7]
# Note: low median will always be
# a member of the data-set.
# Print low median of the data-set
print("Low median of the data-set is % s "
% (statistics.median_low(set1)))
Output :
Low median of the data-set is 3
Code #2 : Working of median_low() and median to distinguish between them.
Python3
# Python code to demonstrate the
# working of median_low()
# 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 low median of the data-set
print("Low Median of the set is % s "
% (statistics.median_low(set1)))
Output :
Median of the set is 3.5
Low Median of the set is 3
Code #3 : Working of median_low() on a varying range of data-set
Python3
# Python code to demonstrate the
# working of median_low()
# importing statistics module
from statistics import median_low
# 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 low_median() of given data-sets
print("Low Median of data-set 1 is % s" % (median_low(data1)))
print("Low Median of data-set 2 is % s" % (median_low(data2)))
print("Low Median of data-set 3 is % s" % (median_low(data3)))
print("Low Median of data-set 4 is % s" % (median_low(data4)))
print("Low Median of data-set 5 is % s" % (median_low(data5)))
Output :
Low Median of data-set 1 is 5
Low Median of data-set 2 is 5.1
Low Median of data-set 3 is 2/3
Low Median of data-set 4 is -5
Low Median of data-set 5 is -1
Code #4 : Raising StatisticsError
Python3
# Python code to demonstrate
# StatisticsError of median_low()
# importing the statistics module
from statistics import median_low
# creating an empty data-set
empty = []
# will raise StatisticsError
print(median_low(empty))
Output :
Traceback (most recent call last):
File "/home/5f3e758236f872d014f9d741743c30a4.py", line 10, in
print(median_low(empty))
File "/usr/lib/python3.5/statistics.py", line 376, in median_low
raise StatisticsError("no median for empty data")
statistics.StatisticsError: no median for empty data
Applications : median_low() is used when the data is discrete and prefer median to be actual point in data rather than an extrapolated one.
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read