Foundations of Data Science slides 📂 Introduction · 6 of 10 36 min read

Quartiles, IQR & Outlier Detection: The Robust Toolkit

Split sorted data into four equal parts, measure the middle 50%, and let a simple fence flag what doesn't belong — no assumptions about the data's shape. This tutorial covers Q1/Q2/Q3, the IQR, the five-number summary and box plot, Tukey's 1.5×IQR outlier rule, why quartiles resist outliers where the mean caves, IQR vs z-score, how to handle outliers, and code — with animated diagrams.

📦

Quartiles, IQR & Outlier Detection

Split sorted data into four equal parts, measure the spread of the middle 50%, and let a simple fence flag the values that don't belong — all without a single assumption about the data's shape.
Quartiles IQR 1.5 × IQR Fence Outliers

Press Next → or use ← → arrow keys

Section 01

The Intuition — Runners At The Finish Line

Quarter-way, halfway, three-quarters done
Picture 100 runners crossing the finish line one by one. The 25th to finish marks the first quartile (Q1) — a quarter of the field is home. The 50th is the median (Q2), the halfway point. The 75th is Q3, with only the slowest quarter still running.

Those three checkpoints carve any sorted dataset into four equal groups. Unlike the mean, they don't care how fast the very fastest or slowest runner was — which is exactly what makes them so hard to fool.
💰
Why Not Just Use The Mean?

In a small company where juniors earn ₹32k but one CTO earns ₹380k, the mean salary balloons to ₹88,917 — a figure nobody actually earns. Quartiles ignore that single extreme and report the honest middle. When outliers lurk, quartiles tell the truth the mean can't.

Section 02 · Quartiles

Q1, Q2, Q3 — Four Equal Quarters

4 7 9 12 15 18 21 24 28 35 38 45 Q1 = 10.5 Q2 = 19.5 Q3 = 31.5 25% 25% 25% 25%
📊
Three Cut-Points, Four Groups

Q1 is the 25th percentile, Q2 the median (50th), and Q3 the 75th. Each slices off a quarter of the sorted values — so a quartile is just a percentile at a round 25% step.

Section 02 · Calculation

Computing Quartiles — Split, Then Median Each Half

🧮 Worked On [4, 7, 9, 12, 15, 18, 21, 24, 28, 35, 38, 45]
1Sort the data (already sorted here) — always the first step.
2Q2 = median of all 12 values = (18 + 21) / 2 = 19.5.
3Q1 = median of the lower half [4,7,9,12,15,18] = (9 + 12) / 2 = 10.5.
4Q3 = median of the upper half [21,24,28,35,38,45] = (28 + 35) / 2 = 31.5.
5IQR = Q3 − Q1 = 31.5 − 10.5 = 21.0.
⚠️
A Note On Methods

There are a few conventions for whether the median is included in each half, and libraries interpolate between values — so numpy.percentile may report slightly different quartiles than a hand calculation. The idea is identical; just be consistent about which method you use.

Section 03 · IQR

The IQR — Spread Of The Middle 50%

Interquartile range
IQR = Q3 − Q1
The width of the central half of the data — the "typical" band, ignoring both tails.
Why it's robust
extremes fall outside Q1…Q3
Outliers live in the outer quarters, so they can't move Q1 or Q3 — the IQR barely budges.
🛡️
Robust Where Standard Deviation Is Fragile

Standard deviation squares every deviation, so one wild value can blow it up. The IQR only looks at the middle 50%, so a lone extreme leaves it untouched. For skewed data — incomes, prices, wait times — the median + IQR pairing is the honest default summary.

Section 03 · Summary

The Five-Number Summary & Box Plot

🔢
The Five Numbers
Minimum, Q1, median (Q2), Q3, maximum — the compact fingerprint of any distribution.
📦
The Box
Spans Q1 to Q3 (the IQR), with a line inside at the median. That box is the middle 50%.
〰️
The Whiskers
Reach out to the last points inside the fences; anything past them is drawn as an outlier dot.
👁️
One Picture, Centre + Spread + Skew + Outliers

A box plot renders the five-number summary visually — you read the median, the spread (box width), the skew (median off-centre in the box), and the outliers all at a glance. It's the fastest way to compare several groups side by side.

Section 04 · The Fence

The 1.5 × IQR Rule In Action

salary (₹000s) → upper fence 154.75 380 · OUTLIER min 32 Q1 41 med Q3 86.5 IQR = 45.5
🚧
Build A Fence 1.5 IQRs Beyond Each Quartile

Lower fence = Q1 − 1.5·IQR, upper fence = Q3 + 1.5·IQR. For the salary data (Q1 = 41, Q3 = 86.5, IQR = 45.5), the upper fence is 86.5 + 1.5×45.5 = 154.75. The ₹380k CTO sails past it → flagged as an outlier, automatically.

Section 04 · Why 1.5?

Where Does The 1.5 Come From?

The multiplier isn't arbitrary. Statistician John Tukey, who invented the box plot, showed that for roughly normal data a value beyond 1.5 × IQR from the nearest quartile turns up by chance less than 0.7% of the time. Rare enough to be worth a second look, but not so strict it flags every mild bump. It's a sensible default — a guideline, not a law of nature.
🎚️
1.5 For Suspected · 3.0 For Extreme

Use 1.5 × IQR for ordinary outlier screening. When you only want the truly wild values — genuine "far outs" — widen the fence to 3 × IQR. Match the multiplier to how aggressive you want the flagging to be.

Section 05 · Robustness

One Outlier Wrecks The Mean — Not The IQR

house price → median ₹193.5k · IQR unmoved ✓ ₹2.1M mean dragged to ₹342.7k
🏠
₹342.7k vs ₹193.5k

Eleven ordinary homes plus one ₹2.1M mansion: the mean leaps to ₹342.7k — above every normal house — while the median stays at ₹193.5k and the IQR box doesn't move. Drop the outlier and the mean collapses back to ₹192.9k, proving how much it depended on that one value.

Section 06 · Comparison

IQR Fences vs Z-Score / 3σ

AspectIQR MethodZ-Score / 3σ Method
RuleBeyond Q1/Q3 ± 1.5·IQR|z| > 2 or 3
Based onQuartiles (robust)Mean & std dev (fragile)
Distribution assumptionNoneAssumes ~normal
Affected by outliers?NoYes — they inflate σ
Best forSkewed / unknown shapeClean, normal data
🧭
When Unsure, Default To IQR

The z-score method uses the very statistics (mean and σ) that outliers distort — a bad outlier can hide itself by inflating σ. The IQR makes no assumption about shape and can't be gamed that way, so it's the safer default whenever you don't know the distribution.

Section 07 · What Next?

Found An Outlier — Now What?

🗑️
Remove
Delete it — but only when it's a clear error (a typo, a sensor glitch, an impossible value).
✂️
Cap (Winsorise)
Pull the extreme value in to the fence, keeping the row while limiting its distorting pull.
🔬
Keep Separately
Report it as its own group — sometimes the outlier is the most interesting story in the data.
🚫
Never Delete Blindly

An outlier can be a data-entry error, a measurement fault, a legitimate rare event, or a genuinely fascinating anomaly — and each needs different handling. Investigate why it exists before you touch it. Silently deleting real extremes can erase exactly the signal you were hired to find (fraud, faults, breakthroughs).

Section 08 · Code

Quartiles & Fences In Python

import numpy as np

q1 = np.percentile(salaries, 25)
q3 = np.percentile(salaries, 75)
iqr = q3 - q1

lower_fence = q1 - 1.5 * iqr
upper_fence = q3 + 1.5 * iqr

outliers = salaries[(salaries < lower_fence) | (salaries > upper_fence)]

# ── pandas: flag a column in one shot ──
q1, q3 = df["salary"].quantile([0.25, 0.75])
iqr = q3 - q1
df["is_outlier"] = (df["salary"] < q1 - 1.5*iqr) | (df["salary"] > q3 + 1.5*iqr)
🐼
And describe() Gives Quartiles For Free

df.describe() already reports the 25%, 50%, and 75% rows — that's Q1, the median, and Q3. Subtract to get the IQR, and you're one line away from a full outlier screen on every numeric column.

Section 09 · Applications

Where Quartiles & IQR Earn Their Keep

💰
Income & Prices
Median + IQR is the honest summary for right-skewed money data the mean misrepresents.
🧹
Data Cleaning
The 1.5×IQR fence is the go-to first pass for catching bad rows before modelling.
🚨
Fraud & Faults
Transactions or sensor readings past the fence flag anomalies worth investigating.
📊
Comparing Groups
Side-by-side box plots reveal which group is higher, wider, or more skewed at a glance.
🩺
Lab & Health
Reference ranges and abnormal-result flags lean on percentiles and quartile fences.
📈
Percentile Reporting
"You're in the 90th percentile" — growth charts, exam scores, and SLAs all speak quartile.
Section 10 · Golden Rules

Six Rules For Quartiles & Outliers

🏅 Quartiles, IQR & Outliers, Distilled
1Always sort first. Quartiles are positions in ordered data — no sort, no quartiles.
2Pair median with IQR for skewed data — reserve mean & std dev for roughly-normal data.
3The 1.5×IQR fence is a guideline, not a law — widen to 3× for extreme-only flagging.
4Prefer IQR over z-score when the distribution is skewed or unknown — it's assumption-free.
5Investigate before removing. Understand why an outlier exists before deciding its fate.
6Draw the box plot. It shows centre, spread, skew, and outliers in a single glance.
Wrap-Up

You Can Now Fence Out Outliers

Q1·Q2·Q3Four quarters
Q3−Q1IQR · middle 50%
1.5×Tukey fence
robustIgnores extremes
5-numBox plot
investigateBefore removing
🎯
The Through-Line

Quartiles split sorted data into four equal parts; the IQR measures the middle 50% and shrugs off extremes; and a fence at 1.5×IQR beyond each quartile flags outliers without assuming any distribution. For skewed data, median + IQR + box plot is the honest, robust toolkit.

📚
Where To Go Next

Connect quartiles back to mean, median & mode and standard deviation to know when each summary fits, then move on to data visualization — where box plots, histograms, and violin plots bring all of this to life.

📦 End of tutorial · Press to review, or click Restart