Sub topic 6
Why Subsampling Helps: Swamping and Masking
This chapter assumes you know how an isolation tree is built, what path length means, and how a forest averages path lengths into a score. It explains why each tree is trained on a small random sample instead of the full dataset, and which two failure modes that choice avoids.
Two problems with using all the data
Isolation Forest scores a point by how few random splits it needs to be separated. Two effects can corrupt that measure when a tree sees every point.
- Swamping is when normal points are mistaken for anomalies. Normal points that lie close to an anomaly cluster, or near the edge of the normal region, get isolated quickly because the dense data around them makes splits fall nearby. Their path lengths shrink and their scores rise.
- Masking is when anomalies hide among many other anomalies. If there are many anomalies, they form a dense group of their own. Random splits take more steps to separate one member from its neighbors, so their path lengths grow and their scores fall toward the normal range.
Both effects get worse as the dataset grows. More points means more points in each region, so the sparse regions that make anomalies easy to isolate fill up.
How a small sample helps
Each tree is built on a random subset of max_samples points drawn from the training set. With fewer points, there is less chance that anomalies are crowded together, and fewer normal points sit near them. The anomalies stay sparse relative to the sample, and the normal region stays clearly denser.
The original paper uses a sample size of 256 per tree and a depth limit of ceil(log2(256)) = 8. The depth limit works because anomalies are isolated near the top of a tree. Splitting beyond that depth mostly separates normal points from each other, which adds cost and no information about abnormality.
Seeing the effect
The script below plants a tight cluster of 100 anomalies far from 900 normal points. It compares a forest that uses all points per tree against one using 256. Results depend on the random seed, but the subsampled forest usually separates the cluster better.
import numpy as np
from sklearn.ensemble import IsolationForest
from sklearn.metrics import roc_auc_score
rng = np.random.RandomState(1)
X_normal = rng.normal(loc=0.0, scale=1.0, size=(900, 2))
X_cluster = rng.normal(loc=5.0, scale=0.3, size=(100, 2))
X = np.vstack([X_normal, X_cluster])
y_true = np.r_[np.zeros(900), np.ones(100)]
for max_samples in (len(X), 256):
clf = IsolationForest(n_estimators=200, max_samples=max_samples, random_state=1)
clf.fit(X)
scores = -clf.score_samples(X)
print(max_samples, "ROC AUC:", round(roc_auc_score(y_true, scores), 3))Common errors
- Treating more data per tree as better. Unlike many supervised models, a larger
max_samplescan lower detection quality, and it always raises training cost. - Setting
max_samplesabove the training size. scikit-learn caps it at the number of samples and warns you. The result is a full-data forest, with the masking risk described above. - Going too small. With very small samples, such as a few dozen points, trees may not represent the normal region well, and scores become noisy. More trees reduce that noise but do not repair a sample that is too small.
- Expecting subsampling to fix everything. If anomalies make up a large share of the data, such as 20% or more, masking can persist even in a sample.
Exercise
A dataset has 50,000 points, and 8% of them are a tight cluster of anomalies. Why might a forest with max_samples=50000 score the cluster as normal, and what effect do you expect from max_samples=256?
Show answer
With all points in every tree, the 4,000 anomalies are dense enough that random splits need many steps to separate any one of them, so their path lengths grow and they are masked. With 256 points per tree, the cluster contributes only about 20 points to each sample, so it is sparse compared with the sample. Paths for the cluster become shorter, and scores rise. The sample also holds fewer normal points near the cluster, which reduces swamping.