What is anomaly detection in machine learning?
Anomaly detection identifies observations that differ significantly from expected patterns in data. It solves problems such as fraud detection, equipment monitoring, and intrusion detection by separating unusual events from normal behavior. The main trade-off is between detecting more anomalies and generating too many false positives.
Read full answerHide full answer
Applies tomachine learning concepts (all versions)
Detection Settings
-
Supervised anomaly detection: Training data contains labelled anomalies and normal observations. Treat this as imbalanced classification using models such as logistic regression, decision trees, gradient boosting, or neural networks. Evaluate with precision, recall, F1, PR-AUC, and recall at an acceptable false-positive rate.
-
Semi-supervised anomaly detection: Training data contains mainly or only normal observations. Algorithms such as One-Class SVM learn the boundary of normal behavior and flag observations outside it. Contaminated normal data can distort that boundary.
-
Unsupervised anomaly detection: No labels are available. Algorithms such as Isolation Forest, Local Outlier Factor, DBSCAN, and autoencoders identify unusual structure without knowing which anomalies are present.
Anomaly Types
-
Point anomalies: A single observation is unusual relative to the rest of the data. Example: a fraudulent transaction with an unusually high amount.
-
Contextual anomalies: A value is normal overall but unusual for its context. Example: a temperature of 30°C is ordinary in summer but extreme in winter.
-
Collective anomalies: A group or sequence is anomalous even if its individual values are not. Example: several failed logins in succession indicating a password-guessing attack.
Method Comparison
| Method | How it works | Strengths | Weaknesses | When to use and typical scale |
|---|---|---|---|---|
| Z-score and IQR rules | Flag values far from the mean or outside Q1 - 1.5 × IQR and Q3 + 1.5 × IQR. Here, Q1 and Q3 are the first and third quartiles, and IQR = Q3 - Q1. |
Simple, fast, interpretable | Assume roughly suitable distributions; thresholds are sensitive to outliers | Small tabular data; approximately O(n × d) |
| Isolation Forest | Randomly partitions features and isolates observations with unusually short paths | Fast, scalable, robust, works well on mixed tabular data | Contamination estimate affects the threshold; local anomalies can be difficult | General-purpose detection; near-linear for a fixed subsample size |
| Local Outlier Factor (LOF) | Compares each point’s local density with those of its neighbors | Detects local anomalies in heterogeneous data | Sensitive to n_neighbors; expensive on large datasets |
Clustered or nonuniform data; brute-force distances can cost O(n² × d) |
| One-Class SVM | Learns a boundary enclosing normal observations | Flexible boundary and kernel methods | Sensitive to kernel and parameters; can be computationally expensive | Moderate, scaled datasets; solver-dependent cost |
| DBSCAN noise points | Marks low-density points as noise (-1) |
Finds arbitrary cluster shapes without specifying cluster count | Noise is not always an anomaly; density parameters matter | Clustered spatial data; often O(n log n), worst case O(n²) |
| Autoencoder reconstruction error | Trains a neural network to reconstruct normal inputs and flags high-error samples | Handles complex nonlinear relationships and high-dimensional data | Requires substantial data and tuning; may reconstruct anomalies | Images, sensor data, or sequences; cost scales with epochs, data size, and network size |
| Forecasting residuals | Flags observations whose actual values differ greatly from forecasts | Preserves temporal context | Sensitive to seasonality, drift, and forecast quality | Time-series monitoring; residual scoring is O(n) after model fitting |
Evaluation and Thresholds
-
contamination: A user-supplied estimate of the expected anomaly proportion that controls the decision threshold. It should reflect operational expectations and be validated when possible. -
Score percentiles: Flag observations whose anomaly scores exceed a chosen percentile when the expected anomaly rate is stable. Confirm each algorithm’s score direction before applying a threshold.
-
Labelled validation data: Select thresholds on training or validation data, then evaluate once on untouched test data. Avoid tuning on the final test set.
-
Cross-validation: Use
StratifiedKFoldfor labelled, imbalanced classification when each fold has enough anomaly examples. UseTimeSeriesSplitfor temporal data and preserve chronological ordering. -
precision@k: Measures how many of the
khighest-scoring alerts are true anomalies. This is useful when investigators can review only a limited number of alerts. -
PR-AUC and recall at a fixed false-positive rate: Both are useful for rare anomalies. Accuracy is misleading because a model can achieve high accuracy by predicting every observation as normal.
Feature scaling matters for distance- and gradient-based methods such as LOF and One-Class SVM. It is generally unnecessary for tree ensembles, although monotonic feature scaling does not change Isolation Forest’s split logic. Many models require finite values, so impute missing values using a procedure fitted only on training data; missingness itself may also be predictive.
Common Pitfalls
-
Data leakage: Fit preprocessing and detectors only on training data; use a pipeline or cross-validation.
-
Ignoring imbalance: Do not rely on accuracy; report precision, recall, F1, PR-AUC, and operational false-positive rates.
-
Wrong novelty behavior: LOF requires
novelty=Truebefore callingpredicton new observations;fit_predictis intended for identifying training outliers. -
Concept drift: Normal behavior changes over time. Monitor feature distributions and retrain or recalibrate thresholds.
-
Time-series structure: Account for seasonality and temporal dependencies before interpreting residual anomalies.
-
Alert fatigue: Too many false positives cause alerts to be ignored. Use thresholds based on investigation capacity and business cost.
-
Overfitting the validation set: Keep a final untouched test set and limit threshold and hyperparameter selection to training or validation folds.
-
Reproducibility: Fix random seeds, record library versions, and retain data-processing decisions for stochastic models and repeated experiments.