# Anomaly and Outlier Detection

> Finding rare, unusual observations with statistical rules, Isolation Forest, LOF and one-class methods when labels are scarce. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/anomaly-detection
Last updated: 2026-11-28

Finding rare, unusual observations with statistical rules, Isolation Forest, LOF and one-class methods when labels are scarce.

Intermediate · about 4 hours · difficulty 3/5

Learn first: [k-Means Clustering](https://www.internhack.in/roadmaps/tracks/machine-learning/k-means)

Anomaly detection flags data points that do not fit the usual pattern: fraudulent transactions, failing machines, intrusions, data-quality errors. Labels are usually rare or missing, so methods are largely unsupervised.

### Methods

- **Statistical rules**: z-scores, IQR fences, rolling thresholds for time series. Simple and often enough.
- **Isolation Forest**: anomalies are isolated in fewer random splits; fast, scales well, a strong default.
- **Local Outlier Factor**: compares local density with neighbours; good for varying densities.
- **One-class SVM**: learns a boundary around normal data; sensitive to parameters.
- **Reconstruction error** from PCA or autoencoders: high error suggests an anomaly.

### Practical concerns

- The `contamination` parameter sets how many points get flagged; tie it to review capacity.
- Evaluate with any labels you do have (precision at top k) or with human review of the top flagged cases.
- If you gather enough labelled anomalies, a supervised classifier usually beats unsupervised detection.

**Good enough to move on** when you can build an anomaly detector, rank cases by score and evaluate precision at k against held-out labels.

## Key concepts

- **Statistical thresholds**
- **Isolation Forest**
- **Local Outlier Factor**
- **One-class SVM**
- **Reconstruction-based detection**
- **Precision at k evaluation**

## Mini project

Train Isolation Forest and LOF on the Kaggle Credit Card Fraud data without using labels, then use the labels only to compute precision at the top 500 flagged transactions.

## Check yourself

When would you switch from an unsupervised anomaly detector to a supervised classifier?

## Free resources

- [scikit-learn: Novelty and outlier detection](https://scikit-learn.org/stable/modules/outlier_detection.html) (scikit-learn)
- [PyOD documentation](https://pyod.readthedocs.io/en/latest/) (PyOD)

Previous: [t-SNE and UMAP for Visualization](https://www.internhack.in/roadmaps/tracks/machine-learning/tsne-umap) · Next: [Perceptrons and Multilayer Networks](https://www.internhack.in/roadmaps/tracks/machine-learning/neural-networks-basics)

## More in Finding Structure Without Labels

- [k-Means Clustering](https://www.internhack.in/roadmaps/tracks/machine-learning/k-means)
- [Hierarchical and Density-Based Clustering](https://www.internhack.in/roadmaps/tracks/machine-learning/hierarchical-and-density-clustering)
- [Principal Component Analysis](https://www.internhack.in/roadmaps/tracks/machine-learning/pca)
- [t-SNE and UMAP for Visualization](https://www.internhack.in/roadmaps/tracks/machine-learning/tsne-umap)
- [Anomaly and Outlier Detection](https://www.internhack.in/roadmaps/tracks/machine-learning/anomaly-detection)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
