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
- ๐ป GitHub: https://github.com/mahira-code/fraudshield-ml
- ๐ Live Demo: https://fraudshield-ml-mahira.streamlit.app/
๐ง 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.
- ๐ Portfolio: https://mahirabanu.website
- ๐ป GitHub: https://github.com/mahira-code
- ๐ LinkedIn: https://www.linkedin.com/in/mahira-banu
๐ฌ 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)