Python statistics quantiles()

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

  1. Introduction
  2. statistics.quantiles Function Syntax
  3. Examples
    • Basic Usage
    • Calculating Quartiles
    • Calculating Quintiles
    • Calculating Deciles
  4. Real-World Use Case
  5. 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: If data is 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.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare