← Back to Autonomy
Monograph № 2 · On Anomaly Detection

The Isolation
Forest

Why anomalies are the points that get lonely first — an interactive primer on a strange, elegant algorithm.

By Majid Mazouchi

The core idea, in one sentence

Most anomaly detectors work by modeling what normal looks like — fitting a density, drawing a boundary, reconstructing through a bottleneck — and then flagging anything that doesn't fit. Isolation Forest, introduced by Liu, Ting & Zhou in 2008, takes the opposite route. It doesn't model normal at all. Instead, it asks a sneakier question: how hard is it to isolate this point from all the others using random cuts?

The observation behind this is trivial once you see it. Anomalies are, by definition, few and different. A point that sits far from the crowd gets fenced off after just a couple of random axis-aligned slices. A point buried in the middle of a dense cluster takes many more slices to pry away from its neighbors. So if you grow a tree of random splits and record the depth at which each point ends up alone, anomalies live at shallow depths and normals live deep. That depth is your anomaly score. No distance metric. No density estimate. No notion of "normal" required.

Key idea

Anomalies are easier to isolate than normals — so use the number of random cuts needed to isolate a point as its anomaly score. Shallow = anomalous. Deep = normal.

A point gets isolated

Before the math, the feel. Below is a synthetic 2-D dataset — a cluster of 80 normal points and a handful of scattered anomalies. Click any point to select it, then press Isolate. The algorithm makes random axis-aligned cuts (pick a feature at random, pick a split value at random between the current min and max), each time shrinking the active region to the side containing your chosen point. Repeat until the region holds only your point. The number of cuts needed is the path length.

Normal Anomaly Cut
Click a point to select it. Try one of the red anomalies first — notice how few cuts it takes. Then try a green normal point buried in the cluster and see the difference.
Each axis-aligned cut is a random split on a random feature. The shaded outline tracks the active region as it closes around the selected point. Anomalies typically get isolated in 3–6 cuts; points deep in the cluster take 10–15 or more. That gap is what the algorithm exploits.

Two patterns emerge after a couple of tries. First, the gap between anomaly depths and normal depths is large — often a factor of three or four. Second, the gap is robust: it shows up across different random seeds, different cut sequences, different data realizations. The randomness washes out and the signal remains.

Growing a single tree

What you just watched, done to completion and for every point at once, is the construction of a single isolation tree (iTree). Start with all the data in one node. Pick a feature at random. Pick a split value at random between its min and max in that node. Partition the node into left and right children. Recurse on each child. Stop when a node has one point, or when you've hit a depth cap.

Axis-aligned random splits accumulate into a partition of the plane. Three highlighted probe points (A, B, C) track their current tree depth — the number of cuts standing between them and the rest of the data. Anomaly probes settle at shallow depth and stay there; normal probes keep getting subdivided.

Two details are worth noticing. One: the tree is not balanced. It doesn't try to be. A well-isolated point settles in a shallow leaf and stops; a dense region keeps splitting. This asymmetry is the whole point. Two: the leaves near anomalies cover large, mostly-empty rectangles of the plane. The leaves near the cluster core are tiny. The tree has implicitly mapped out the density of the data just by growing randomly.

From one tree to a forest

One tree is too noisy to trust. The splits are random — a different random seed gives a different tree, different path lengths, sometimes misleading answers. The fix is the same fix every ensemble method uses: average over many trees. Build t independent iTrees on random subsamples of the data (the standard subsample size is ψ = 256). For any query point, compute its path length in each tree and average. Short average path length → anomaly. Long average path length → normal.

Normal Anomaly heat → high anomaly score
Slide the tree count up and watch the score map smooth out. Click anywhere on the plot to drop a test point and see its anomaly score.
The background is the anomaly score s(x, n) evaluated on a dense grid. Paler regions = lower score = "looks normal." Darker regions = higher score = "looks anomalous." One tree gives a chaotic, stripy map; a hundred trees give a smooth contour that tracks the data density remarkably well.

The score, formally

The raw path length has an inconvenient property: it grows with the sample size, so comparing across datasets is awkward. The paper resolves this with a normalization. Define

s(x, n) = 2−E[h(x)] / c(n)

where E[h(x)] is the average path length of point x across the forest, and c(n) ≈ 2·H(n−1) − 2(n−1)/n is the expected path length of an unsuccessful search in an unsuccessful binary search tree of n samples. c(n) is just a normalizer: it makes the score live in (0, 1]. Scores near 1 are strongly anomalous; scores near 0.5 are ambiguous; scores well below 0.5 are safely normal.

Path lengths tell the whole story

The cleanest way to see why this all works is to plot the distribution of average path lengths for normals versus anomalies. The two histograms should sit nearly disjoint, and that separation is exactly what lets a threshold do useful work.

Green = normal training points, red = anomalies. The vertical dashed line is the anomaly-score threshold. Everything to its right is flagged. Drag the threshold and watch true positives, false positives, and the resulting precision & recall update live.

The shape of those two histograms is the entire case for the algorithm. If you could move the threshold freely and always make one or the other zero, you'd have a perfect classifier. In reality there's a thin overlap region — borderline points that sit near the cluster edge and could plausibly be either. That overlap is where threshold tuning matters, and where domain knowledge earns its keep.

Why it works so well

The Isolation Forest has some unusual properties that set it apart from nearest-neighbor, density-based, or boundary-based detectors:

It also has some limitations you should know about before betting production on it. Axis-aligned splits mean it can struggle with anomalies that are only unusual along oblique directions (e.g. a point that violates a linear relationship between two features). Extended Isolation Forest (Hariri et al., 2019) addresses this with random hyperplane splits. And because the randomness only enters through splits, it can be less sensitive to local density variations than distance-aware methods in some edge cases.

Where it earns its keep

Sensor & telemetry monitoring

Catch abnormal operating points in multi-sensor streams — unusual current/voltage/temperature/vibration combinations in powertrains, factory lines, server fleets. Runs fast enough to score incoming points in near-real-time.

Fraud detection

Flagging anomalous transactions in credit card, insurance claims, or online account behavior. Handles heterogeneous tabular features gracefully — no need to scale, no need to encode everything into a common metric space.

Network intrusion detection

Unusual packet headers, session lengths, or access patterns. One of the earliest and most cited application domains for iForest, with many production deployments.

Manufacturing defect screening

Test results or process-log records that deviate from the bulk of historical measurements. Works even when defect types are unseen in training because you never labeled anything.

Quick baseline for novel domains

When you've been handed a new dataset and asked "anything weird in here?", iForest is the 15-minute first answer. It almost always gives a useful signal and costs nearly nothing to run.

Pre-filter for expensive models

Use iForest to triage the bulk of the data down to a small pool of suspicious candidates, then run a heavier (and more accurate) model only on those. Cheap gatekeeper in front of an expensive specialist.

Rare-event data augmentation targets

Feed iForest scores into active-learning loops to surface the few interesting samples in oceans of mundane data — anomalies to label, rare faults to collect, edge cases worth investigating.

Data quality & EDA

Flag probable data-entry errors, corrupted rows, or out-of-range measurements before they poison downstream models. A cheap sanity check applied to any new batch of data.

Practical notes from the trenches

  1. Use the default subsample size (ψ = 256) as a starting point. The paper's surprising result is that increasing ψ does not generally improve detection. Larger subsamples let normals cluster thickly around anomalies and actually hide them. If ψ = 256 isn't working, try 128 or 512 — don't jump to 4096.
  2. 100 trees is plenty. Adding more rarely helps past a few hundred; the average path length converges quickly. Spend your compute budget elsewhere.
  3. Set the contamination rate explicitly if you can. The scikit-learn implementation uses a contamination parameter to pick the score threshold. If you know roughly what fraction of your data is anomalous, set it. If you don't, leave it at 'auto' and pick the threshold by inspecting the score distribution.
  4. Don't train on data that contains anomalies if you can help it. iForest is robust to a small amount of contamination, but clean training data gives tighter score separations. In anomaly detection terms, prefer a semi-supervised setup (train on normals only) when practical.
  5. Beware axis-aligned blind spots. iForest splits along one feature at a time. If your anomalies are defined by correlations across features (e.g. "x and y are both high" is fine but "x high, y low" is weird), standard iForest will miss some of them. Use the Extended Isolation Forest variant (oblique splits) or engineer interaction features manually.
  6. Categorical features need encoding. One-hot is usually fine for low-cardinality categoricals. For high-cardinality, target encoding or embedding-based encoding before feeding into iForest often works better than raw one-hot.
  7. Watch for feature collapse on constant columns. If a feature has zero variance in a subsample, the random split on it is degenerate. Most implementations skip these gracefully, but double-check if you're rolling your own.
  8. Scoring is cheap, reporting is hard. Getting a score is fast. Explaining why a point got a high score is harder — the algorithm's "reason" is distributed across many trees. For explainability, use SHAP values designed for iForest, or pair the detector with a downstream interpretable classifier on flagged points.
  9. Retrain on rolling windows for drifting data. If the normal distribution shifts over time (seasonality, concept drift), a stale forest will start flagging the new normal as anomalous. Retrain on a sliding window of recent history.
  10. Compare against simple baselines. Before celebrating an iForest result, run a Z-score on each feature and a Mahalanobis distance over the full feature set. If those find everything iForest finds, you didn't need the forest. If iForest finds things they miss, you have a real lift worth deploying.

References & further reading

  1. Liu, F. T., Ting, K. M., & Zhou, Z.-H. (2008). Isolation Forest. Eighth IEEE International Conference on Data Mining. The original paper — short, sharp, and worth reading in full.
  2. Liu, F. T., Ting, K. M., & Zhou, Z.-H. (2012). Isolation-Based Anomaly Detection. ACM Transactions on Knowledge Discovery from Data, 6(1). The extended journal version with more theory and experiments.
  3. Hariri, S., Kind, M. C., & Brunner, R. J. (2019). Extended Isolation Forest. IEEE TKDE. Oblique hyperplane splits that fix the axis-aligned blind spot.
  4. Guha, S., Mishra, N., Roy, G., & Schrijvers, O. (2016). Robust Random Cut Forest Based Anomaly Detection on Streams. ICML. A streaming cousin with elegant incremental updates.
  5. Pedregosa, F. et al. (2011). Scikit-learn: Machine Learning in Python. JMLR. See sklearn.ensemble.IsolationForest for the reference implementation.
  6. Chalapathy, R., & Chawla, S. (2019). Deep Learning for Anomaly Detection: A Survey. arXiv:1901.03407. Useful for placing iForest in the wider landscape of anomaly methods.
  7. Emmott, A. F. et al. (2015). A Meta-Analysis of the Anomaly Detection Problem. arXiv:1503.01158. Benchmark across many detectors, including iForest.
  8. Aggarwal, C. C. (2017). Outlier Analysis (2nd ed.). Springer. Textbook-level treatment of the whole field.