An outlier is a data point far removed from the rest of the distribution. Left untreated, outliers can distort means, standard deviations, distance calculations, and regression coefficients — sometimes dominated by a handful of extreme rows.
Detection Method 1 — IQR (Interquartile Range)
\(Q1\) and \(Q3\) are the 25th and 75th percentiles. Any value outside \([\text{lower}, \text{upper}]\) is flagged as an outlier — this is exactly the rule a boxplot's whiskers visualize.
Points beyond the whiskers (1.5×IQR past Q1/Q3) are flagged as outliers.
Detection Method 2 — Z-Score
Numerical Example
For a feature with \(Q1=50\), \(Q3=80\) (so \(IQR=30\)): lower bound \(= 50 - 1.5(30) = 5\), upper bound \(= 80 + 1.5(30) = 125\). A value of \(150\) falls outside \([5, 125]\) and is flagged.
Using z-score with \(\mu=60\), \(\sigma=10\): for \(x=95\), \(z = \frac{95-60}{10} = 3.5\) — beyond the common \(|z|>3\) threshold, flagged as an outlier.
import pandas as pd
Q1 = df["income"].quantile(0.25)
Q3 = df["income"].quantile(0.75)
IQR = Q3 - Q1
lower, upper = Q1 - 1.5 * IQR, Q3 + 1.5 * IQR
outliers = df[(df["income"] < lower) | (df["income"] > upper)]
print(len(outliers))
# Capping instead of removing (winsorizing)
df["income_capped"] = df["income"].clip(lower, upper)
What to Actually Do About an Outlier
| Approach | When to Use |
|---|---|
| Remove the row | Confirmed data entry error, and removing it doesn't lose meaningful information |
| Cap/winsorize (clip to bounds) | Legitimate extreme values you want to keep but limit their influence |
| Transform (e.g. log) | Naturally skewed data (income, prices) where outliers are real, not errors |
| Leave as-is, use a robust model | Tree-based models (Random Forest, XGBoost) are largely insensitive to outliers already |
Common Mistakes
- Automatically deleting every statistical outlier without checking whether it's a real, valid extreme value (a legitimately high-net-worth customer) vs. a data entry error.
- Computing IQR/z-score bounds on the full dataset instead of the training set, leaking test-set distribution information.
Interview Relevance
Q: "How would you detect outliers in a skewed feature like income?" IQR-based detection, not z-score — z-score assumes a roughly normal distribution, while IQR works regardless of shape and is more robust for skewed financial data.
Practice Question
A feature has \(Q1=20\), \(Q3=50\). Compute the IQR and the lower/upper outlier bounds, then determine if a value of 95 is an outlier.