Navigation

Introduction to AI

Machine Learning

Deep Learning

Generative AI

Tools & Frameworks

General

Anomaly Detection with Machine Learning

Anomaly detection (also called outlier detection or novelty detection) is the task of identifying data points, patterns, or sequences that deviate significantly from expected behavior. It is one of the most practically valuable machine learning problems — with applications ranging from fraud detection and network security to industrial monitoring and medical diagnosis.

Unlike most supervised learning problems, anomalies are typically rare, often unlabeled, and highly varied in nature — making them uniquely challenging to detect.

Types of Anomalies

Point Anomalies

A single data point is anomalous relative to the rest of the dataset.

Example: A single transaction of $50,000 in an account that typically processes $200 transactions.

Contextual Anomalies

A data point is anomalous in a specific context but normal otherwise.

Example: A temperature of 35°C is normal in summer but anomalous in winter.

Collective Anomalies

A sequence or group of data points is anomalous as a collective, even if individual points appear normal.

Example: A series of individually valid but collectively suspicious API calls indicating a coordinated credential-stuffing attack.

The Label Scarcity Problem

Most real-world anomaly detection scenarios are unsupervised or semi-supervised. This is because:

  • Anomalies are rare — collecting enough labeled examples is difficult.
  • Anomalies are diverse — you can label known anomaly types, but novel anomalies won’t match them.
  • The definition of “normal” shifts over time (concept drift).

This makes anomaly detection fundamentally different from standard classification.

Classical Statistical Methods

Before machine learning, anomaly detection relied on statistical assumptions:

  • Z-score / 3σ rule — Flag points more than 3 standard deviations from the mean. Assumes Gaussian distribution.
  • Interquartile Range (IQR) — Flag values below $Q_1 - 1.5 \times IQR$ or above $Q_3 + 1.5 \times IQR$.
  • Grubbs’ test — Statistical test for outliers in univariate normal data.

These are fast and interpretable but break down for high-dimensional, non-Gaussian, or temporal data.

Machine Learning Approaches

Isolation Forest

The Isolation Forest algorithm detects anomalies by exploiting the observation that anomalies are few and different — they are easier to “isolate” in a random tree partition.

How it works:

  1. Randomly select a feature and a split value.
  2. Recursively partition the data.
  3. Anomalies require fewer splits to isolate → shorter path lengths.

The anomaly score is based on the average path length across an ensemble of trees. It is efficient ($O(n \log n)$), scales well to high dimensions, and requires no distribution assumptions.

from sklearn.ensemble import IsolationForest

model = IsolationForest(contamination=0.05, random_state=42)
model.fit(X_train)
scores = model.decision_function(X_test)  # lower = more anomalous
labels = model.predict(X_test)            # -1 = anomaly, 1 = normal

Local Outlier Factor (LOF)

LOF computes the local density of each point relative to its neighbors. Points in low-density regions compared to their neighbors are anomalous.

It captures contextual anomalies well — a point that looks normal globally may be anomalous in its local neighborhood.

One-Class SVM

Trains a decision boundary around the normal class in a high-dimensional feature space. Any point outside the boundary is flagged as anomalous. Effective for moderate-dimensional data with clean normal examples.

Autoencoders for Anomaly Detection

An autoencoder is trained to reconstruct normal data. At test time:

  • Normal points are reconstructed with low error.
  • Anomalies produce high reconstruction error — they cannot be well-represented by a model trained only on normal patterns.

$$\text{Anomaly Score} = |x - \hat{x}|^2$$

Variants:

  • Variational Autoencoders (VAEs) — Use a probabilistic latent space; anomaly score incorporates reconstruction loss + KL divergence.
  • Convolutional Autoencoders — For image or spatial data (e.g., visual defect detection).

Time-Series Anomaly Detection

Sequential data requires specialized methods that model temporal dependencies:

MethodApproach
LSTM AutoencoderEncode sequences; high reconstruction error = anomaly
ProphetDecompose trend + seasonality; flag deviations from forecast
ARIMA residualsModel expected values; flag residuals exceeding threshold
Transformer-basedAttention weights reveal which timesteps break expected patterns

Key challenge: distinguishing anomalies from change points (legitimate regime shifts) and seasonality variations.

Evaluation Metrics

Standard classification metrics are misleading when classes are heavily imbalanced (e.g., 0.1% anomalies). Preferred metrics:

  • Precision / Recall / F1 at a given threshold.
  • Area Under ROC Curve (AUC-ROC) — threshold-independent ranking metric.
  • Area Under Precision-Recall Curve (AUC-PR) — more informative than AUC-ROC for imbalanced data.
  • Average Precision (AP) — Summarizes the precision-recall curve.

For time-series, point-adjusted evaluation is common: if any anomaly score in an anomalous window exceeds the threshold, the entire window is counted as detected.

Semi-Supervised and Few-Shot Approaches

When some labeled anomalies are available, they can be incorporated:

  • One-vs-rest classification — Train a classifier treating known anomalies as one class.
  • Anomaly-conditioned contrastive learning — Use known anomalies to shape the representation space.
  • Few-shot anomaly detection — Meta-learning approaches that generalize from a small number of anomaly examples to novel anomaly types.

Concept Drift in Anomaly Detection

In production, “normal” behavior evolves over time. A system trained on historical data may generate false positives as normal patterns shift. Mitigations:

  • Sliding window retraining — Periodically retrain on recent normal data.
  • Online learning — Update models incrementally as new data arrives.
  • Drift detection — Use a drift detector (e.g., ADWIN, Page-Hinkley) to trigger model updates.

Real-World Applications

DomainUse Case
FinanceFraud detection, money laundering, market manipulation
CybersecurityIntrusion detection, lateral movement, account takeover
ManufacturingPredictive maintenance, defect detection
HealthcarePatient vital sign monitoring, rare disease screening
IT OperationsLog anomaly detection, performance degradation alerts (AIOps)
EnergySmart meter anomaly detection, grid fault detection

Practical Tips

  • Define “normal” carefully — Anomaly detection is only as good as the normal data it learns from. Contaminated training data degrades model quality.
  • Tune the contamination/threshold — Don’t blindly use default contamination estimates; calibrate to your false-positive tolerance.
  • Consider interpretability — In regulated domains, stakeholders need to understand why something is flagged. SHAP values and attention maps can help.
  • Combine multiple approaches — Ensemble methods (combining LOF + Isolation Forest + Autoencoder scores) often outperform any single method.

Anomaly detection sits at the intersection of statistics, machine learning, and domain expertise — the most effective systems combine principled models with deep understanding of what constitutes meaningful deviation in a given context.