Stop 06 · Data Preparation & Feature EngineeringTopics · 2/10

Outlier Detection

Part of the Machine Learning Roadmap

In short

One wrong entry, like a 900-minute delivery, can drag your averages and confuse your model. The skill is spotting outliers, then deciding whether each is an error or a real rare event.

Outliers are values far from the rest, caused by data errors or genuinely rare cases. Spot them with box plots, z-scores or the IQR rule, then decide whether to fix, cap, remove or keep them, because some models are very sensitive to them.

The mascot stares at one giant pumpkin sitting at the end of a row of normal ones.

Think of it like this

The giant pumpkin at the mandi

A row of normal pumpkins, and then one the size of a scooter. Is it a prize-winning pumpkin, or did someone stack two together? You don't throw it out blindly. You check what it is, then decide.

Watch it happen

Delivery times in minutes, IQR rule

12
0
15
1
14
2
13
3
16
4
15
5
98
6

1/5Seven delivery times. The mean is about 26, but most deliveries took 12 to 16 minutes.

The big ideas

What you need to know

IQR fences

Values below Q1 − 1.5×IQR or above Q3 + 1.5×IQR are flagged. Works well on skewed data.

Z-scores

How many standard deviations from the mean. Above about 3 is often flagged, but only for roughly normal data.

Investigate before acting

Data entry error? Fix or remove it. Real rare event? Keep it, and maybe model it separately.

Choose a treatment

Remove, cap (winsorise), log-transform, or use models less sensitive to outliers, like trees.

See it in code

Walk through the code

python
1import pandas as pd2 3s = pd.Series([12, 15, 14, 13, 16, 15, 98], name="delivery_min")4q1, q3 = s.quantile([0.25, 0.75])5iqr = q3 - q16low, high = q1 - 1.5 * iqr, q3 + 1.5 * iqr7print(s[(s < low) | (s > high)])         # 988 9capped = s.clip(lower=low, upper=high)   # cap instead of delete10print(capped.max())                      # 18.5

1/4Seven delivery times, one suspicious.

The IQR rule in a few lines, and capping with clip so you keep the row but limit its pull.

Save yourself the pain

Common mistakes

Oops

Deleting every flagged outlier automatically.

Do this instead

Fraud, VIP customers and system failures are often outliers, and often exactly what you need to study.

Oops

Using z-scores on very skewed data like income.

Do this instead

Use the IQR rule, or log-transform first. Skew inflates the standard deviation and hides real outliers.

Your mission

Hunt outliers

~25 min

Quick check

A salary column has a few CEO salaries far above the rest, and they're correct. What's a sensible approach?

Go deeper

  1. 1Find outliers in one numeric column using the 1.5 times IQR rule.
  2. 2Plot the column before and after capping extreme values.
  3. 3Ask whether an outlier is a typo or a real customer before deleting it.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge