The statistics.median_high
function in Python's statistics
module calculates the high median of a given set of numbers. The high median is the middle value in a sorted list of numbers. If the list has an even number of elements, the high median is the higher of the two middle values.
Table of Contents
- Introduction
statistics.median_high
Function Syntax- Examples
- Basic Usage
- Median High of a List of Numbers
- Median High of a Tuple of Numbers
- Handling Odd and Even Number of Elements
- Real-World Use Case
- Conclusion
Introduction
The statistics.median_high
function is part of the statistics
module, which provides functions for mathematical statistics of numeric data. The high median is useful in cases where you need the higher middle value in a dataset, especially when the dataset contains duplicate middle values.
statistics.median_high Function Syntax
Here's how you use the statistics.median_high
function:
import statistics
median_high_value = statistics.median_high(data)
Parameters:
data
: A sequence or iterable of numeric data (list, tuple, etc.).
Returns:
- The high median of the given data.
Raises:
StatisticsError
: Ifdata
is empty.
Examples
Basic Usage
Calculate the high median of a list of numbers.
import statistics
data = [1, 2, 3, 4, 5]
median_high_value = statistics.median_high(data)
print(f"Median High: {median_high_value}")
Output:
Median High: 3
Median High of a List of Numbers
Calculate the high median of a list of integers.
import statistics
numbers = [10, 20, 30, 40, 50]
median_high_value = statistics.median_high(numbers)
print(f"Median High of numbers: {median_high_value}")
Output:
Median High of numbers: 30
Median High of a Tuple of Numbers
Calculate the high median of a tuple of floats.
import statistics
numbers = (1.5, 2.5, 3.5, 4.5, 5.5)
median_high_value = statistics.median_high(numbers)
print(f"Median High of numbers: {median_high_value}")
Output:
Median High of numbers: 3.5
Handling Odd and Even Number of Elements
Calculate the high median of lists with an odd and even number of elements.
Odd Number of Elements
import statistics
numbers = [1, 3, 3, 6, 7, 8, 9]
median_high_value = statistics.median_high(numbers)
print(f"Median High of numbers: {median_high_value}")
Output:
Median High of numbers: 6
Even Number of Elements
import statistics
numbers = [1, 2, 3, 4, 5, 6, 8, 9]
median_high_value = statistics.median_high(numbers)
print(f"Median High of numbers: {median_high_value}")
Output:
Median High of numbers: 5
Real-World Use Case
Calculating the High Median Income
Calculate the high median income from a list of incomes.
import statistics
incomes = [30000, 50000, 45000, 60000, 70000, 80000, 55000]
median_high_income = statistics.median_high(incomes)
print(f"Median High income: {median_high_income}")
Output:
Median High income: 55000
Conclusion
The statistics.median_high
function is a simple and effective way to calculate the high median of a set of numbers in Python. It is useful for finding the higher middle value of a dataset, especially when the dataset contains an even number of elements. This function makes it easy to determine the high median value, which is a common requirement in data analysis and statistics.
Comments
Post a Comment
Leave Comment