A different question entirely
In b4 you scored points by how dense their neighbourhood was - kNN distance and the Local Outlier Factor. Those need a distance metric and slow down as the data grows. Tonight's method, Isolation Forest (Liu, Ting & Zhou, 2008), needs neither. It builds an ensemble of random trees that recursively cut the data with random splits, and simply counts how many cuts it takes to isolate each point. Anomalies fall out in a handful of cuts; normal points hide deep in the tree. It is fast, it scales to large and high-dimensional data, and it has become the default first reach for tabular anomaly detection.
IsolationForest scored honestly with precision and recall, a feel for reading score_samples to rank the most anomalous days, and a hard-won understanding of what the contamination parameter really does - it sets your threshold.
Few and different isolate fast 6 min live
The core insight is almost too simple. Pick a random feature, pick a random split value, and cut. Repeat until every point sits alone. A point that is different from the rest gets separated in very few cuts; a point deep inside a dense cluster needs many. The number of cuts to isolate a point - its path length - is the anomaly signal. Short path means anomalous.
LiveWhy this beats distance methods at scale3 min▶
- No distance metric - it only ever compares a value to a random split, so it sidesteps the curse of dimensionality that bloats kNN and LOF distances.
- Near-linear time - each tree is built on a small subsample (256 points by default), so it stays fast on millions of rows and many features.
- Ensemble, not one tree - a single random tree is luck; averaging path length over a forest of them gives a stable, smooth anomaly score.
Isolation Forest is the workhorse of tabular anomaly detection. Fraud teams, infrastructure monitoring, and manufacturing QA reach for it first because it needs almost no tuning, handles dozens of features, and does not assume any shape for normal. When someone says "we threw an anomaly model at it," this is usually the model.
Average path length becomes the score 5 min live
One random tree is noisy - a lucky split could isolate a normal point early. So the forest builds many trees and averages each point's path length across all of them. Short average path across the ensemble means the point was consistently easy to cut off: anomalous. scikit-learn wraps this in score_samples (lower = more anomalous) and decision_function (the same, shifted so negative means flagged).
Self-studyThe contamination trap2 min read▶
Isolation Forest produces a continuous score for every point. To turn that into a yes/no flag it needs a threshold - and that is exactly what contamination sets. Tell it contamination=0.05 and it will flag the lowest-scoring 5% of points, no matter what the data actually contains.
- It is your assumed anomaly rate - a prior you supply, not something the model discovers.
- Set it too high and you flood the business with false alarms; the detector gets muted (alert fatigue).
- Set it too low and real anomalies slip through unflagged.
- You rarely know the true rate - so treat contamination as a dial you tune against precision and recall, or skip the flag entirely and rank by raw score.
The safest habit: read the scores, rank the most anomalous days, and let a human set the cut where the cost of a miss balances the cost of a false alarm.
Fit Isolation Forest on Lumen ★ 12 min · everyone builds
Build a feature matrix from the Lumen series, fit the forest unsupervised, and score it against the ground-truth labels.
Rebuild the labeled Lumen series so the notebook runs standalone.
Engineer features: the value, a lag, and rolling mean and std - so the forest can see context, not just level.
Fit IsolationForest, read predict and score_samples, and score precision, recall, and F1.
Sweep contamination, then rank by score ★ 10 min · build your own
Contamination is the whole ballgame. Sweep a few values and watch precision and recall trade off, then drop the flag entirely and just rank the most anomalous days by their raw score - the honest, threshold-free view.
Fit the forest at several contamination values and score each.
Watch precision fall as contamination rises (more flags, more false alarms) and recall climb.
Rank days by score_samples to surface the most anomalous - no threshold needed.
Ranking beats flagging when you own the response. A fraud analyst does not need a binary flag - they need a queue sorted worst-first, and they work down it until their time runs out. Handing the raw score to a human sidesteps the contamination guess entirely, and it is why most mature detection systems surface a ranked list, not a red/green light. Reserve hard flags for fully automated actions where a person cannot be in the loop.
Try it yourself - this week ◐ 30-45 min total
- Fit Isolation Forest on your own feature matrix and compare its F1 to the z-score baseline from b1. Does it earn its complexity?
- Drop the lag and rolling features and refit on the raw value alone. Watch it lose the contextual anomaly, and explain why.
- Sweep contamination from 0.02 to 0.25 and plot precision vs recall. Mark the value that matches your real cost of a miss vs a false alarm.
- Rank the days by
score_samplesand check whether the top of the list matches the true anomalies - the threshold-free view. - Optional: read Liu, Ting & Zhou (2008), "Isolation Forest" - the path-length derivation is short and elegant.
Sources covered (cited)
Taught from the detection canon. This session covers:
IsolationForestDemos 1-2 · fit, score_samples, and the contamination parameterThree questions before you go 🎯 ◐ 90 seconds
1 · What does Isolation Forest use as its anomaly signal?
Anomalies are few and different, so random splits cut them off in few steps. The average path length across the forest is the score: short = anomalous.
2 · What does the contamination parameter actually control?
Contamination is your assumed anomaly rate. It just sets where the score is cut into flag/no-flag. Too high floods false alarms; too low misses real anomalies.
3 · Why give Isolation Forest lag and rolling features instead of the raw value alone?
On the raw value alone, the forest is just a global threshold and misses the contextual dip. Context features let a random split isolate points that are odd for their moment.