Anomaly Detection
Finding unusual cases with few or no labels: robust z-scores, isolation forests, density and distance methods, and human review.
Unusual, not necessarily labelled
Anomaly detection finds cases that look different from the rest: fraudulent claims, faulty sensors, data-pipeline errors, intrusions, sudden drops in sales. The defining difficulty is that anomalies are rare and often unlabelled: you may have no examples of the next kind of fraud at all.
So the output is usually a ranking or a score, not a final verdict: the most unusual cases go to a human reviewer, whose decisions become labels that later allow a supervised model. Three practical families: statistical rules (z-scores, percentiles, control charts), distance and density methods (points far from their neighbours, such as k-NN distance and Local Outlier Factor), and isolation methods (Isolation Forest: anomalies are easy to separate with few random splits).
Robust statistics
The ordinary z-score has a flaw: the outliers themselves inflate the mean and standard deviation, hiding themselves. The robust z-score uses the median and the median absolute deviation (MAD) instead, which a few extreme values barely move:
(The 1.4826 makes it comparable to a standard deviation for normal data.) Values beyond about 3.5 are commonly flagged. Single-feature rules miss contextual anomalies, though: a $300 claim is normal for a 30-minute consultation and very strange for a 5-minute one. That needs methods that look at features together.
Try it: which claims look wrong?
Each dot is a billing claim. Choose a method and how many cases the review team can check; flagged cases are circled, and true problems are red (known only for this demo).
Interactive: this part runs in your browser.
Code example: rank unusual billing claims
A worked example: read it step by step (every part is explained), then run it and experiment.
Interactive: this part runs in your browser.
In production
Choose the number of alerts from review capacity, not a fixed threshold, and measure precision at that capacity. Expect drift: what is normal changes (seasonal patterns, new products), so rolling baselines and regular refits are needed. Remove known, explained events (public holidays, planned outages) to cut false alarms. When labels accumulate, switch to or combine with a supervised model, which is usually much more accurate for known anomaly types, and keep the unsupervised detector for new kinds. For time series, detect anomalies on the residual after removing trend and seasonality.
Code: scikit-learn detectors
Three detectors on the same data, ranked by score.
import numpy as np
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor
from sklearn.preprocessing import StandardScaler
Xs = StandardScaler().fit_transform(X)
iso = IsolationForest(n_estimators=300, random_state=0).fit(Xs)
iso_score = -iso.score_samples(Xs) # higher = more anomalous
lof = LocalOutlierFactor(n_neighbors=20)
lof.fit_predict(Xs); lof_score = -lof.negative_outlier_factor_
top = np.argsort(-iso_score)[:25] # what the review team checks this week
print("precision@25:", y[top].mean()) # only if some labels existQuiz
1. Why use the median and MAD rather than the mean and standard deviation?
2. A $300 claim for a 5-minute visit is an example ofโฆ
3. Isolation Forest flags points thatโฆ
4. How many alerts should the system raise per week?
5. Once you have many labelled fraud cases, you shouldโฆ
๐ Further reading
Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.
- scikit-learn: Novelty and outlier detection โ Official guide