Anomaly Detection in Machine Learning
Why anomaly detection is a different kind of problem
Most classification problems assume you have a reasonable number of labeled examples of every class you care about. Anomaly detection breaks that assumption by design: the interesting cases — fraudulent transactions, failing machinery, network intrusions, defective products — are rare, often under 1% of the data, and new kinds of anomalies keep appearing that no training set could have anticipated.
This rarity makes standard supervised classification a poor fit. A classifier trained on 0.1% positive examples can achieve 99.9% accuracy by predicting 'normal' every single time, which is useless. Anomaly detection techniques instead focus on learning what normal looks like, and flagging anything that deviates from it — an approach that works even with few or no labeled anomalies at all.
Family 1: Statistical methods
The simplest approach assumes normal data follows a known statistical distribution (often Gaussian) and flags points that are improbable under that distribution.
For a single feature, this is as simple as flagging points more than k standard deviations from the mean (the z-score method), or using the Interquartile Range (IQR) rule — flagging points below Q1 - 1.5*IQR or above Q3 + 1.5*IQR. For multiple correlated features, the Mahalanobis distance generalizes this by measuring distance from the mean while accounting for the covariance structure between features, so it correctly identifies points that are unusual given how features normally relate to each other, not just unusual in any single dimension.
Statistical methods are fast, interpretable, and work well when the normal-data assumption genuinely holds — but they degrade quickly on high-dimensional or multimodal data that doesn't fit a single clean distribution.
Family 2: Isolation Forest
Isolation Forest takes a completely different, tree-based approach based on a simple insight: anomalies are 'few and different,' which makes them easier to isolate than normal points.
The algorithm builds many random trees, where each split randomly picks a feature and a random split value within that feature's range. Normal points, sitting in dense regions surrounded by many similar points, require many splits to isolate into their own leaf. Anomalies, sitting in sparse regions far from other points, tend to get isolated in very few splits — they're surrounded by much less 'company' that would otherwise need to be split away first.
Each point's anomaly score is based on the average path length (number of splits) needed to isolate it across all trees in the forest. Short average path length means easily isolated means likely anomalous.
Note
Family 3: Autoencoder-based detection
For complex, high-dimensional data like images, sensor time series, or network traffic, neural approaches often work better than tree- or distance-based methods. An autoencoder is a neural network trained to compress its input into a small latent representation and then reconstruct the original input from that compressed form, trained purely to minimize reconstruction error on normal data.
The anomaly detection trick: an autoencoder trained only on normal data learns to reconstruct normal patterns well, but will struggle to reconstruct genuinely novel, anomalous patterns it never saw during training. The reconstruction error — the gap between the input and the autoencoder's reconstruction of it — becomes the anomaly score: low error means the pattern looks like something the model has learned to represent well (normal), high error means it doesn't (anomalous).
Choosing an anomaly detection approach
| Method | Best for | Scales to high dimensions? | Interpretability |
|---|---|---|---|
| Statistical (z-score, Mahalanobis) | Low-dimensional, roughly Gaussian data | Poor | High — direct statistical meaning |
| Isolation Forest | Tabular data, fraud, general-purpose default | Good | Moderate — feature importances available |
| Autoencoder | Images, sensor streams, complex high-dim data | Good (with enough training data) | Low — reconstruction error only |
Note
What's next
Anomaly detection closes out the core unsupervised toolkit alongside clustering and dimensionality reduction. From here, the natural next step is combining these building blocks — using clustering, mixture models, and anomaly scores together as feature engineering inputs to the supervised models covered earlier in this track.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.