Skip to content

mrudula1501/fraud-detection-ml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ’ณ Fraud Detection ML System

Python XGBoost Scikit-learn SHAP License

Machine learning fraud detection system achieving 99.92% ROC-AUC on 6.3M+ financial transactions. Combines XGBoost, LightGBM, and Logistic Regression in a stacking ensemble with SMOTE for class imbalance.


๐ŸŽฏ Problem Statement

Financial fraud costs institutions over $32 billion annually. Traditional rule-based systems fail because they:

  • Generate 90%+ false positives, alienating legitimate customers
  • Miss novel fraud patterns (low recall on unseen schemes)
  • Can't scale to millions of transactions per day

This system uses an ML ensemble to achieve near-perfect AUC while remaining interpretable through SHAP explanations.


๐Ÿ“Š Dataset

Metric Value
Total Transactions 6,362,620
Fraudulent 8,213 (0.129%)
Legitimate 6,354,407 (99.871%)
Features 11
Time Period 30 days simulated

Source: Kaggle โ€” Online Payments Fraud Detection


๐Ÿ—๏ธ System Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     DATA INGESTION                               โ”‚
โ”‚  Real-time transaction stream (Kafka) OR Batch CSV              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                           โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   FEATURE ENGINEERING                            โ”‚
โ”‚  โ€ข Transaction velocity (amount / time since last)              โ”‚
โ”‚  โ€ข Balance change ratios (new/old)                              โ”‚
โ”‚  โ€ข Hour-of-day / day-of-week cyclical encoding                  โ”‚
โ”‚  โ€ข Merchant category risk scores                                โ”‚
โ”‚  โ€ข Customer behavioral patterns (rolling averages)              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                           โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    MODEL ENSEMBLE                                โ”‚
โ”‚                                                                  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”           โ”‚
โ”‚  โ”‚   XGBoost   โ”‚   โ”‚  LightGBM   โ”‚   โ”‚  Logistic   โ”‚           โ”‚
โ”‚  โ”‚  (Primary)  โ”‚   โ”‚ (Secondary) โ”‚   โ”‚  Regression โ”‚           โ”‚
โ”‚  โ”‚             โ”‚   โ”‚             โ”‚   โ”‚ (Baseline)  โ”‚           โ”‚
โ”‚  โ”‚ โ€ข SMOTE     โ”‚   โ”‚ โ€ข SMOTE     โ”‚   โ”‚ โ€ข SMOTE     โ”‚           โ”‚
โ”‚  โ”‚ โ€ข scale_pos โ”‚   โ”‚ โ€ข class_wt  โ”‚   โ”‚ โ€ข class_wt  โ”‚           โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜           โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ”‚
โ”‚                           โ–ผ                                     โ”‚
โ”‚                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                         โ”‚
โ”‚                โ”‚  STACKING           โ”‚                         โ”‚
โ”‚                โ”‚  Meta-learner:      โ”‚                         โ”‚
โ”‚                โ”‚  XGBoost            โ”‚                         โ”‚
โ”‚                โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                           โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Fraud Score (0โ€“1)  โ†’  Alert (if >0.7)  โ†’  Case Management     โ”‚
โ”‚  Threshold: 0.7 (optimized for F-beta=2)                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽฏ Model Performance

Final Results (Test Set)

Metric Score Interpretation
ROC-AUC 0.9992 Near-perfect discrimination
Precision 0.96 96% of flagged cases are real fraud
Recall 0.94 Catches 94% of all fraud
F1-Score 0.95 Balanced precision-recall
F2-Score 0.945 Optimized for recall (fraud focus)
Average Precision 0.98 Excellent ranking quality

Confusion Matrix

              Predicted
           Fraud    Legit
Actual
Fraud       1,542      98    โ† 94% recall
Legit          64  1,270,296 โ† 99.995% specificity

Baseline Comparison

Model ROC-AUC Recall Precision
Random Guess 0.50 0.50 0.001
Logistic Regression 0.89 0.72 0.85
Random Forest 0.97 0.85 0.91
XGBoost (tuned) 0.9992 0.94 0.96
Ensemble (XGB+LGBM+LR) 0.9992 0.94 0.96

๐Ÿš€ Quick Start

Installation

git clone https://github.com/mrudula1501/fraud-detection-ml.git
cd fraud-detection-ml

python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

pip install -r requirements.txt

# Download dataset
kaggle datasets download -d rupakroy/online-payments-fraud-detection-dataset
unzip online-payments-fraud-detection-dataset.zip -d data/

Usage

from src.fraud_detector import FraudDetector

detector = FraudDetector(model_path='models/xgboost_fraud_v1.pkl')

transaction = {
    'step': 100,
    'type': 'TRANSFER',
    'amount': 181.00,
    'oldbalanceOrg': 181.00,
    'newbalanceOrig': 0.00,
    'oldbalanceDest': 0.00,
    'newbalanceDest': 0.00
}

result = detector.predict(transaction)
print(result)
# {
#   'is_fraud': True,
#   'fraud_probability': 0.987,
#   'confidence': 'HIGH',
#   'explanation': {
#     'top_features': ['amount', 'balance_change_ratio', 'type_TRANSFER'],
#     'shap_values': [...]
#   }
# }

Batch Processing

import pandas as pd

df = pd.read_csv('data/transactions.csv')
predictions = detector.predict_batch(df, batch_size=10000)
predictions.to_csv('fraud_predictions.csv', index=False)
print(f"Flagged {predictions['is_fraud'].sum()} potential fraud cases")

Model Training

python train.py \
  --data data/PS_20174392719_1491204439457_log.csv \
  --model xgboost \
  --tune \
  --output models/

python evaluate.py --model models/xgboost_fraud_v1.pkl --test data/test.csv

๐Ÿ” Feature Engineering

Feature Formula Importance
balance_change_ratio (new โˆ’ old) / old 0.23
amount_velocity amount / hours_since_last_txn 0.19
type_TRANSFER One-hot encoding 0.15
hour_of_day sin/cos encoding 0.12
merchant_risk_score Historical fraud rate 0.11
customer_txn_count_24h Rolling window count 0.08

โš–๏ธ Handling Class Imbalance

With only 0.129% fraud, a naive model predicting "all legitimate" gets 99.87% accuracy but catches 0% of fraud. We solve this with:

Technique Tool Effect
SMOTE imbalanced-learn Synthetic minority oversampling
scale_pos_weight XGBoost param Cost-sensitive learning
Threshold tuning F-beta optimization Recall-focused decisions
Stacking ensemble Meta-learner Reduced variance

We optimize for F2-Score (weights recall 2x over precision) because missing fraud costs far more than a false alarm.


๐Ÿ“ Project Structure

fraud-detection-ml/
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ raw/                   # Original Kaggle dataset
โ”‚   โ””โ”€โ”€ processed/             # Feature-engineered data
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ features/
โ”‚   โ”‚   โ””โ”€โ”€ build_features.py
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ”œโ”€โ”€ train_xgboost.py
โ”‚   โ”‚   โ”œโ”€โ”€ train_lightgbm.py
โ”‚   โ”‚   โ””โ”€โ”€ ensemble.py
โ”‚   โ”œโ”€โ”€ explainability/
โ”‚   โ”‚   โ””โ”€โ”€ shap_explainer.py
โ”‚   โ””โ”€โ”€ fraud_detector.py      # Main API class
โ”œโ”€โ”€ notebooks/
โ”‚   โ”œโ”€โ”€ 01_eda.ipynb
โ”‚   โ”œโ”€โ”€ 02_feature_engineering.ipynb
โ”‚   โ”œโ”€โ”€ 03_model_comparison.ipynb
โ”‚   โ””โ”€โ”€ 04_explainability.ipynb
โ”œโ”€โ”€ models/
โ”‚   โ””โ”€โ”€ xgboost_fraud_v1.pkl
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ Dockerfile
โ””โ”€โ”€ README.md

๐Ÿ”ฎ Future Enhancements

  • Graph Neural Networks for transaction network modeling
  • Neo4j for real-time relationship analysis
  • AutoML with Featuretools for automated feature engineering
  • Federated learning across institutions without data sharing
  • Isolation Forest for unsupervised anomaly detection

๐Ÿ“š References


๐Ÿ“„ License

MIT License โ€” see LICENSE

โš ๏ธ Disclaimer: Research project. Not for production financial use without proper compliance review.


๐Ÿ“ฌ Contact

Mrudula Deshmukh

GitHub Portfolio LinkedIn Email

About

Machine learning fraud detection system achieving 99.92% ROC-AUC on 6.3M+ transactions

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors