Isolation Forest › Example

Michael BrenndoerferJuly 6, 202510 min read
Machine Learning from ScratchAll chapters
Part 5 of 9Tree-Based Models
Chapter 47Isolation Forest › Example
Reading section 4 of 8 · Example · 10 minSection 4 of 8 · 10 min

Book contents

Section 4 of 8 · 10 min read

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
Out[8]:
Visualization
Scatter plot showing 8 labeled points: normal cluster (A-E) near (1,1) and anomalies (F-H) at (5,5), (6,6), (7,7).
Simple 2D dataset with 8 points: Points A, B, C, D, E form a normal cluster near (1,1), while Points F, G, H are potential anomalies at (5,5), (6,6), and (7,7) respectively. This visualization shows the spatial distribution that Isolation Forest will analyze.

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.

Out[9]:
Visualization
Scatter plot showing Isolation Forest tree partitions with labeled points and colored split lines separating normal and anomalous regions.
Visualization of how Isolation Forest partitions the data space. The first tree splits on Feature 1 ≤ 3.5, separating the normal cluster (A, B, C, D, E) from potential anomalies (F, G, H). Each split creates regions that isolate points with fewer partitions, making anomalies easier to identify. The teal dashed line shows the first split, while amber and purple lines show subsequent splits that further partition the space.

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:

c(8)=2H(7)−2(7)8=2(1+12+13+14+15+16+17)−148=2(2.592857)−1.75=3.435714c(8) = 2H(7) - \frac{2(7)}{8} = 2(1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \frac{1}{5} + \frac{1}{6} + \frac{1}{7}) - \frac{14}{8} = 2(2.592857) - 1.75 = 3.435714

Step 4: Calculate Anomaly Scores

Using the formula s(x,n)=2−hˉ(x)c(n)s(x,n) = 2^{-\frac{\bar{h}(x)}{c(n)}} with the actual path lengths:

  • Point A: s(A,8)=2−2.13.435714=2−0.611=0.655s(A,8) = 2^{-\frac{2.1}{3.435714}} = 2^{-0.611} = 0.655 (normal)
  • Point B: s(B,8)=2−1.83.435714=2−0.524=0.695s(B,8) = 2^{-\frac{1.8}{3.435714}} = 2^{-0.524} = 0.695 (normal)
  • Point C: s(C,8)=2−1.73.435714=2−0.495=0.710s(C,8) = 2^{-\frac{1.7}{3.435714}} = 2^{-0.495} = 0.710 (normal)
  • Point D: s(D,8)=2−1.83.435714=2−0.524=0.695s(D,8) = 2^{-\frac{1.8}{3.435714}} = 2^{-0.524} = 0.695 (normal)
  • Point E: s(E,8)=2−2.13.435714=2−0.611=0.655s(E,8) = 2^{-\frac{2.1}{3.435714}} = 2^{-0.611} = 0.655 (normal)
  • Point F: s(F,8)=2−1.43.435714=2−0.407=0.754s(F,8) = 2^{-\frac{1.4}{3.435714}} = 2^{-0.407} = 0.754 (anomaly)
  • Point G: s(G,8)=2−1.63.435714=2−0.466=0.724s(G,8) = 2^{-\frac{1.6}{3.435714}} = 2^{-0.466} = 0.724 (anomaly)
  • Point H: s(H,8)=2−1.23.435714=2−0.349=0.785s(H,8) = 2^{-\frac{1.2}{3.435714}} = 2^{-0.349} = 0.785 (anomaly)
Out[10]:
Visualization
Table showing path lengths, average path lengths, and anomaly scores for 8 data points in the Isolation Forest example.
Summary of Isolation Forest calculations for the 8-point example. The table shows path lengths, average path lengths, and final anomaly scores for each point. Notice how normal cluster points (A-E) have lower anomaly scores (0.655-0.710) while anomaly points (F-H) have higher scores (0.724-0.785).

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.