0
0
SciPydata~5 mins

Percentiles and quantiles in SciPy

Choose your learning style9 modes available
Introduction

Percentiles and quantiles help us understand how data is spread out. They show the value below which a certain percent of data falls.

To find the score below which 90% of students scored in a test.
To understand the distribution of house prices in a city.
To detect outliers by checking extreme percentiles in sales data.
To divide customers into groups based on spending habits.
To summarize large datasets with key cutoff points.
Syntax
SciPy
from scipy import stats

# Calculate the percentile
percentile_value = stats.scoreatpercentile(data, percentile)

# Calculate quantiles
quantiles = stats.mstats.mquantiles(data, prob=[q1, q2, ...])

data is your list or array of numbers.

percentile is a number between 0 and 100.

Examples
Finds the 50th percentile (median) of the data.
SciPy
from scipy import stats

data = [10, 20, 30, 40, 50]
value_50th = stats.scoreatpercentile(data, 50)
Finds the 25th, 50th, and 75th percentiles (quartiles) of the data.
SciPy
from scipy import stats

data = [5, 15, 25, 35, 45]
quantiles = stats.mstats.mquantiles(data, prob=[0.25, 0.5, 0.75])
Sample Program

This program calculates key percentiles of exam scores to understand score distribution.

SciPy
from scipy import stats

# Sample data: exam scores
scores = [55, 70, 65, 90, 85, 75, 60, 80, 95, 50]

# Find the 25th, 50th, and 75th percentiles
p25 = stats.scoreatpercentile(scores, 25)
p50 = stats.scoreatpercentile(scores, 50)
p75 = stats.scoreatpercentile(scores, 75)

print(f"25th percentile: {p25}")
print(f"50th percentile (median): {p50}")
print(f"75th percentile: {p75}")
OutputSuccess
Important Notes

Percentiles divide data into 100 equal parts.

Quantiles are general terms for dividing data into equal parts, like quartiles (4 parts) or deciles (10 parts).

Use percentiles to quickly find thresholds in your data.

Summary

Percentiles show the value below which a percentage of data falls.

Use stats.scoreatpercentile to find specific percentiles.

Quantiles divide data into equal groups and can be found with stats.mstats.mquantiles.