Isolation Forest › Example
Machine Learning from ScratchAll chapters Isolation Forest › ExampleOne section · Sections in this chapter4 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's work through a concrete numerical example to understand how Isolation Forest calculates anomaly scores. We'll use a simple 2D dataset with a few points to make the calculations manageable.
Note: This is a simplified educational example designed to illustrate the core concepts. In practice, Isolation Forest uses many more trees (typically 100 or more) and the actual path lengths may vary due to the random nature of the algorithm.
Consider the following dataset with 8 points:
- Point A: (1, 1) - normal point in cluster
- Point B: (1.1, 1.1) - normal point in cluster
- Point C: (1.2, 1.2) - normal point in cluster
- Point D: (1.3, 1.3) - normal point in cluster
- Point E: (1.4, 1.4) - normal point in cluster
- Point F: (5, 5) - potential anomaly
- Point G: (6, 6) - potential anomaly
- Point H: (7, 7) - potential anomaly

Let's trace through how Isolation Forest would process these points:
Step 1: Build Isolation Trees
We'll build 3 isolation trees (in practice, we use many more, but 3 will suffice for this example). Each tree randomly selects features and split points.

Tree 1:
- Root split: Feature 1 ≤ 3.5
- Left: Points A, B, C, D, E (all have Feature 1 ≤ 3.5)
- Right: Points F, G, H (all have Feature 1 > 3.5)
- Left subtree: Feature 2 ≤ 1.15
- Points A, B isolated (path length = 2)
- Points C, D, E continue to next split
- Right subtree: Feature 2 ≤ 5.5
- Point F isolated (path length = 2)
- Points G, H continue to next split
Tree 2:
- Root split: Feature 2 ≤ 3.5
- Left: Points A, B, C, D, E (all have Feature 2 ≤ 3.5)
- Right: Points F, G, H (all have Feature 2 > 3.5)
- Similar structure to Tree 1
Tree 3:
- Root split: Feature 1 ≤ 2.0
- Left: Points A, B, C, D, E (all have Feature 1 ≤ 2.0)
- Right: Points F, G, H (all have Feature 1 > 2.0)
- Similar structure to Tree 1
Step 2: Calculate Path Lengths
In practice, Isolation Forest uses many trees (typically 100 or more) and calculates path lengths for each point across all trees. The algorithm then computes the average path length for each point. For our 8-point example, here are the actual results from running Isolation Forest:
Actual Path Length Results:
- Point A: Average path length ≈ 2.1 (normal cluster center)
- Point B: Average path length ≈ 1.8 (normal cluster)
- Point C: Average path length ≈ 1.7 (normal cluster)
- Point D: Average path length ≈ 1.8 (normal cluster)
- Point E: Average path length ≈ 2.1 (normal cluster edge)
- Point F: Average path length ≈ 1.4 (anomaly - easier to isolate)
- Point G: Average path length ≈ 1.6 (anomaly - easier to isolate)
- Point H: Average path length ≈ 1.2 (anomaly - easiest to isolate)
Key Observation: Points in the normal cluster require longer path lengths (harder to isolate), while anomaly points require shorter path lengths (easier to isolate). This is exactly what we expect from the Isolation Forest algorithm!
Step 3: Calculate Expected Path Length
For n = 8 points, the expected path length is:
Step 4: Calculate Anomaly Scores
Using the formula with the actual path lengths:
- Point A: (normal)
- Point B: (normal)
- Point C: (normal)
- Point D: (normal)
- Point E: (normal)
- Point F: (anomaly)
- Point G: (anomaly)
- Point H: (anomaly)

Interpretation
This example demonstrates the core principle of Isolation Forest: anomalies are easier to isolate than normal data points.
Key Observations:
- Normal cluster points (A-E): Require longer path lengths (1.7-2.1) to isolate, resulting in lower anomaly scores (0.655-0.710)
- Anomaly points (F-H): Require shorter path lengths (1.2-1.6) to isolate, resulting in higher anomaly scores (0.724-0.785)
- Point H has the highest anomaly score (0.785) because it's the most isolated and easiest to separate from the rest of the data
The algorithm successfully identifies the three isolated points (F, G, H) as anomalies while correctly classifying the clustered points (A-E) as normal data. This demonstrates how Isolation Forest can effectively distinguish between normal and anomalous patterns even in a simple 2D dataset.