Anomaly Detection: Statistical and Learning-Based Methods
Module: Machine Learning | Difficulty: Advanced
Isolation Forest
Anomalies are easier to isolate. Path length :
where .
One-Class SVM
Autoencoder
Comparison
| Method | Assumes Distribution | Scalability | Accuracy |
|---|---|---|---|
| Isolation Forest | No | High | High |
| One-Class SVM | No | Low | Medium |
| Autoencoder | No | High | High |
| Mahalanobis | Yes | High | Medium |
import numpy as np
from sklearn.ensemble import IsolationForest
class AutoencoderAnomaly:
def __init__(self, encoder, decoder, threshold_percentile=95):
self.encoder = encoder; self.decoder = decoder
self.threshold_percentile = threshold_percentile
def fit(self, X):
recon = self.decoder(self.encoder(X))
errors = np.mean((X - recon)**2, axis=1)
self.threshold = np.percentile(errors, self.threshold_percentile)
def predict(self, X):
recon = self.decoder(self.encoder(X))
errors = np.mean((X - recon)**2, axis=1)
return errors > self.threshold
Research Insight: Isolation forest works because anomalies have shorter average path lengths in random trees. The key insight is that anomalies are few and different, so they are isolated faster than normal points.