Work / NaijaShield: Fraud Detection

Machine Learning

NaijaShield: Real-Time Fraud Detection for Nigerian Payments

An open-source, end-to-end fraud detection pipeline built on 5 million Nigerian payment transactions, covering leakage-safe feature engineering, imbalance-aware modelling, SHAP explanations and a cost model in naira.

Type Open-source projectData 5M synthetic NGN transactionsModels XGBoost · LightGBM · SMOTEStack Python · MLflow · FastAPI · DockerYear 2026
Test-set confusion matrix: 93.9% of fraud caught, 76.6% of legitimate payments flaggedScreenshot comingimages/fraud-detection.png

01Problem

Nigerian payments have their own fraud patterns. USSD and mobile channels, SIM-swap takeovers, BVN-linked identity fraud and attacks timed to salary week don't look like the card fraud most public models are trained on.

NaijaShield is my attempt to build the whole pipeline for that context: from raw transactions to a model registered in MLflow and ready to serve behind an API.

5MTransactions, 45 raw features
3.6%Fraud rate (27:1 imbalance)
93.9%Fraud caught on the test set
92%Modelled cost saving vs no model

02Dataset

The dataset is a public synthetic Nigerian financial-transactions dataset (Hugging Face). It has 5,000,000 transactions from January 2023 to January 2024, with 45 attributes covering payment channel, merchant, location, device, IP, velocity and user history.

  • I split the data chronologically into train, validation and test sets (3.75M / 625K / 625K), so the model is always tested on the future, never the past.
  • The fraud rate is a consistent 3.6% in every split, with 134,539 fraud cases in training.

03Feature engineering

  • Leakage audit: dropped fraud_type, which is only known after the fact, along with a constant column and perfectly duplicated velocity features.
  • 13 new context features: amount relative to the user's normal spending (ratio and z-score), velocity relative to typical gaps, device novelty, new device + large amount, new device + night-time, new-user flag and account maturity.
  • Missing history handled explicitly: a has_prior_transaction flag instead of silent imputation.
  • One-hot encoding for low-cardinality fields, and smoothed target encoding for merchant, location and category, computed on the training fold only. The result is 58 model features.

04Modelling

I compared three imbalance strategies, each tracked as an MLflow experiment, and used AUC-PR as the primary metric because accuracy is meaningless at 27:1.

Model (validation)AUC-PRRecallPrecision
XGBoost, scale_pos_weight0.04440.9600.044
LightGBM, balanced weights0.04420.9830.044
XGBoost + SMOTE (10:1)0.04431.0000.044

XGBoost with class weighting won narrowly. It was evaluated once on the untouched test set (AUC-PR 0.044, recall 93.9%, threshold 0.53) and promoted to Staging in the MLflow model registry, with a model card.

05Explainability

SHAP, computed on a 2,000-transaction sample with fraud deliberately oversampled, shows the model leans almost entirely on two signals: whether the sender has any prior transaction, and whether this is a new device. I also wrote per-transaction explanations for true positives, false positives and missed fraud, so an analyst can see why each case was flagged.

06Errors and business cost

Metrics alone don't tell a payments team what to do, so I priced the errors in naira. A missed fraud costs about ₦599K (an average ₦748K transaction, with 20% recovered), and a wrongly blocked payment costs about ₦500 in operations.

  • With no model, missed fraud on the test set costs about ₦13.5B. With the model, about ₦12.4B of that is prevented, a 92% saving.
  • The catch is volume. 76.6% of legitimate payments get flagged, and only 4.4% of flags are real fraud. A threshold sweep found 0.37 to be cost-optimal.

07Architecture

  • Experiment tracking and registry: MLflow, covering runs, parameters, metrics, versioned models and stage promotion.
  • Serving: a FastAPI scoring service with Prometheus and Grafana monitoring, packaged with Docker Compose.
  • Design target: under 100 ms end-to-end latency for real-time decisions. This is a goal for the serving layer, not yet a measured benchmark.

08What I learned

The honest result: an AUC-PR of 0.044 is only slightly above the 0.036 baseline. The synthetic data carries very little fraud signal beyond device novelty and transaction history, and every model hit the same ceiling. Tuning the algorithm couldn't fix that.

The pipeline is the part that transfers: chronological splits, a leakage audit, fold-safe encoding, cost-based thresholds, explanations and a registered model. Next is retraining on richer or real-world data, then adding an anomaly-detection layer for fraud patterns the model hasn't seen.

← Back to all work