Sub topic 2
Anomalies are few and different: the isolation idea
This chapter assumes you know basic Python and NumPy and understand how a decision tree splits data on a feature. It builds on the previous chapter, which defined anomaly detection. Here you learn the single observation that Isolation Forest is built on.
Two properties of anomalies
Most detectors first model what "normal" looks like. They then flag points that fit the model badly. Isolation Forest takes a different route. It relies on two properties that anomalies usually have:
- They are few. They make up a small fraction of the data.
- They are different. Their feature values lie far from the dense regions where normal points sit.
Because of these properties, anomalies are easy to separate from everything else. Normal points sit close together, so you need many cuts to separate one from its neighbors. An anomaly sits alone, so a few cuts are enough.
Isolating a point with random cuts
To isolate a point means to cut the data until that point is alone. A random cut picks a feature and then a split value drawn uniformly between the minimum and maximum of the current data. Each cut keeps only the side containing the target point.
Take one feature with values 1.0, 1.1, 1.2, 1.3, 1.4 and 9.0. A random split value falls between 1.0 and 9.0. Almost every possible split puts 9.0 alone on one side. The same split rarely separates 1.2 from its neighbors. Isolating 1.2 takes many more cuts.
The number of cuts needed is the point's depth. Later chapters call this the path length. Anomalies tend to have a small depth. Normal points tend to have a large one.
Measuring it by simulation
One random sequence of cuts is noisy. Averaging over many sequences gives a stable signal. The code below isolates a normal point and an outlier 200 times each and compares the mean depths.
import numpy as np
def isolation_depth(x, data, rng):
"""Count random cuts needed to isolate x within data."""
points = data.copy()
depth = 0
while len(points) > 1:
lo, hi = points.min(), points.max()
if lo == hi:
break # identical values cannot be separated
split = rng.uniform(lo, hi)
if x < split:
points = points[points < split]
else:
points = points[points >= split]
depth += 1
return depth
rng = np.random.RandomState(0)
data = np.append(rng.normal(0.0, 1.0, size=200), 10.0)
normal_point = data[0]
outlier = 10.0
normal_depths = [isolation_depth(normal_point, data, rng) for _ in range(200)]
outlier_depths = [isolation_depth(outlier, data, rng) for _ in range(200)]
print("mean depth, normal point:", np.mean(normal_depths))
print("mean depth, outlier: ", np.mean(outlier_depths))The outlier's mean depth comes out much smaller than the normal point's. Isolation Forest turns exactly this gap into a score.
Common misreadings
- No distances or densities are computed. The method never measures how far a point is from its neighbors. It only counts cuts.
- Short depth does not prove an anomaly. A single random sequence can isolate a normal point quickly by luck. You need the average over many sequences. The forest supplies that average.
- Anomalies must be rare. If a large group of anomalies sits together, they behave like a normal cluster and need many cuts. The chapter on subsampling returns to this problem, called masking.
Exercise
A dataset has 1,000 points on one feature. 998 lie between 0 and 1, and two lie at 50 and 60. Why does a random cut usually isolate the point at 50 or 60 faster than a typical point near 0.5?
Show answer
The split value is uniform over the full range, about 0 to 60. Almost all of that range lies between the cluster and the two outliers, so the first cuts mostly separate the outliers from the cluster. Cutting inside the cluster needs a split value inside the narrow interval from 0 to 1, which is a tiny share of the range. Isolating a clustered point therefore needs many more cuts.