Machine Learning
Topic 25 ยท Applied ML ยท โฑ about 4 h

Anomaly Detection

Finding unusual cases with few or no labels: robust z-scores, isolation forests, density and distance methods, and human review.

Show:
In this topic
Beginner

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).

Beginner

Robust statistics

The ordinary z-score (xโˆ’xห‰)/s(x-\bar x)/s 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:

zi=xiโˆ’medianโก(x)1.4826โ‹…MADโก,MADโก=medianโก(โˆฃxiโˆ’medianโก(x)โˆฃ).z_i=\frac{x_i-\operatorname{median}(x)}{1.4826\cdot\operatorname{MAD}},\qquad \operatorname{MAD}=\operatorname{median}\big(|x_i-\operatorname{median}(x)|\big).

(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

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.

Try it

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.

Advanced

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

Code: scikit-learn detectors

Three detectors on the same data, ranked by score.

Python ยท scikit-learn
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 exist
Open in playground โ†—
Before it runs, the course makes 1,000 synthetic billing claims with 3 features, 2% of them anomalous (y = 1).
Check yourself

Quiz

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โ€ฆ

Go deeper

๐Ÿ“š 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.

Ready