Skip to content

Repository files navigation

πŸ›‘οΈ FinShield Fraud Detection Platform

CI Python FastAPI Scikit-Learn XGBoost MLflow Pytest Docker Status

πŸš€ A production-style fraud detection platform for fintech use cases, built with Python, FastAPI, machine learning, rules engines, anomaly detection, and MLOps.


✨ Vision

FinShield is designed to simulate how modern fintech fraud detection systems are built in real-world environments.

Instead of training a single notebook model, this project combines:

  • 🧠 supervised fraud classification for known fraud patterns
  • πŸ•΅οΈ anomaly detection for unknown suspicious behavior
  • πŸ“ rules-based scoring for interpretable business logic
  • βš–οΈ hybrid scoring for final risk decisions
  • πŸ“‘ FastAPI endpoints for real-time fraud scoring
  • πŸ“Š monitoring, audit logging, and MLflow tracking for operational credibility

The goal is to make this repo feel like a serious portfolio-grade fintech ML system rather than a simple beginner project.


🧩 Project Description

Fraud detection in fintech is not solved with one model alone.

Real systems usually combine:

  • historical behavior analysis
  • merchant and device intelligence
  • hand-crafted fraud rules
  • probabilistic models
  • anomaly scoring
  • explainable decisioning
  • monitoring and audit trails

FinShield follows that same pattern.

It ingests transaction history, builds fraud features, trains supervised and unsupervised models, evaluates rules, combines all signals into a final score, and exposes the results through a real-time API.


🎯 What This Project Solves

FinShield helps answer questions like:

  • Is this transaction suspicious based on past behavior?
  • Does it break known fraud rules?
  • Does it look abnormal compared to normal activity?
  • Should it be approved, reviewed, or blocked?
  • Why did the system make that decision?

πŸ—οΈ Architecture

Raw Transaction Data
        β”‚
        β–Ό
Data Validation + Cleaning
        β”‚
        β–Ό
Batch Feature Engineering
        β”‚
        β”œβ”€β”€ Customer Behavior Features
        β”œβ”€β”€ Velocity Features
        β”œβ”€β”€ Merchant Risk Features
        β”œβ”€β”€ Time Features
        └── Geo / Device Features
        β”‚
        β–Ό
Training Pipelines
        β”‚
        β”œβ”€β”€ Logistic Regression Baseline
        β”œβ”€β”€ XGBoost Supervised Model
        └── Isolation Forest Anomaly Model
        β”‚
        β–Ό
Saved Model Artifacts
        β”‚
        β–Ό
Realtime Feature Builder
        β”‚
        β–Ό
Fraud Intelligence Layers
        β”‚
        β”œβ”€β”€ Rules Engine
        β”œβ”€β”€ Supervised Model Inference
        └── Anomaly Detection Inference
        β”‚
        β–Ό
Hybrid Fraud Scoring Engine
        β”‚
        β–Ό
Decision Output
        β”œβ”€β”€ Approve βœ…
        β”œβ”€β”€ Review ⚠️
        └── Block β›”
        β”‚
        β–Ό
Operational Layer
        β”œβ”€β”€ FastAPI Endpoints
        β”œβ”€β”€ Audit Logs
        β”œβ”€β”€ Monitoring Reports
        └── MLflow Experiment Tracking

🧠 Core Fraud Detection Concepts Used

πŸ“ˆ 1. Feature Engineering

Fraud models are only as good as the features they consume.

This project engineers rich behavioral and contextual features such as:

  • ⏰ Transaction time features (hour, weekend, night flags)
  • ⚑ Customer transaction velocity (1d, 7d, 30d)
  • πŸ’° Rolling spend behavior
  • πŸ“Š Amount deviation from customer history
  • πŸͺ Merchant historical fraud rate
  • πŸ“± New device indicator
  • 🌍 Foreign transaction flag

πŸ“ 2. Rules Engine

Rules provide fast, interpretable fraud logic based on domain knowledge.

Example rules include:

  • πŸ’Έ High transaction amount
  • πŸ“± New device + large spend
  • πŸ” Rapid repeat activity
  • πŸͺ Merchant fraud hotspot
  • 🌍 Foreign high-value transaction

πŸ“Œ Rules are stored in YAML configuration and can be modified without changing code.


🧠 3. Supervised Learning

The supervised model learns from labeled fraud data and predicts fraud probability.

Implemented models:

  • Logistic Regression (baseline)
  • XGBoost (primary classifier)

πŸ•΅οΈ 4. Anomaly Detection

The anomaly model detects unusual or unseen patterns without relying on labels.

Implemented model:

  • Isolation Forest

βš–οΈ 5. Hybrid Risk Scoring

The final fraud score combines multiple intelligence layers:

  • πŸ“ Rule-based score
  • 🧠 Supervised model probability
  • πŸ•΅οΈ Anomaly score

This reflects how real-world fraud systems blend rules and machine learning.


πŸ“‘ 6. Real-Time Scoring

Transactions are scored in real time using FastAPI endpoints.

  • ⚑ Low-latency inference
  • πŸ”„ On-the-fly feature generation
  • 🌐 API-based decisioning

πŸ“Š 7. Monitoring and Auditability

The platform includes production-style observability features:

  • πŸ“ˆ MLflow experiment tracking
  • 🧾 Prediction audit logs
  • πŸ“‰ Drift report generation
  • πŸ“¦ Model metadata storage

πŸ”₯ Key Features

  • βœ… Production-style Python project structure
  • βœ… Configurable fraud rules engine (YAML-driven)
  • βœ… Supervised fraud model training (Logistic + XGBoost)
  • βœ… Anomaly detection pipeline (Isolation Forest)
  • βœ… Hybrid risk scoring engine
  • βœ… FastAPI real-time serving layer
  • βœ… Model metadata endpoint
  • βœ… Monitoring and drift reporting
  • βœ… Audit logging for predictions
  • βœ… MLflow experiment tracking
  • βœ… Unit, integration, and API tests
  • βœ… Docker + GitHub Actions CI

πŸ—‚οΈ Project Structure

finshield-fraud-detection-platform/
β”œβ”€β”€ apps/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── main.py
β”‚   β”œβ”€β”€ monitoring/
β”‚   β”‚   └── monitor.py
β”‚   └── training/
β”‚       └── train.py
β”‚
β”œβ”€β”€ configs/
β”‚   β”œβ”€β”€ base.yaml
β”‚   β”œβ”€β”€ features.yaml
β”‚   β”œβ”€β”€ model.yaml
β”‚   └── rules.yaml
β”‚
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/
β”‚   β”œβ”€β”€ interim/
β”‚   β”œβ”€β”€ processed/
β”‚   └── samples/
β”‚
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ api.md
β”‚   β”œβ”€β”€ architecture.md
β”‚   β”œβ”€β”€ model-card.md
β”‚   └── runbook.md
β”‚
β”œβ”€β”€ features/
β”‚   β”œβ”€β”€ batch_features.py
β”‚   └── realtime_features.py
β”‚
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ anomaly/
β”‚   β”œβ”€β”€ supervised/
β”‚   └── artifacts/
β”‚
β”œβ”€β”€ pipelines/
β”‚   β”œβ”€β”€ data_validation.py
β”‚   β”œβ”€β”€ training_pipeline.py
β”‚   β”œβ”€β”€ scoring_pipeline.py
β”‚   └── evaluation_pipeline.py
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ common/
β”‚   β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ monitoring/
β”‚   β”œβ”€β”€ rules/
β”‚   └── scoring/
β”‚
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ api/
β”‚   β”œβ”€β”€ integration/
β”‚   └── unit/
β”‚
β”œβ”€β”€ logs/
β”œβ”€β”€ reports/
β”œβ”€β”€ mlruns/
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Makefile
β”œβ”€β”€ pyproject.toml
└── README.md


🎯 What This System Does

high_amount foreign_high_amount rapid_repeat_activity merchant_fraud_hotspot new_device_high_amount


  • detect fraud using learned patterns 🧠
  • detect suspicious unknown behavior πŸ•΅οΈ
  • apply business fraud rules πŸ“
  • combine everything into a final decision βš–οΈ
  • explain why the decision was made πŸ”

πŸ§ͺ Live Demo

πŸ”Ή Swagger API

πŸ‘‰ http://127.0.0.1:8000/docs


πŸ”Ή Hybrid Fraud Scoring Example

πŸ“₯ Input:

{
  "amount": 12000,
  "country": "ZA",
  "device_type": "desktop"
}

πŸ“€ Output:

{
  "final_score": 84.55,
  "decision": "block",
  "top_reasons": [
    "high_amount",
    "merchant_fraud_hotspot",
    "high_model_probability"
  ]
}

🧠 How It Works (Simple Flow)

Transaction β†’ Features β†’ Rules + ML + Anomaly β†’ Hybrid Score β†’ Decision

πŸ—οΈ Architecture (Real Industry Pattern)

Raw Data
   ↓
Feature Engineering
   ↓
β”œβ”€β”€ Rules Engine
β”œβ”€β”€ ML Model (XGBoost)
└── Anomaly Model (Isolation Forest)
   ↓
Hybrid Scoring Engine
   ↓
Decision (Approve / Review / Block)
   ↓
API + Monitoring + Logs

🧠 Core Concepts Demonstrated

πŸ“ˆ Feature Engineering

  • Behavioral features (spend patterns)
  • Velocity features (transaction frequency)
  • Risk features (merchant fraud rate)
  • Context features (time, geo, device)

πŸ“ Rules Engine

  • YAML-configurable
  • Explainable decisions
  • Instant fraud detection layer

🧠 Machine Learning

  • Logistic Regression (baseline)
  • XGBoost (primary)

πŸ•΅οΈ Anomaly Detection

  • Isolation Forest
  • Detects unknown fraud patterns

βš–οΈ Hybrid Scoring (REAL SYSTEM DESIGN)

final_score =
0.30 * rules
+ 0.45 * ML probability
+ 0.25 * anomaly score

πŸ“‘ Real-Time API

  • FastAPI-based scoring
  • Production-style endpoints

πŸ“Š MLOps & Monitoring

  • MLflow experiment tracking
  • Audit logs (every prediction tracked)
  • Drift reports
  • Model metadata endpoints

πŸ”₯ Key Features

  • Hybrid fraud detection system
  • Explainable decisions
  • Real-time scoring API
  • ML + anomaly detection
  • Monitoring + audit logging
  • CI/CD pipeline
  • Fully tested system

🚦 Decision System

Score Decision
0–39 βœ… Approve
40–69 ⚠️ Review
70–100 β›” Block

πŸš€ Quick Start (1-Minute Setup)

pip install -e .[dev]
pytest
python pipelines/training_pipeline.py
python -m uvicorn apps.api.main:app --reload

πŸ‘‰ Open:

http://127.0.0.1:8000/docs

πŸ“Š Outputs

Type Location
MLflow runs mlruns/
Audit logs logs/
Drift reports reports/
Models models/artifacts/

πŸ§ͺ Testing

pytest

βœ” Unit

βœ” Integration

βœ” API


πŸ§‘β€πŸ’» Skills Demonstrated

This project shows ability in:

  • Machine Learning Engineering
  • Backend Development (FastAPI)
  • Data Engineering (pipelines)
  • MLOps (MLflow, monitoring)
  • System Design
  • Fraud Detection Domain

πŸ›£οΈ Next Improvements (Production Path)

  • Feature store (Feast)
  • SHAP explainability
  • Streaming fraud detection (Kafka)
  • Dashboard (React)
  • Cloud deployment (AWS/GCP)

πŸ‘¨β€πŸ’» Author

Thabang Rakeng

Fullstack (MERN, dotnet) | Ai/Ml developers


⭐ Recruiter Note

This project is intentionally built to demonstrate:

βœ” real-world ML system thinking

βœ” production pipeline design

βœ” hybrid decision systems

βœ” clean architecture


⭐ Final Note

FinShield is built to showcase what a modern fraud detection platform can look like when engineering, ML, and MLOps are combined thoughtfully.

If you found this useful, give the repo a ⭐

About

πŸš€ A production-style fraud detection platform for fintech use cases, built with Python, FastAPI, machine learning, rules engines, anomaly detection, and MLOps

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages