Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #406

Outlier Treatment

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)

\[ IQR = Q3 - Q1, \qquad \text{lower bound} = Q1 - 1.5 \times IQR, \qquad \text{upper bound} = Q3 + 1.5 \times IQR \]

\(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.

lower bound Q1 median Q3 upper bound outlier

Points beyond the whiskers (1.5×IQR past Q1/Q3) are flagged as outliers.

Detection Method 2 — Z-Score

\[ z = \frac{x - \mu}{\sigma}, \qquad \text{flag as outlier if } |z| > 3 \]

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

ApproachWhen to Use
Remove the rowConfirmed 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 modelTree-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.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →