Isolation Forest
Quick answer
Isolation Forest detects unusual points by repeatedly splitting randomly selected features at random thresholds. Points that become isolated in fewer splits tend to be more unusual. A forest averages isolation behavior across randomized trees. Examples are static Python snippets tested locally on CPU; Native detector examples require NumPy and scikit-learn; exercises use standard-library Python. ByteMind execution is unverified.
Normalized path illustration
Expected output (locally tested): 0.707 0.25. Shorter paths produce larger original anomaly scores.
How it works
A tree recursively partitions a subsample; sparse points often reach small leaves sooner. The original normalized anomaly score is s(x)=2**(-E[h(x)]/c(psi)), with psi the subsample size, h path length including an expected remaining path adjustment for multi-point leaves, and c(n)=2H_(n-1)-2(n-1)/n for n>1 (c(1)=0). A larger original score means shorter paths. Scikit-learn IsolationForest.score_samples uses the opposite orientation: smaller means more abnormal. decision_function equals score_samples minus offset_; predict returns -1 for negative decision values. contamination controls the fitted offset, not an independently measured future anomaly prevalence. With contamination="auto" the offset is -0.5 in this implementation. max_samples controls tree subsampling; n_estimators controls the ensemble size. Fix random_state and n_jobs for repeatable small examples. Feature transformations can change how a feature is represented; affine scaling alone does not generally solve irrelevant-feature problems. Train with a representative clean reference for novelty evaluation, then choose a threshold on separate calibration scores; do not refit after seeing test rows. Unlike a distance-based detector, it is not based on nearest neighbors. An unusual point might represent a valid rare mode, so review features and operating conditions.
Native score orientation
import numpy as npfrom sklearn.ensemble import IsolationForestx=np.arange(20,dtype=float).reshape(-1,1)m=IsolationForest(n_estimators=40,random_state=7,n_jobs=1).fit(x)s=m.score_samples([[10],[100]])print(bool(s[1]<s[0]),bool(np.allclose(m.decision_function([[10],[100]]),s-m.offset_)))Expected output (locally tested): True True. The far point scores lower; the decision offset is checked independently.
Common mistakes
Do not invert score_samples twice or confuse contamination with a known population rate. Do not interpret simplified tree depth as a full Isolation Forest implementation.
Practice and next steps
Compare path lengths 2 and 8 with the same positive normalizer c=4. Explain how increasing tree count affects stochastic variability but does not guarantee better detection.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Isolation Forest — knowledge check
Apply isolation forest to small examples and debugging scenarios.
Which original path is more anomalous?
Which sklearn score_samples value is more abnormal?
What does contamination control?
What controls subsample size?
Why freeze the detector before test scoring?