Skip to content

Clustering, density estimation, and anomaly detection · Anomaly detection · UCL048 · Algorithm

Isolation Forest

Rank observations by how easily random axis-aligned partitions isolate them. Sparse or extreme cases tend to require fewer splits than ordinary observations, making the method a useful scalable tabular baseline.

Runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.iforest. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Rank observations by how easily random axis-aligned partitions isolate them. Sparse or extreme cases tend to require fewer splits than ordinary observations, making the method a useful scalable tabular baseline.

Question / hypotheses

Which observations deviate from the chosen reference notion of normality, and at what operational alert rate? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.

Architecture / computational structure

An ensemble of random axis-aligned isolation trees with normalized path scoring.

Assumptions

Contamination generally sets a decision threshold, not an estimated probability that a point is abnormal. Axis-aligned partitions can produce geometric artifacts; many irrelevant features or clustered anomalies can reduce effectiveness.

Formula / computational definition

s(x)=2−E[h(x)]/c(ψ)s(x)=2^{-E[h(x)]/c(\psi)}, where h(x)h(x) is path length, ψ\psi subsample size, and c(ψ)c(\psi) the average unsuccessful-search path length used for normalization. Trees recursively choose a feature and a random split within its observed range.

Parameters and notation

Learned or fitted quantities: Tree split structures and a selected alert threshold. User-set hyperparameters/design choices: Set tree count, subsample size, feature subsampling, and threshold policy. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.

Fitting, fine-tuning, and adaptation

Neural fine-tuning is not applicable to this classical detector. Refit the reference model when its population changes, then recalibrate an alert threshold on a separate normal holdout. Tune sensitivity without using final-test anomaly labels repeatedly.

How to use and calibrate

Use held-out precision–recall and detection rates at operational false-alarm levels when labels exist. On unlabeled data, inspect stable high-ranked cases and perform sensitivity analysis. Synthetic anomaly performance is only a controlled demonstration.

Example application

Prioritize unusual telemetry records in a large tabular dataset. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.

Interpretation / pitfalls

Watch out

The reported result is conditional on the representation and objective. An alert is a screening result, not a diagnosis or a calibrated posterior probability. Evaluate both ranking and the decision threshold.

Python and R code examples

Python provides sklearn.ensemble.IsolationForest; its score_samples orientation is lower for more abnormal cases. R can bridge to that estimator. The portable component computes the original paper's normalized path score and must not be mixed up with library score offsets.

Component: Original isolation score from supplied average path lengths. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Original isolation score from supplied average path lengths. · needs numpy

import numpy as np
psi=16; cpsi=2*sum(1/j for j in range(1,psi))-2*(psi-1)/psi
path=np.array([2.,5.,8.]); result=2**(-path/cpsi)
print(result)
R

Component demo, not a trained model — Original isolation score from supplied average path lengths.

psi=16; cpsi=2*sum(1/(1:(psi-1)))-2*(psi-1)/psi
path=c(2,5,8); result=2^(-path/cpsi)
print(result)

Sources

From the CodeSplash reference, entry UCL048.