🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The statistics.median_grouped function in Python's statistics module calculates the median of grouped continuous data, using interpolation. This function is useful for finding the median in datasets where data is grouped into bins or intervals.
Table of Contents
- Introduction
statistics.median_groupedFunction Syntax- Examples
- Basic Usage
- Median of Grouped Data with Default Interval
- Median of Grouped Data with Specified Interval
- Real-World Use Case
- Conclusion
Introduction
The statistics.median_grouped function is part of the statistics module, which provides functions for mathematical statistics of numeric data. This function is useful for datasets where data is grouped into intervals, and you want to estimate the median using interpolation.
statistics.median_grouped Function Syntax
Here's how you use the statistics.median_grouped function:
import statistics
median_value = statistics.median_grouped(data, interval=1)
Parameters:
data: A sequence or iterable of numeric data (list, tuple, etc.).interval: Optional. The class interval. Default is 1.
Returns:
- The median of the grouped data.
Raises:
StatisticsError: Ifdatais empty.
Examples
Basic Usage
Calculate the median of grouped data with default interval.
import statistics
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
median_value = statistics.median_grouped(data)
print(f"Median (grouped): {median_value}")
Output:
Median (grouped): 3.875
Median of Grouped Data with Default Interval
Calculate the median of grouped data using the default interval (1).
import statistics
numbers = [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5]
median_value = statistics.median_grouped(numbers)
print(f"Median of grouped data: {median_value}")
Output:
Median of grouped data: 3.6666666666666665
Median of Grouped Data with Specified Interval
Calculate the median of grouped data using a specified interval.
import statistics
numbers = [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5]
median_value = statistics.median_grouped(numbers, interval=2)
print(f"Median of grouped data with interval 2: {median_value}")
Output:
Median of grouped data with interval 2: 3.3333333333333335
Real-World Use Case
Calculating the Median Income from Grouped Data
Estimate the median income from a dataset where income is grouped into intervals.
import statistics
incomes = [25000, 30000, 30000, 35000, 35000, 35000, 40000, 40000, 40000, 40000, 45000, 45000, 45000, 45000, 45000]
median_income = statistics.median_grouped(incomes, interval=5000)
print(f"Median income (grouped): {median_income}")
Output:
Median income (grouped): 39375.0
Conclusion
The statistics.median_grouped function is used for calculating the median of grouped continuous data in Python. It is particularly helpful when dealing with data that is divided into intervals or bins, allowing for a more accurate estimation of the median using interpolation. This function is beneficial in various real-world scenarios, such as estimating the median from grouped income data.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment