An industry-grade machine learning pipeline for detecting fraudulent financial transactions in real-time. Built to mirror production-level fraud detection systems used by banks and e-commerce platforms, this project addresses severe class imbalance, engineers domain-specific fraud indicators, and compares multiple classifiers using industry-standard evaluation metrics.
| Dashboard | ROC & PR Curves |
|---|---|
![]() |
![]() |
Financial institutions and e-commerce platforms face increasing fraudulent transactions resulting in major financial losses. Rule-based systems fail to adapt to evolving fraud patterns. This project builds a machine learning model that identifies suspicious transactions based on behavioral signals and historical patterns.
Raw Transaction Data
│
▼
┌─────────────────────┐
│ Data Generation / │ ← 50,000 transactions, 3.1% fraud rate
│ Ingestion │
└────────┬────────────┘
│
▼
┌─────────────────────┐
│ Preprocessing │ ← Label encoding, StandardScaler, stratified split
└────────┬────────────┘
│
▼
┌─────────────────────┐
│ Class Imbalance │ ← SMOTE-style oversampling + class_weight='balanced'
│ Handling │
└────────┬────────────┘
│
▼
┌─────────────────────┐
│ Feature Engineering │ ← 6 domain fraud indicators
└────────┬────────────┘
│
▼
┌─────────────────────┐
│ Model Training │ ← 4 classifiers trained & compared
└────────┬────────────┘
│
▼
┌─────────────────────┐
│ Evaluation & │ ← ROC-AUC, PR-AUC, F1, Precision, Recall
│ Visualisation │
└────────┬────────────┘
│
▼
Output Reports + Charts
| Feature | Description |
|---|---|
IsNightTxn |
Flags transactions between 10 PM – 4 AM (high-risk window) |
AmountZScore |
Standardised transaction amount (detects outlier spending) |
HighAmountFlag |
1 if transaction amount > 95th percentile |
TransactionCount_24h |
Rolling count of transactions in a 24-hour window |
FrequencyFlag |
1 if more than 8 transactions in 24 hours |
RiskScore |
Composite weighted risk indicator (0–1 scale) |
| Model | Notes |
|---|---|
| Logistic Regression | Baseline linear classifier |
| Decision Tree | Interpretable rule-based splits |
| Random Forest | Ensemble — strong feature importance |
| Gradient Boosting | Boosted ensemble — typically best performer |
class_weight='balanced' — applied to all scikit-learn estimators to penalise misclassification of the minority class| Model | ROC-AUC | PR-AUC | F1 | Precision | Recall |
|---|---|---|---|---|---|
| Logistic Regression | 1.0000 | 1.0000 | 0.9984 | 1.0000 | 0.9968 |
| Decision Tree | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| Random Forest | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
| Gradient Boosting | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |
Note: Near-perfect scores reflect the synthetic dataset's clean separation. On real-world data (e.g. Kaggle Credit Card Fraud dataset), expect ROC-AUC in the 0.95–0.99 range with meaningful differentiation between models.
pip install scikit-learn pandas numpy matplotlib seaborn
https://github.com/RussanaMary/Fraud-Detection
cd fraud-detection-ml
python Fraud.py
Three files are saved to the project directory:
fraud_detection_dashboard.png ← 5-panel visual dashboard
roc_pr_curves.png ← ROC & Precision-Recall curves
fraud_detection_report.md ← Full project documentation
fraud-detection-ml/
│
├── Fraud.py # Main pipeline (single file, fully self-contained)
├── fraud_detection_dashboard.png # Generated dashboard
├── roc_pr_curves.png # Generated ROC/PR curves
├── fraud_detection_report.md # Generated report
└── README.md # This file
To swap in the Kaggle Credit Card Fraud dataset:
creditcard.csv and place it in the project foldergenerate_transaction_data() call in main() with:df = pd.read_csv("creditcard.csv")
df = df.rename(columns={"Time": "Hour"}) # align column names
The rest of the pipeline is plug-and-play.
$ claude mcp add Fraud-Detection \
-- python -m otcore.mcp_server <graph>