DEV Community

Mahira Banu
Mahira Banu

Posted on

๐Ÿ›ก๏ธ Building FraudShield: Credit Card Fraud Detection with Imbalanced Data

Fraud detection is one of those problems that looks simple on the surface โ€” classify transactions as โ€œfraudโ€ or โ€œnot fraudโ€. But once you look at real data, it becomes a completely different challenge.

In this project, I built FraudShield, an end-to-end machine learning system to detect fraudulent credit card transactions using both supervised and unsupervised approaches, along with a live dashboard.


๐Ÿ“Š The Problem

The dataset I used contains over 284,000 transactions, but only:

๐Ÿ‘‰ 0.17% are fraud

This creates a highly imbalanced dataset, where a model can achieve 99% accuracy just by predicting everything as โ€œnot fraudโ€.

So the real question becomes:

How do we detect fraud when itโ€™s so rare?


๐Ÿ” Dataset Overview

The dataset contains real-world credit card transactions made by European cardholders, anonymised using PCA transformation to protect sensitive information. It includes 284,807 transactions, of which only 492 are fraudulent (~0.17%), making it a highly imbalanced classification problem.

๐Ÿง  What are V1โ€“V28?

These are PCA-transformed features.

In simple terms:

  • The original features are hidden
  • Data is transformed into mathematical components
  • We canโ€™t interpret them directly

๐Ÿ‘‰ This makes the problem harder โ€” models must learn patterns without human-readable features.


๐Ÿ“ˆ Exploratory Data Analysis (EDA)

Some key observations:

  • The dataset is extremely imbalanced
  • Most transactions are low value
  • Fraud doesnโ€™t follow obvious patterns
  • Features are weakly correlated due to PCA transformation

One important realization early on:

Accuracy is NOT a useful metric here


โš ๏ธ Why Accuracy is Misleading

If a model predicts:

text All transactions = Normal

It gets:

๐Ÿ‘‰ 99.8% accuracy

โ€ฆbut detects zero fraud

So instead, I focused on:

  • Precision
  • Recall
  • F1 Score

๐Ÿค– Model 1 โ€” XGBoost (Supervised Learning)

I trained an XGBoost classifier, which is well-suited for tabular data and imbalanced problems.

Key setup:

  • scale_pos_weight to handle imbalance
  • Stratified train/test split
  • Feature scaling

๐Ÿ“Š Results:

  • Precision: 0.71
  • Recall: 0.87 ๐Ÿ”ฅ
  • F1 Score: 0.78

๐Ÿง  Insight:

The model successfully detects 87% of fraud cases, which is critical in real-world systems.


๐Ÿงช Model 2 โ€” Isolation Forest (Unsupervised)

To compare approaches, I also used Isolation Forest, an anomaly detection model.

๐Ÿ“Š Results:

  • Precision: 0.29
  • Recall: 0.30
  • F1 Score: 0.30

๐Ÿง  Insight:

Unsupervised models struggle to detect subtle fraud patterns without labelled data.


โš–๏ธ Model Comparison

Model Precision Recall F1
XGBoost 0.71 0.87 0.78
Isolation Forest 0.29 0.30 0.30

๐Ÿš€ Key takeaway:

Supervised learning significantly outperforms unsupervised anomaly detection when labelled data is available.


๐Ÿ” Explainability with SHAP

To understand how the model makes decisions, I used SHAP (SHapley Additive exPlanations).

This helps answer:

  • Which features influence predictions?
  • Why was a transaction classified as fraud?

๐Ÿ‘‰ This adds transparency and trust to the system.


๐Ÿ–ฅ๏ธ Deployment โ€” Streamlit Dashboard

To make the system usable, I built a Streamlit dashboard.

Features:

  • Input transaction data
  • Predict fraud probability
  • Display risk level
  • Show model metrics

๐ŸŒ Live Demo & Code


๐Ÿง  What I Learned

This project taught me a lot about real-world machine learning:

  • Handling imbalanced datasets
  • Choosing the right evaluation metrics
  • Comparing supervised vs unsupervised models
  • Using SHAP for explainability
  • Building and deploying end-to-end ML systems

๐Ÿš€ Whatโ€™s Next

  • Hyperparameter tuning
  • Model monitoring (drift detection)
  • API deployment (FastAPI)
  • MLOps integration

๐Ÿ‘ฉโ€๐Ÿ’ป About Me

Iโ€™m Mahira Banu, a Data Scientist and AI Engineer focused on building practical, real-world AI systems.


๐Ÿ’ฌ Final Thoughts

Fraud detection isnโ€™t just about building a model โ€” itโ€™s about understanding data, handling imbalance, and making reliable decisions in high-risk scenarios.

If youโ€™re working on similar problems, Iโ€™d love to hear your thougts

Top comments (0)