Sub topic 5
From One Tree to a Forest
This chapter assumes you know how a single isolation tree is built from random feature and split choices, and that you know what path length means. It covers why one tree is not enough and how an ensemble fixes that.
Why one tree is unreliable
An isolation tree picks a random feature and a random split value at every node. Because of this randomness, one tree can isolate an anomaly late by bad luck. It can also isolate a normal point early. A single path length is therefore a noisy measurement.
The remedy is the one used in other ensembles: build many trees and average. Each tree is an independent, cheap, random experiment. The average of their path lengths is stable even though each individual tree is not.
How the forest is built
The training procedure has three steps, repeated for each tree:
- Draw a random subsample of
max_samplesrows from the training data, without replacement. - Grow an isolation tree on that subsample, splitting randomly until each point is alone or a depth limit is reached.
- Store the tree.
The depth limit is about log2 of the subsample size, ceil(log2(max_samples)). Anomalies are expected to be isolated well before that depth, so deeper splitting only spends time on normal points.
Scoring with the forest
To score a point, send it through every tree and record its path length in each. The forest's measure of abnormality is the mean, written E[h(x)]. A short mean means the point is isolated easily across many random trees. The next chapters turn this mean into a score.
Seeing the averaging effect
The code below fits a forest and reads the depth of two points in each tree. One point is at the center of the data. The other is far away. It uses the fitted trees in estimators_ and the raw tree depth. scikit-learn's own score adds a correction for leaves that still hold several points, so these numbers are close to, but not identical to, its internal path lengths.
import numpy as np
from sklearn.ensemble import IsolationForest
rng = np.random.RandomState(1)
X = rng.normal(size=(500, 2))
points = np.array([[0.0, 0.0], [5.0, 5.0]]) # center vs. far away
clf = IsolationForest(n_estimators=100, max_samples=256, random_state=1).fit(X)
depths = np.array([
np.asarray(tree.decision_path(points).sum(axis=1)).ravel() - 1
for tree in clf.estimators_
]) # shape: (n_trees, 2)
print("first tree depths:", depths[0])
print("mean depths: ", depths.mean(axis=0))
print("std of depths: ", depths.std(axis=0))The standard deviation shows how much a single tree varies. The mean differs clearly between the two points: the far point has a much smaller mean depth.
Choosing the number of trees
The mean path length converges as trees are added, and the original paper reports that 100 trees are enough in practice. More trees reduce variance in the score but cost linear time in training and prediction. Past the point where scores stop changing, extra trees add cost without benefit.
Common errors
- Expecting a deterministic result. Without
random_state, two fits give slightly different scores. - Trusting a forest with very few trees. With
n_estimators=5, the score is still noisy. - Setting
max_samplesequal to a huge dataset. This slows training and weakens the benefit of subsampling, which the next chapter explains.
Exercise
A forest has 100 trees. A point has path length 4 in 60 trees and path length 10 in the other 40. What is its mean path length, and why is the mean preferred over any single tree's value?
Show answer
The mean is (60 × 4 + 40 × 10) / 100 = 6.4. A single tree's path depends on random split choices and can be misleadingly long or short, while the mean over many independent trees is a more stable estimate of how easily the point is isolated.