🎓 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.quantiles function in Python's statistics module calculates the quantiles for a given dataset. Quantiles are points taken at regular intervals from the cumulative distribution function (CDF) of a random variable, dividing the range into continuous intervals with equal probabilities.
Table of Contents
- Introduction
statistics.quantilesFunction Syntax- Examples
- Basic Usage
- Calculating Quartiles
- Calculating Quintiles
- Calculating Deciles
- Real-World Use Case
- Conclusion
Introduction
The statistics.quantiles function is part of the statistics module, which provides functions for mathematical statistics of numeric data. Quantiles are useful for understanding the distribution and spread of data.
statistics.quantiles Function Syntax
Here's how you use the statistics.quantiles function:
import statistics
quantiles_values = statistics.quantiles(data, n=4, method='inclusive')
Parameters:
data: A sequence or iterable of numeric data (list, tuple, etc.).n: The number of intervals to divide the data into. Default is 4 (quartiles).method: The method used to compute the quantiles. Options are 'exclusive' and 'inclusive'. Default is 'exclusive'.
Returns:
- A list of quantiles.
Raises:
StatisticsError: Ifdatais empty or if there are not enough data points to calculate the quantiles.
Examples
Basic Usage
Calculate the quantiles of a list of numbers.
import statistics
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
quantiles_values = statistics.quantiles(data)
print(f"Quantiles: {quantiles_values}")
Output:
Quantiles: [2.75, 5.5, 8.25]
Calculating Quartiles
Calculate the quartiles (4 intervals) of a list of numbers.
import statistics
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
quartiles = statistics.quantiles(data, n=4)
print(f"Quartiles: {quartiles}")
Output:
Quartiles: [27.5, 55.0, 82.5]
Calculating Quintiles
Calculate the quintiles (5 intervals) of a list of numbers.
import statistics
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
quintiles = statistics.quantiles(data, n=5)
print(f"Quintiles: {quintiles}")
Output:
Quintiles: [22.0, 44.0, 66.0, 88.0]
Calculating Deciles
Calculate the deciles (10 intervals) of a list of numbers.
import statistics
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
deciles = statistics.quantiles(data, n=10)
print(f"Deciles: {deciles}")
Output:
Deciles: [11.0, 22.0, 33.0, 44.0, 55.0, 66.0, 77.0, 88.0, 99.0]
Real-World Use Case
Calculating Income Distribution
Calculate the quintiles of income data to understand the income distribution in a dataset.
import statistics
incomes = [25000, 27000, 24000, 26000, 30000, 28000, 29000, 31000, 22000, 23000, 25000, 27000]
income_quintiles = statistics.quantiles(incomes, n=5)
print(f"Income Quintiles: {income_quintiles}")
Output:
Income Quintiles: [23600.0, 25200.0, 27000.0, 29400.0]
Conclusion
The statistics.quantiles function is used for calculating quantiles in a dataset in Python. It helps in understanding the distribution and spread of data by dividing it into equal intervals. This function is beneficial in various fields, such as finance, economics, and social sciences, where data distribution analysis is essential.
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