Skip to content
AI360XpertBeta

Isolation Forest

Cut the data with random splits and count how many cuts each point needs before it sits alone. Odd points fall away in a few cuts, typical points take many, so the count itself is the anomaly score.

A far point sits alone after 3 random cuts scoring 0.82 while a buried point needs 10 cuts scoring 0.51, so depth alone ranks the stranger first
A far point sits alone after 3 random cuts scoring 0.82 while a buried point needs 10 cuts scoring 0.51, so depth alone ranks the stranger first

Why Does This Exist?

Most anomaly detection needs distance or density: how far a point sits from neighbours, how thin the crowd around it is. Both get slow and shaky as columns grow. Isolation forest skips the question entirely. It never measures how far a point is from anything. It measures how easy the point is to cut away from everything else with random splits.

Carry this picture down the page. A point alone in empty space gets separated almost immediately, because nearly any random cut leaves it by itself. A point buried in a dense cloud needs many cuts in a row, because every cut still leaves it bundled with neighbours. That count of cuts, called isolation depth, is the entire score. The parent problem and its thresholds live in anomaly detection. The density method that counts neighbours instead lives in DBSCAN. The reason a tuned threshold can still fail on new data lives in overfitting and underfitting.

Think of It Like This

Finding one person in a field versus a stadium

One person hides in an empty field. You draw random lines across the field and keep the half they stand in. Two or three lines and the remaining patch is tiny. Almost any line isolates them fast, because nobody else gets swept into the same half.

Another person hides in a packed stadium and you play the same game. Every line still leaves thousands on the same side. It takes cut after cut after cut before that person stands alone in one section. The field hider needed almost no work. The stadium hider needed a great deal. That difference in effort is isolation depth, and nobody measured a seat number to get it.

Where it stops: field and stadium have fences, so the search area is fixed. Data has no fence. Random cuts can also slice empty space forever, so the method caps tree depth and subsamples rows to keep the game honest.

How It Actually Works

An isolation tree is built like a decision tree with the learning removed. At each node pick one feature at random, pick one split value at random between its minimum and maximum, and divide the points. Recurse until every point sits alone or a depth limit is hit. No labels, no impurity gain, nothing optimized. Randomness is the instrument, because the tree shape then reflects only how points happen to be spread out.

Path length h(x)h(x) is the number of splits a point xx travels from root to its leaf. Points in sparse regions average short paths, because random cuts through empty space likely free them quickly. Points in dense regions average long paths, because most cuts leave them bundled with neighbours.

Worked example: depth 3 against depth 10

One tree can get lucky, so a forest builds many trees with independent random splits and averages. Call that average E(h)E(h). Averages alone do not compare across dataset sizes, so the method normalizes by c(n)c(n), the expected path length in a random dataset of nn rows. For n=256n = 256:

c(256)=2H(255)−2(255)/256≈10.25c(256) = 2H(255) - 2(255)/256 \approx 10.25

where HH is the harmonic number. The anomaly score is:

s=2−E(h)/c(n)s = 2^{-E(h)/c(n)}

A far point averaging E(h)=3E(h) = 3 cuts scores 2−3/10.25≈0.822^{-3/10.25} \approx 0.82, near 1, flagged. A buried point averaging E(h)=10E(h) = 10 cuts scores 2−10/10.25≈0.512^{-10/10.25} \approx 0.51, near 0.5, typical. Same formula, same c(n)c(n). The 0.310.31 gap is the diagram headline: one or two cuts on the left, a long chain of cuts on the right, and the score bar showing 0.82 against 0.51.

From score to decision

Scores near 1.0 mean strong anomalies, near 0.5 mean typical points, well below 0.5 mean points harder to isolate than average. The forest never sets the cutoff itself. The contamination setting states what fraction of rows you expect to be anomalous, and that fraction sets the threshold. Expect 1 percent and the top 1 percent of scores get flagged. That number is a business assumption about base rates, not a learned quantity.

Code

The point is the normalization: same depths, one formula, two clearly separated scores.

import math
harmonic: float = sum(1.0 / k for k in range(1, 256))c256: float = 2 * harmonic - 2 * 255 / 256
def score(mean_depth: float) -> float:    return 2.0 ** (-mean_depth / c256)
print(round(c256, 2))  # -> 10.25print(round(score(3.0), 2), round(score(10.0), 2))  # -> 0.82 0.51print(round(score(4.5), 2))  # -> 0.74

The printout matches the hand computation: c(256)c(256) is 10.2510.25, depth 3 scores 0.820.82, depth 10 scores 0.510.51. A point at depth 4.5 scores 0.740.74, still above the typical band, which shows how quickly the score falls as cuts accumulate.

Watch Out For

Setting contamination from a round number instead of a base rate

Contamination directly sets the decision threshold. A guess borrowed from another dataset, or a round 0.05 left as default, shifts the cutoff for every row and can flag several times too many or too few points.

Symptom: the flagged count always matches the setting even as incident reports disagree, quarter after quarter. Fix: estimate the fraction from historical incident rates where they exist, review it with the team that handles the flags, and treat it as an assumption to revalidate, the same validation discipline overfitting and underfitting asks of any threshold.

Expecting the score to explain why a point was flagged

The score averages path lengths over many random trees. No single feature or split translates cleanly into a reason a downstream analyst can act on.

Symptom: a flagged transaction reaches review with a bare number and stalls there. Fix: pair the ranking with a separate attribution step for the flagged few, and keep the forest as the ranker rather than the explainer. When the dense-region boundary itself is the question, compare with DBSCAN rather than squeezing reasons from depths.

The Quick Version

  • Isolation forest cuts data with random splits and counts cuts per point, never computing distance or density.
  • Path length is short for odd points in sparse regions and long for buried points in dense ones.
  • Averaging across many random trees corrects any single tree lucky split.
  • On 256 rows depths 3 and 10 score 0.82 and 0.51 through 2−E(h)/10.252^{-E(h)/10.25}, so one threshold works across sizes.
  • Contamination sets the cutoff from an expected base rate. A guessed rate flags the wrong count by construction.