End-to-end financial fraud detection for M-Pesa, bank, and KRA transactions — XGBoost + SMOTE on synthetic Kenyan data, with a live Streamlit demo.
# 🛡️ FraudShield Kenya — Financial Fraud Detection with XGBoost + SMOTE
An end-to-end machine learning pipeline that detects financial fraud across **M-Pesa mobile money**, **bank account transactions**, and **KRA tax filings** using synthetic Kenyan financial data.
Handles extreme class imbalance (fraud 8` |
| **Large unusual** | Single amount far above norm | `ratio_to_avg_amount > 12` |
| **Account takeover** | New device + location + large transfer | `new_device + location_mismatch` |
| **KRA evasion** | Declared income far below expected | `declared_vs_expected_ratio < 0.15` |
| **Structuring** | Just-below-threshold transactions (AML) | `amount ≈ KES 99,000` |
---
## Key Features Engineered
```python
ratio_to_avg_amount # Is this transaction 10x larger than their normal?
velocity_score # (txns_last_hour × 3) + txns_last_day
risk_score # Composite: ratio + device + location + time signals
tax_risk # (1 - declared_ratio) + (days_late / 365)
account_maturity # log(account_age_days) — newer accounts riskier
is_night # Transactions between midnight and 5 AM
```
---
## Results
| Model | ROC-AUC | Avg Precision | F1 (Fraud) |
|---|---|---|---|
| XGBoost | **1.0000** | **1.0000** | **0.9934** |
| LightGBM | **1.0000** | **1.0000** | **0.9934** |
*Note: High AUC reflects strong synthetic signal injection. Real-world performance will vary.*
---
## Project Structure
```
fraud_detection/
├── 1_generate_data.py # Synthetic data generator (Faker + Kenyan context)
├── 2_train_model.py # SMOTE + XGBoost + LightGBM + SHAP
├── 3_app.py # Streamlit live demo
├── data/
│ ├── kenya_fraud_dataset.csv # 50,000 synthetic transactions
│ └── test_set.csv # Held-out test set with predictions
├── models/
│ ├── best_model.pkl # Saved XGBoost model
│ └── feature_cols.pkl # Feature column list
├── outputs/
│ ├── confusion_matrix_xgboost.png
│ ├── roc_ …