Multivariate Anomaly Detection on Time-Series Data in Python: Using Isolation Forests to Detect Credit Card Fraud

Isolation Forest detects unusual observations by measuring how quickly random trees isolate them. This updated tutorial uses a deterministic transaction fixture, an explicit contamination assumption, current scikit-learn APIs, and known injected anomalies for evaluation.
Fit the detector
model = IsolationForest(
n_estimators=250,
contamination=EXPECTED_ANOMALY_SHARE,
random_state=RANDOM_SEED,
)
predictions = model.fit_predict(features)
The model is unsupervised during fitting. Fixture labels are used only afterward to calculate precision, recall, and a confusion matrix.

An anomaly is not automatically fraud. Real fraud detection needs time-aware validation, investigation feedback, threshold selection based on operational costs, monitoring for drift, and controls that prevent automated adverse decisions without review.




2 Commentsarchived from the original site