Sub topic 15
Explaining Why a Point Was Flagged
This chapter assumes you can train an IsolationForest, read score_samples, and pick a threshold, as covered in earlier chapters. It shows how to answer the question an analyst asks next: why was this point flagged?
Why the forest does not explain itself
An Isolation Forest returns a score. It does not return a reason. The score is built from path lengths averaged over many random trees, and each split uses a random feature and a random value. No single split carries a meaning like "income too high".
A usable explanation therefore has to be computed after the fact. It should name the features that pushed the point toward the anomalous end of the score range.
Perturbation: replace one feature at a time
The simplest method needs only score_samples. Take a flagged point and replace one feature at a time with a typical value, such as the median of the training data. Then rescore the point. If the score rises a lot, that feature was a main cause of the flag.
import numpy as np
from sklearn.ensemble import IsolationForest
rng = np.random.RandomState(0)
X = rng.normal(size=(1000, 3))
X[0] = [0.1, 6.0, -0.2] # abnormal in feature 1 only
names = ["f0", "f1", "f2"]
clf = IsolationForest(random_state=0).fit(X)
x = X[0:1]
base = clf.score_samples(x)[0]
medians = np.median(X, axis=0)
for j, name in enumerate(names):
x_mod = x.copy()
x_mod[0, j] = medians[j]
delta = clf.score_samples(x_mod)[0] - base
print(name, round(delta, 3))Lower score_samples values mean more abnormal. A large positive delta therefore means that resetting the feature made the point look more normal. Here f1 should show a clearly larger change than f0 and f2.
Other options
| Method | What it gives you | Cost |
|---|---|---|
| Single-feature perturbation | Per-feature score change for one point | One extra scoring call per feature |
| Per-feature z-score or quantile | How far each value sits from the bulk of the data | Cheap, ignores the model |
| SHAP tree explanations | Additive contributions per feature | Heavier, needs the `shap` package |
| DIFFI | Feature importance from depths at which splits isolate points | Needs a separate implementation |
The z-score check is worth running first. It is model-free, so it also catches cases where the forest's score and a plain statistical view disagree.
Gotchas
- Interactions. Changing one feature at a time misses anomalies that exist only as a combination, such as a normal age with a normal income that never occur together. Try replacing pairs of features if single changes show nothing.
- The reference value matters. The median is a reasonable default. For skewed or multimodal features, sample several values from the inliers and average the score change.
- Explanations describe the model, not the cause. A feature that drives the score may be a symptom of a data problem or of drift rather than of a real event.
Exercise
A flagged point's perturbation results are: replacing feature A gives a score change of +0.01, and replacing feature B gives +0.12. Replacing both gives +0.13. Which feature mainly explains the flag, and what does the score change sign tell you?
Show answer
Feature B mainly explains the flag. Because lower score_samples values are more abnormal, a positive change means the point looked more normal after the replacement. The change from B (+0.12) is far larger than the change from A (+0.01), and both together add little beyond B.