# How to Handle Imbalanced Data, Anomaly Detection, and Ensemble Methods in Production ML

> Learn to handle imbalanced data, anomaly detection, and ensemble methods in production ML. Discover techniques for rare event detection and continuous auditability in your AI pipelines.

- Repository: [Rohit Ghumare/ai-engineering-from-scratch](https://github.com/rohitg00/ai-engineering-from-scratch)
- Tags: how-to-guide
- Published: 2026-08-31

---

**Production ML pipelines require controlled synthetic data generation, isolation-based anomaly scoring, and score-averaging ensembles to reliably detect rare events while maintaining continuous auditability.**

Transitioning from experimental notebooks to production-grade ML systems demands rigorous handling of class imbalance, robust unsupervised detection, and ensemble strategies that combine multiple models without introducing opaque dependencies. The **rohitg00/ai-engineering-from-scratch** repository demonstrates how to implement these patterns using clean, handcrafted Python modules that expose every implementation detail for inspection and tuning.

## Generating Controlled Imbalanced Datasets

Before deploying anomaly detectors, you must validate strategies against datasets with known imbalance ratios. In [`phases/02-ml-fundamentals/17-imbalanced-data/code/imbalanced.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/02-ml-fundamentals/17-imbalanced-data/code/imbalanced.py), the `make_imbalanced_data` function creates synthetic binary classification data with configurable majority and minority class sizes.

```python
from phases.02_ml_fundamentals.17_imbalanced_data.code.imbalanced import make_imbalanced_data

# Create a 95:5 imbalanced split (950 majority, 50 minority)

X, y = make_imbalanced_data(n_majority=950, n_minority=50, seed=42)

print(f"Class distribution: {dict(zip(*np.unique(y, return_counts=True)))}")

```

This utility lets you test **cost-sensitive learning** and **resampling strategies** (SMOTE, ADASYN, or class weighting) under controlled conditions before touching production data.

## Implementing Isolation-Based Anomaly Detection

For production environments where labeled anomalies are scarce, the repository provides a from-scratch **Isolation Forest** implementation in [`phases/02-ml-fundamentals/16-anomaly-detection/code/anomaly_detection.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/02-ml-fundamentals/16-anomaly-detection/code/anomaly_detection.py). The `IsolationForest` class implements `fit`, `predict`, and `anomaly_score` methods, offering full transparency into the isolation mechanism.

```python
from phases.02_ml_fundamentals.16_anomaly_detection.code.anomaly_detection import IsolationForest
import numpy as np

# Initialize with expected contamination rate

iso_forest = IsolationForest(contamination=0.05)
iso_forest.fit(X)

# Generate anomaly scores (higher = more anomalous)

scores = iso_forest.anomaly_score(X)
predictions = iso_forest.predict(X)  # -1 for anomaly, 1 for normal

```

Unlike black-box libraries, this implementation allows you to modify the tree-building logic, adjust the subsampling size, or inject custom branching criteria to match your domain’s latency constraints.

## Evaluating Rare Event Detection with Precision@k

Standard accuracy metrics fail in imbalanced scenarios. The repository emphasizes **Precision@k** evaluation, implemented in [`phases/02-ml-fundamentals/09-model-evaluation/code/evaluation.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/02-ml-fundamentals/09-model-evaluation/code/evaluation.py). This metric measures the proportion of true positives within the top-k highest anomaly scores, directly reflecting business needs to surface the most suspicious cases first.

```python

# Calculate Precision@k for k=5%

k = int(0.05 * len(scores))
top_k_indices = np.argsort(scores)[-k:]
precision_at_k = np.mean(y[top_k_indices])

print(f"Precision@{k}: {precision_at_k:.2%}")

```

Use this approach to set alerting thresholds that minimize false positives while capturing critical anomalies in fraud detection, system monitoring, or quality control pipelines.

## Building Robust Ensembles in Production

Single detectors often miss edge cases that complementary models catch. The repository demonstrates ensemble techniques by averaging normalized anomaly scores from multiple detectors (e.g., combining Isolation Forest with One-Class SVM or autoencoder reconstructions).

```python

# Ensemble via score averaging (assuming scores2 from a second detector)

ensemble_scores = (scores + scores2) / 2.0

# Or use majority voting for binary predictions

ensemble_predictions = np.sign(scores + scores2)

```

Wrap your ensemble in the lightweight service boilerplate found in [`phases/02-ml-fundamentals/16-anomaly-detection/code/main.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/02-ml-fundamentals/16-anomaly-detection/code/main.py) to expose the model via an API endpoint with built-in logging and latency tracking.

## Validating Production Readiness with Automated Audits

Deployment requires deterministic validation. The [`scripts/audit_lessons.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/scripts/audit_lessons.py) script enforces that every detector ships with reproducible unit tests and documented performance benchmarks. Integrate this into your CI/CD pipeline to prevent regression when data distributions shift.

```bash

# Run audit to verify all lessons meet production standards

python scripts/audit_lessons.py

```

This audit checks that anomaly detection modules handle edge cases (empty inputs, single-feature datasets) and that imbalanced data utilities return consistent shapes across random seeds.

## Summary

- **Use `make_imbalanced_data`** from [`imbalanced.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/imbalanced.py) to generate controlled test datasets with specific majority-to-minority ratios.
- **Implement `IsolationForest`** from [`anomaly_detection.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/anomaly_detection.py) for transparent, unsupervised anomaly scoring that requires no labeled outliers.
- **Measure success with Precision@k** rather than accuracy to align evaluation with operational priorities for rare event detection.
- **Combine detectors** via score averaging or majority voting to reduce false negatives and improve robustness against adversarial inputs.
- **Enforce quality gates** using [`audit_lessons.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/audit_lessons.py) to ensure every model update passes deterministic tests before reaching production.

## Frequently Asked Questions

### Why is Precision@k preferred over accuracy for imbalanced anomaly detection?

Accuracy becomes meaningless when anomalies constitute less than 1% of data, as a trivial classifier predicting "normal" for every sample achieves 99% accuracy. **Precision@k** focuses exclusively on the top-k most suspicious predictions, measuring whether the highest anomaly scores actually correspond to true positives, which matches business requirements for investigative prioritization.

### How does the IsolationForest implementation differ from scikit-learn's version?

The repository's `IsolationForest` class in [`anomaly_detection.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/anomaly_detection.py) provides an educational implementation that exposes the recursive partitioning logic, subsampling mechanism, and path-length calculations as explicit methods. While scikit-learn optimizes for speed via Cython, this version allows direct modification of tree depth limits, feature selection strategies, and scoring normalization—essential when adapting the algorithm to streaming or federated learning scenarios.

### What is the best practice for combining multiple anomaly detectors in production?

Normalize anomaly scores from each detector to the [0,1] range using min-max scaling or percentile ranking, then compute a weighted average where weights reflect each model's historical Precision@k on validation data. Deploy this ensemble behind the [`main.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/main.py) service wrapper to ensure consistent input validation and centralized logging of which specific detector flagged each anomaly.

### How do automated audits ensure model reliability in CI/CD pipelines?

The [`audit_lessons.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/audit_lessons.py) script programmatically imports each lesson module, verifies that functions like `make_imbalanced_data` and `IsolationForest.fit` execute without errors on deterministic test inputs, and checks that output shapes match expectations. By running this audit on every pull request, teams catch breaking changes in data pipelines or model serialization before deployment, maintaining the contract between feature engineering and serving infrastructure.