Quartiles, IQR & Outlier Detection
Press Next → or use ← → arrow keys
The Intuition — Runners At The Finish Line
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.
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.
Q1, Q2, Q3 — Four Equal Quarters
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.
Computing Quartiles — Split, Then Median Each Half
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.
The IQR — Spread Of The Middle 50%
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.
The Five-Number Summary & Box Plot
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.
The 1.5 × IQR Rule In Action
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.
Where Does The 1.5 Come From?
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.
One Outlier Wrecks The Mean — Not The IQR
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.
IQR Fences vs Z-Score / 3σ
| Aspect | IQR Method | Z-Score / 3σ Method |
|---|---|---|
| Rule | Beyond Q1/Q3 ± 1.5·IQR | |z| > 2 or 3 |
| Based on | Quartiles (robust) | Mean & std dev (fragile) |
| Distribution assumption | None | Assumes ~normal |
| Affected by outliers? | No | Yes — they inflate σ |
| Best for | Skewed / unknown shape | Clean, normal data |
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.
Found An Outlier — Now What?
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).
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)
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.
Where Quartiles & IQR Earn Their Keep
Six Rules For Quartiles & Outliers
You Can Now Fence Out Outliers
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.
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