Anomaly Detection — Isolation Forest and Autoencoders
Detect unusual observations with Isolation Forest and reconstruction error, then choose thresholds with labels and operational costs in mind.
An unusual point is not automatically a bad point
Before this lesson, know train/test splits, feature scaling, decision thresholds and basic classification metrics. Anomaly detection is useful when unusual events are rare, expensive to label or not fully known in advance. The model produces a score or flag—not a verified explanation. You still need a review process and evidence that alerts help.
- Distinguish outlier detection, novelty detection and supervised rare-event classification.
- Train Isolation Forest and an autoencoder without leaking held-out data.
- Select an alert threshold using validation evidence and operational costs.
Examples include unexpected payment patterns, a sensor reading that departs from normal machine behavior, or a sudden shift in API latency. Some tasks have a clear normal-only training set; others include a mixture of normal and anomalous observations. That distinction changes the modelling approach and what the score means.
| Task framing | Training data | Typical question |
|---|---|---|
| Outlier detection | A general dataset that may contain anomalies | Which rows look unusual relative to this sample? |
| Novelty detection | A curated set of normal observations | Does this new row depart from the learned normal region? |
| Supervised rare-event classification | Representative labelled examples of both classes | Which event is most likely to belong to a known positive class? |
Unusual points take fewer random splits to isolate
Isolation Forest repeatedly chooses a feature and a random split value. A point that differs strongly from the bulk of observations tends to be separated after fewer splits. Across many randomized trees, shorter average path lengths indicate more anomalous observations. The method is attractive for tabular data because it does not need labelled examples and can handle many feature dimensions.
Scikit-learn's IsolationForest exposes scores with a sign convention worth checking: decision_function values below zero are classified as outliers by the fitted threshold; predict returns -1 for outlier and 1 for inlier. A score is a ranking signal, not a probability that an event is malicious or erroneous.
contamination to a guessed anomaly rate bakes that assumption into the default threshold. If the true rate is unknown or changes over time, keep scores, validate thresholds using reviewed examples, and track alert volume separately.An autoencoder learns to reconstruct familiar inputs
An autoencoder maps input features into a smaller representation and then reconstructs them. Train it primarily on normal examples. If the network captures normal structure, an unfamiliar example may have larger reconstruction error. The score for row x can be the mean squared difference between input and reconstruction, though feature scales and the loss definition matter.
The network is powerful enough to reconstruct many inputs—including anomalies—if it is too wide, too deep, or trained too long. A high reconstruction error can also come from a valid but rare normal subgroup. The score is a triage signal, not a semantic explanation. We use a small scikit-learn MLP to demonstrate the architecture without assuming the later PyTorch lessons.
The code reserves a normal-only calibration subset and uses validation labels only to assess ranking. Keep the final test set untouched until the approach and threshold are chosen. Fit the scaler on normal training rows, not on future observations. A bottleneck and regularisation can encourage useful compression, but tune model capacity on development data and review false alarms, not just reconstruction loss.
Turn a ranking score into an alert policy
Both methods produce continuous scores. A threshold determines how many rows are raised for review. Choose it on validation or calibration data—not on the final test set. When you have labelled examples, plot precision and recall at candidate thresholds and estimate expected workload. When labels are unavailable, involve domain experts to review samples across score bands and document the uncertainty.
Average precision is often more informative than ROC-AUC when the positive class is very rare, but it still depends on prevalence. Report the test-set prevalence, confusion counts at the chosen threshold, precision-recall curve and relevant slice metrics. In a human-review workflow, also report alerts per day, average review time and the cost of missed events.
An anomaly score should lead to a useful, safe next step
Decide whether each alert is automatically blocked, queued for human review, or simply logged for aggregate analysis. For high-impact actions, prefer review and recourse until evidence supports a narrower automation. Include enough context for an investigator to understand why a row was flagged, while limiting sensitive data and avoiding explanations that overstate what the detector learned.
Monitor the fraction of traffic flagged, score distributions, input missingness and the rate at which reviewed cases are confirmed. If confirmed labels arrive later, track review delays. Set owners and response thresholds for sudden alert spikes or drops, and keep a fallback path when model features are unavailable. A model retrained on reviewed alerts can inherit selective-label bias, so audit which examples receive review.
Treat anomaly flags as uncertain evidence
Build an anomaly workflow, not just an estimator
- Create a normal-only training set and a separate labelled validation set. Fit Isolation Forest and compare its scores with a simple distance or rule-based baseline.
- Train a small autoencoder on normal rows. Plot reconstruction-error distributions for normal and known anomalous examples.
- Choose a threshold to meet a review capacity or recall objective. Report the corresponding precision and number of alerts.
- Inspect false positives and false negatives. Group the review by a relevant segment and note any sensitive proxies or data-quality patterns.
- Write a deployment checklist covering score logging, drift, model refresh, human review and a safe fallback when data is missing.
Isolation Forest isolates rare points with random partitions; autoencoders flag inputs that are hard to reconstruct from learned normal patterns. Both depend on representation, training data and threshold choices. Validate with labels where possible, report alert costs and limits, and never treat an anomaly score as proof of intent or harm.
Next, reduce correlated dimensions and understand how variance-preserving projections affect analysis.
🎯 Key Takeaways
- ✓Separate outlier detection from novelty detection and supervised rare-event classification; they make different assumptions about training data.
- ✓Isolation Forest ranks unusual points by how easily random partitions isolate them.
- ✓An autoencoder uses reconstruction error as a novelty signal, but can reconstruct anomalies and over-flag rare normal cases.
- ✓Choose score thresholds using development evidence and operational costs; do not treat scores as probabilities.
- ✓Monitor alert volume, input drift, reviewed outcomes and group-level effects after deployment.
HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
