13 — Analyze Your Data

Outlier Detection#

Data AnalysisStatistical 1 min read
◆ The PatternIQR, Z-score, Isolation Forest — finding extreme values and knowing when they're the signal

Outliers can be errors to fix or signal to keep. Z-score flags points far from the mean, IQR is robust to skew, and Isolation Forest catches complex multi-dimensional outliers that univariate methods miss.

IQR method: outlier if x < Q1 − 1.5·IQR or x > Q3 + 1.5·IQR
IQR = Q3 − Q1 (interquartile range). Robust to skewed distributions.
Z = (x − μ) / σ    outlier if |Z| > 3
Z-score = standard deviations from mean. Assumes roughly normal data.
// Interactive — scattered points with outlier detection zones
Method
Outliers—
Pattern bridge: In markets, outliers are black swan events — the crash days that break every model. In fraud detection, the outliers are the target. Context decides whether to remove or study them.
Worked example

Twelve values around 10, plus 25 and 300. The z-score rule flags only 300: that one value inflates the standard deviation so much that 25 hides behind it (“masking”). The median-based modified z-score flags both 25 and 300. Outlier rules should not be built from statistics the outliers themselves distort.

Where it misleads
  • An outlier is defined relative to a model; a value extreme in one feature can be normal given the others.
  • Deleting outliers automatically can delete the most important cases (fraud, failures, rare events).
  • Thresholds such as 3 or 3.5 are conventions; the expected number of false flags grows with the data.
In code
x = np.array([10, 11, 9, 10, 12, 11, 10, 9, 11, 10, 25, 300.0])   # two outliers: 25 and 300
z = (x - x.mean()) / x.std()
mad = np.median(np.abs(x - np.median(x)))
robust = 0.6745 * (x - np.median(x)) / mad          # modified z-score
flag_z = x[np.abs(z) > 3].tolist()
flag_robust = x[np.abs(robust) > 3.5].tolist()
Python, in your browser — edit the code and run it again

Assumes import numpy as np and import pandas as pd (and SciPy where imported). Each snippet simulates or makes up its own data, as the comments say.

The same idea with a library
# Python — outlier detection
from sklearn.ensemble import IsolationForest
import numpy as np

# IQR method
Q1, Q3 = np.percentile(data, [25, 75])
IQR = Q3 - Q1
mask = (data < Q1 - 1.5*IQR) | (data > Q3 + 1.5*IQR)

# Isolation Forest — multi-dimensional
iso = IsolationForest(contamination=0.05)
labels = iso.fit_predict(X)  # -1 = outlier
Sources
  • B. Iglewicz & D. C. Hoaglin, How to Detect and Handle Outliers, ASQC Quality Press, 1993 — the modified z-score
  • C. Leys, C. Ley, O. Klein, P. Bernard & L. Licata, “Detecting Outliers: Do Not Use Standard Deviation around the Mean, Use Absolute Deviation around the Median”, Journal of Experimental Social Psychology 49(4), 2013
  • F. T. Liu, K. M. Ting & Z.-H. Zhou, “Isolation Forest”, IEEE ICDM, 2008

Last reviewed October 2026 · How this site checks its content · Report a mistake

Linked from 7
← Previous
Distribution Shape
Open in the full reader, with the topic sidebar