Sub topic 1
Anomaly Detection with Isolation Forest
Prerequisites
Assume you know basic Python and NumPy, feature matrices, train/test data, decision trees, tree ensembles, probability, and logarithms.
What Anomaly Detection Is and Why It Is Hard
Anomaly detection finds observations that differ substantially from the usual pattern in a dataset. An observation is one row, such as a transaction or sensor reading. An anomaly might indicate fraud, a failed machine, a data-entry error, or a rare but valid event.
The main difficulty is that anomaly detection often has no labeled examples. You may know what normal data looks like, but not which rows are abnormal. Normal data can also contain several groups, changing behavior, noise, or rare events that are still legitimate.
A good detector must separate unusual observations without treating every uncommon observation as an error. It must also work when features have different ranges and when the data changes over time.
Anomalies Are Few and Different: The Isolation Idea
Isolation Forest uses a simple observation: anomalies are usually both rare and unlike the rest of the data. Because they have fewer nearby observations, random splits can separate them quickly.
Normal observations usually belong to a dense region. A random split often leaves them together with many other observations, so several more splits are needed to isolate one. An anomalous observation tends to be isolated after fewer splits.
Building an Isolation Tree with Random Splits
An isolation tree is a binary tree built by repeatedly dividing data into two groups. At each node, the algorithm:
- Selects a feature at random.
- Selects a split value between that feature's minimum and maximum values.
- Sends rows below the split to one child and the remaining rows to the other.
- Repeats the process for each child.
The process stops when a node contains one observation, contains identical values, or reaches a depth limit. The number of splits from the root to a point is its path length.
Path Length as a Measure of Abnormality
Isolation Forest builds many isolation trees and calculates each point's path length in every tree. It then averages those lengths.
A short average path length suggests that the point is easy to isolate and therefore more anomalous. A long average path length suggests that the point remains mixed with many observations and is more likely to be normal. The later anomaly score converts this average into a standardized value.
Exercise
Why does Isolation Forest usually give an anomaly a shorter path length than a normal observation?
Show answer
Random splits are more likely to separate a rare, different observation early, while a normal observation remains among many similar observations.