Data Science Track · Lesson 4

Outlier Detection — The IQR Method

What makes a value an outlier, how Tukey's fences work step by step, and the critical decision of whether to delete or keep what you find.

What is an Outlier?

An outlier is a data point that sits far away from the rest. But "far" is subjective — there is no universal threshold that separates normal values from outliers. A delivery time of 90 minutes is an extreme outlier in a fast-food context but completely normal for a sit-down restaurant order placed online.

This is the first lesson of outlier analysis: context matters more than the number itself.

Type 1 — Errors

Data entry mistakes, sensor failures, system bugs. A delivery time of −5 minutes or 9,999 minutes is almost certainly wrong. These should be investigated and corrected or removed.

Type 2 — Genuine Extremes

Real but rare events. A single customer who ordered 13 times is genuinely unusual — but real. An order taking 90 minutes happened. These are often the most interesting data points.

Why Outliers Matter

Outliers can quietly break your analysis in several ways:

The IQR Method — Step by Step

The IQR (Interquartile Range) method was developed by the statistician John Tukey. It defines "normal" as the middle 50% of your data and flags anything dramatically outside that range.

Worked example — FoodHub delivery_time: Q1 = 20.0 minutes (25th percentile) Q3 = 28.0 minutes (75th percentile) IQR = Q3 − Q1 = 8.0 minutes Lower fence = 20.0 − (1.5 × 8.0) = 20.0 − 12.0 = 8.0 min Upper fence = 28.0 + (1.5 × 8.0) = 28.0 + 12.0 = 40.0 min Any order with delivery time > 40 minutes is flagged as an outlier. Any order with delivery time < 8 minutes is flagged as an outlier.

Why 1.5 × IQR?

Tukey chose 1.5 as a rule of thumb. For data that follows a normal (bell curve) distribution, the fences at 1.5 × IQR will flag approximately 0.7% of values as outliers — a reasonable sensitivity for most datasets.

If you want to catch only the most extreme outliers, use 3 × IQR instead (this flags roughly 0.01% of normally distributed data). This is sometimes called the "far outlier" threshold.

The 1.5 rule is a starting point, not a law The right threshold depends on your domain. In a medical dataset where a single extreme value could represent a life-threatening condition, you might lower the threshold to catch more cases. In a noisy sensor dataset, you might raise it to avoid flagging real but variable measurements.

What To Do With Outliers

Finding an outlier is step one. Deciding what to do with it requires judgment:

Remove

Only if you can confirm it's a data error — wrong units, impossible value, clear entry mistake. Deleting real data because it's inconvenient is a form of dishonesty.

Cap (Winsorise)

Replace values beyond the fence with the fence value itself. Preserves the row but limits the distortion. Useful when you need all data but want to limit extreme influence.

Keep & Acknowledge

If the outlier is real, keep it and note it. Report both mean and median so readers can see its influence. This is often the most honest approach.

Analyse Separately

Build one analysis for typical data and another specifically about the outliers. Sometimes the outlier segment IS the most interesting finding.

Alternatives to the IQR Method

Z-Score Method

Flag values more than 3 standard deviations from the mean (z > 3 or z < −3). Problem: the mean and standard deviation are themselves sensitive to outliers — a method that uses them to detect outliers is somewhat circular.

z = (x − μ) / σ

Isolation Forest

A machine learning approach that randomly partitions data and measures how few splits it takes to isolate a point. Outliers are isolated quickly because they're "different." Ideal for high-dimensional data where IQR per-column misses multivariate outliers.

The IQR method's advantage It's simple, interpretable, and robust — it doesn't rely on assumptions about the data's distribution. For a first-pass outlier check on a new dataset, it's almost always the right starting point.
In the FoodHub Project

The IQR method was applied to delivery_time, food_preparation_time, and cost_of_the_order. Outliers were found in delivery time (some orders taking well above the upper fence). The decision was to keep them — a delivery taking 90 minutes is a real operational event, not a data entry error. It's exactly the kind of problem FoodHub needs to identify and fix. Removing it would have hidden the business problem from view. See the Data Cleaning section →