anomaly-detection-fraud-prevention

Anomaly detection for fraud prevention using Isolation Forest and LOF with FastAPI serving

0
0
0
Python
public

03 – Anomaly Detection (Fraud Prevention)

Synthetic fraud/anomaly detection pipeline that trains Isolation Forest + LOF on 50k hourly transactions, tracks runs in MLflow, exposes Streamlit + FastAPI for dashboards/RPA, and outputs alerts.

Setup

Set directory:

cd /Users/andresverafigueroa/Documents/GitHub/Works/03-anomaly-detection-fraud-prevention

  1. Create venv + install deps
    python3 -m venv .venv && source .venv/bin/activate
    pip install -r requirements.txt
    
  2. Point MLflow locally
    export MLFLOW_TRACKING_URI="$(pwd)/mlruns"
    

Quickstart

  1. Train model + log artifacts
    python3 scripts/train.py --config configs/config.yaml
    
    Outputs: model joblib, metrics JSON, scores CSV, MLflow run.
  2. Serve FastAPI for external callers
    ./scripts/run_api.sh
    
    Endpoints: /health, /score (single transaction), /batch_score, /metrics.
  3. Start Streamlit dashboard
    streamlit run src/app.py -- --config configs/config.yaml
    
  4. Use Power BI / other dashboards
    • Point to http://localhost:8000/score or /batch_score with JSON payload for live anomaly data.

Repo Layout

  • configs/ – config (paths, model params, threshold, alerts)
  • data/03-anomaly/ – raw + processed CSVs
  • models/03-anomaly/ – serialized pipeline
  • artifacts/ – metrics, scores, alerts
  • src/ – data gen, features, model training, API, dashboard
  • scripts/ – train, run API, serve Streamlit, run alerts
  • tests/ – unit + integration tests
  • docs/ – reference notes

Key Commands

  • Train: python3 scripts/train.py --config configs/config.yaml
  • API: ./scripts/run_api.sh
  • Dashboard: streamlit run src/app.py -- --config configs/config.yaml
  • Tests: pytest -q

Notes

  • Alert rules write alerts.jsonl with flagged transactions; inspect it to feed RPA.
  • FastAPI payload must match configs schema (see src/api/app.py).
v0.3.3[beta]