A complete end-to-end machine learning pipeline for handling highly imbalanced tabular data using Neural Networks and LightGBM, with advanced techniques such as threshold optimization, probability calibration, and model stacking.
This project demonstrates how to properly design, train, and evaluate machine learning models for extreme class imbalance problems (e.g., fraud detection).
It includes:
- Neural Network (TensorFlow)
- LightGBM (tree-based boosting)
- PR-AUC optimization (critical for imbalance)
- Threshold tuning (F1 / F2)
- Probability calibration (Isotonic Regression)
- Model stacking (NN + LightGBM)
- End-to-end pipeline (data → model → evaluation → deployment artifacts)
- Proper handling of imbalanced data
- Comparison of deep learning vs gradient boosting
- Production-oriented evaluation (not just accuracy)
- Clean and reproducible experimental setup
- Fully connected architecture
- Batch Normalization + Dropout
- AdamW optimizer with LR scheduling
- Class weighting for imbalance
- Raw tabular input (no scaling)
- scale_pos_weight for imbalance
- Early stopping based on PR-AUC
- Hyperparameter tuning (small search)
- Threshold tuning using F-beta (F1 / F2)
- Probability calibration (Isotonic Regression)
- Stacking (meta-model with Logistic Regression)
- ROC-AUC (ranking performance)
- PR-AUC / Average Precision (primary metric for imbalance)
- Precision / Recall / F1-score
- Confusion Matrix (threshold-based evaluation)
In highly imbalanced datasets:
- Accuracy is misleading
- ROC-AUC can be overly optimistic
PR-AUC focuses on the minority class and is more reliable.
- Generate imbalanced dataset
- Train Neural Network (scaled data)
- Train LightGBM (raw data)
- Evaluate baseline models
- Optimize LightGBM (PR-AUC tuning)
- Tune decision threshold (F-beta)
- Apply probability calibration
- Build stacking model (NN + LightGBM)
- Compare all models
- LightGBM achieves performance comparable to Neural Networks
- PR-AUC is significantly improved after tuning
- Threshold optimization drastically improves recall
- Stacking provides additional performance gains
tabular-imbalance-benchmark.ipynb
artifacts_advanced/
├── scaler.joblib
├── nn_model.keras
├── lgbm_base.joblib
├── lgbm_best.joblib
├── lgbm_isotonic.joblib
├── meta_logreg.joblib
├── y_test.npy
├── predictions...
- Python
- TensorFlow / Keras
- LightGBM
- Scikit-learn
- NumPy / Pandas
- Matplotlib
- Fraud Detection
- Credit Risk Modeling
- Medical Diagnosis
- Anomaly Detection
- Rare Event Prediction
- Tree-based models (LightGBM) are extremely strong for tabular data
- Neural Networks can match performance with proper tuning
- Threshold tuning is critical in real-world systems
- Calibration improves decision-making reliability
- Stacking combines strengths of multiple models
Mohammad Amiri
AI / ML Engineer
Give it a star and feel free to fork!