Part 07 / Topic 5 of 5
Anomaly detection
Isolation Forest flags outliers by how easy they are to isolate: random splits separate an odd point quickly, a normal point slowly.
iso = IsolationForest(contamination=0.01, random_state=0).fit(X)
labels = iso.predict(X) # -1 = anomaly, 1 = normal
Planted test: 500 normal customer rows plus three absurd ones — all three planted rows came back flagged (along with three borderline normals; contamination sets how many flags you get, so expect some). Typical uses: fraud screening, sensor faults, data-quality gates before training.
🧪 Quick quiz
- Which task is unsupervised: predicting churn from labelled history, or grouping customers with no labels into segments?
Show the answer
Grouping customers with no labels — clustering finds structure without targets. Predicting churn from labelled history is supervised.
Done here? The practice set for Part 7 — 4 exercises with solutions — is on the part page.