Sub topic 3
Building an Isolation Tree with Random Splits
This chapter assumes you know how decision trees partition data, can read basic NumPy, and are comfortable with logarithms. It covers how one isolation tree is built. Path length, the forest, and the score come in later chapters.
The goal of an isolation tree
An isolation tree (iTree) is a binary tree that keeps splitting data until each point sits alone, or until a depth limit is reached. It is not trained to predict anything. There are no labels and no impurity measure. The only question is how many splits it takes to separate a point from its neighbors.
Because anomalies are few and different, random splits tend to cut them off from the rest early. Normal points sit in dense regions and need many more cuts.
The construction procedure
Start with a sample of points at the root node. At each node, repeat these steps:
- If the node holds one point, or all points are identical, or the depth limit is reached, stop. The node becomes a leaf.
- Pick one feature uniformly at random.
- Pick a split value uniformly at random between the minimum and maximum of that feature among the points in this node.
- Send points with a value below the split value to the left child and the rest to the right child.
- Recurse on both children.
Nothing in this procedure looks at the data distribution beyond its range. That is why building a tree is cheap.
The outlier reaches a leaf after one split. The other points need further splits, so they end up deeper.
A minimal implementation
The code below builds one tree and returns the depth at which a query point lands in a leaf. It uses only NumPy.
import numpy as np
def build_tree(X, depth, max_depth, rng):
n = X.shape[0]
if n <= 1 or depth >= max_depth:
return {"size": n}
lo, hi = X.min(axis=0), X.max(axis=0)
usable = np.where(hi > lo)[0] # features that still vary
if len(usable) == 0:
return {"size": n} # all points identical
q = rng.choice(usable)
p = rng.uniform(lo[q], hi[q])
mask = X[:, q] < p
return {
"feature": q,
"split": p,
"left": build_tree(X[mask], depth + 1, max_depth, rng),
"right": build_tree(X[~mask], depth + 1, max_depth, rng),
}
def depth_of(tree, x, depth=0):
if "feature" not in tree:
return depth
child = "left" if x[tree["feature"]] < tree["split"] else "right"
return depth_of(tree[child], x, depth + 1)
rng = np.random.RandomState(1)
X = np.vstack([rng.normal(size=(255, 2)), [[8.0, 8.0]]])
max_depth = int(np.ceil(np.log2(len(X)))) # 8 for 256 points
depths_normal, depths_outlier = [], []
for _ in range(100):
tree = build_tree(X, 0, max_depth, rng)
depths_normal.append(depth_of(tree, X[0]))
depths_outlier.append(depth_of(tree, X[-1]))
print("mean depth, normal point :", np.mean(depths_normal))
print("mean depth, outlier :", np.mean(depths_outlier))The outlier at (8, 8) ends up much shallower on average than a typical point, even though every tree is random.
Details that matter
- Depth limit. The paper sets the maximum depth to ceil(log2(n)), roughly the average depth of a balanced tree. Anomalies are expected to isolate early, so deep branches carry little information and are not grown.
- Split range. The split value is drawn from the range of the points in the current node, not the whole dataset. Every split therefore separates at least one point from the rest.
- Constant features. A feature with no spread in the node cannot be split. The code above skips such features, and scikit-learn handles them too.
- Unfinished leaves. A leaf stopped by the depth limit may still hold several points. Later chapters adjust the path length for this using the leaf size.
Exercise
A node contains 6 points. The randomly chosen feature has minimum 2.0 and maximum 10.0 in this node. Between which values is the split value drawn, and why is it not drawn from the global range of the feature?
Show answer
The split value is drawn uniformly between 2.0 and 10.0. Drawing from the node's own range guarantees the split falls inside the spread of the points present, so both children are usually non-empty and the node makes progress. A global range could place the split outside the node's data and produce an empty child.