Anomaly Detection Cheat Sheet
Explains point, contextual, and collective anomalies, key detection algorithms like One-Class SVM and autoencoders, and how to evaluate results on imbalanced data.
2 PagesIntermediateMar 2, 2026
Types of Anomalies
Categorize the anomaly before picking a technique.
- Point anomaly- A single data instance that is far from the rest of the data (e.g., one huge transaction)
- Contextual anomaly- Normal in general but abnormal given a specific context (e.g., high heating usage in summer)
- Collective anomaly- A group of related instances that is anomalous together, even if individual points look normal
- Global vs. local- Global anomalies deviate from the whole dataset; local anomalies deviate only from their neighborhood
One-Class SVM
Learn a boundary around normal data using only non-anomalous training examples.
python
from sklearn.svm import OneClassSVM# Train only on data assumed to be normalclf = OneClassSVM(kernel='rbf', nu=0.05, gamma='scale')clf.fit(X_train_normal)preds = clf.predict(X_test) # 1 = normal, -1 = anomaly
Autoencoder Reconstruction Error
Flag inputs the network reconstructs poorly as anomalies.
python
import torch, torch.nn as nnclass Autoencoder(nn.Module): def __init__(self, in_dim, latent_dim=8): super().__init__() self.encoder = nn.Sequential(nn.Linear(in_dim, 32), nn.ReLU(), nn.Linear(32, latent_dim)) self.decoder = nn.Sequential(nn.Linear(latent_dim, 32), nn.ReLU(), nn.Linear(32, in_dim)) def forward(self, x): return self.decoder(self.encoder(x))# After training on normal data only:recon = model(x_batch)error = torch.mean((recon - x_batch) ** 2, dim=1)anomalies = error > threshold # threshold set from validation error distribution
Evaluating Anomaly Detectors
Anomaly datasets are almost always heavily imbalanced.
- Precision@k- Fraction of the top-k flagged points that are true anomalies; useful when review capacity is limited
- Recall- Fraction of true anomalies that were caught; often prioritized when missed anomalies are costly
- PR-AUC- Area under the precision-recall curve; more informative than ROC-AUC on rare-class problems
- F1 / F-beta score- Harmonic mean of precision and recall; use F-beta to weight recall higher when misses are costly
Pro Tip
Never tune the anomaly threshold on the test set -- pick it from a held-out validation set's score distribution, then evaluate once on test to get an honest estimate of production performance.
Was this cheat sheet helpful?
Explore Topics
#AnomalyDetection#AnomalyDetectionCheatSheet#DataScience#Intermediate#TypesOfAnomalies#OneClassSVM#AutoencoderReconstructionError#EvaluatingAnomalyDetectors#OOP#Algorithms#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance