Using Machine Learning to identify and prevent fraudulent transactions
# M-Pesa Fraud Detection System
A real-time transaction fraud detection and prevention system built for the Kenyan mobile money ecosystem. The system intercepts M-Pesa transactions before settlement and returns an `ALLOW`, `CHALLENGE`, or `BLOCK` decision within the latency window of a transaction initiation.
---
## Problem Statement
M-Pesa processes **37.15 billion transactions annually**, with a total value of **KSh 38.29 trillion**. Conservative estimates place annual fraud losses at **KSh 2.3 billion (~$17.6M USD)** (approximately KSh 6.3 million stolen every single day).
Common attack vectors include SIM swaps, social engineering, fraudulent merchant transactions, number masking, and fake balance SMS traps.
---
## Solution
Rather than logging fraud after the fact, this system sits **between transaction initiation and transaction completion**, scoring each transaction in real time and returning one of three decisions:
| Decision | Condition |
|---|---|
| `ALLOW` | Low fraud probability: transaction proceeds normally |
| `CHALLENGE` | Elevated risk: OTP or user verification triggered |
| `BLOCK` | High fraud probability or deterministic fraud signal: transaction halted before settlement |
---
## Project Structure
```text
Mpesa-Fraud-Detection-System/
├── Data/
│ ├── mpesa_synthetic.csv: Raw dataset (120K synthetic M-Pesa transactions)
│ ├── Feature_engineered.csv: Engineered dataset
│ ├── training.csv: Training split (109,915 rows)
│ └── evaluation.csv: Unseen evaluation set (10,000 rows)
├── inference/
│ ├── models/
│ │ ├── best_model.pkl: Trained XGBoost model (GridSearchCV tuned)
│ │ └── encoder.pkl: Fitted ColumnTransformer encoder
│ ├── utils/
│ │ ├── feature_engineering.py: Derives drain_rate, account_emptied, cyclic encoding
│ │ └── preprocessing.py: Drops columns, maps device_type, applies encoder
│ ├── main.py: FastAPI application
│ ├── requirements.txt: Production dependencies
│ ├── .env: Local environment v …