
Introduction
Among these functions that Python’s statistics module provides is median_high(), which specifically calculates the median of a dataset, returning the higher of the two middle values in cases with an even number of observations, and the middle value if the number of observations is odd.
Let’s take a look at how to use the median_high() function.
Using the median_high() Function
To utilize the median_high() function, you first need to import the statistics module.
import statistics
The syntax for the median_high() function is straightforward:
statistics.median_high(data)
with data being am iterable sequence (like a list or tuple) of numeric values.
Let’s look at a step-by-step example using a sample dataset to illustrate how median_high() works.
# Sample dataset with an odd number of elements
data_odd = [3, 1, 4, 2, 5]
# Calculate median_high
median_high_odd = statistics.median_high(data_odd)
print(f"Median High of odd dataset: {median_high_odd}")
# Output: Median High of odd dataset: 3
In this example, the dataset is [3, 1, 4, 2, 5], which, when sorted, becomes [1, 2, 3, 4, 5]. The median is 3, since this is the middle value. No surprises here.
Now consider a dataset with an even number of elements:
# Sample dataset with an even number of elements
data_even = [3, 1, 4, 2]
# Calculate median_high
median_high_even = statistics.median_high(data_even)
print(f"Median High of even dataset: {median_high_even}")
# Output: Median High of even dataset: 3
Here, the sorted dataset is [1, 2, 3, 4]. The two middle values are 2 and 3, so the median_high() function returns 3.
If you call median_high() with an empty list, it will raise a StatisticsError. Here’s how you can handle this:
# Handling an empty list
data_empty = []
try:
median_high_empty = statistics.median_high(data_empty)
except statistics.StatisticsError as e:
print(f"Error: {e}")
# Output: Error: no median for empty data
In this example, the code attempts to calculate the median of an empty list. The except block captures the error and prints an appropriate message.
The statistics module offers other functions for calculating medians, including median() and median_low(). Here’s a brief comparison:
- median(): Returns the middle value of the dataset. In case of an even number of elements, it returns the average of the two middle values.
- median_low(): Returns the lower of the two middle values when the dataset has an even number of elements.
Here’s an example demonstrating the differences:
# Sample dataset
data_example = [3, 1, 4, 2]
# Find medians
median_value = statistics.median(data_example)
median_low_value = statistics.median_low(data_example)
median_high_value = statistics.median_high(data_example)
print(f"Median: {median_value}")
print(f"Median Low: {median_low_value}")
print(f"Median High: {median_high_value}")
# Output:
# Median: 2.5
# Median Low: 2
# Median High: 3
In this case, for the dataset [3, 1, 4, 2], the outputs will be:
- Median: 2.5 (average of 2 and 3)
- Median Low: 2
- Median High: 3
Wrapping Up
Here we explored the use of the statistics.median_high() function in Python. For further reading on Python’s statistical functions, consider exploring the official Python documentation or resources on data analysis libraries like NumPy or Pandas, which also offer rich functionalities for statistical calculations.
