Skip to content

Latest commit

 

History

History
318 lines (212 loc) · 6.1 KB

File metadata and controls

318 lines (212 loc) · 6.1 KB

💳 Credit Card Fraud Anomaly Detection

Unsupervised Machine Learning using Isolation Forest & Streamlit

        

Python Pandas Scikit Learn Streamlit


Live Demo GitHub


📌 Project Overview

Credit card fraud detection is one of the most challenging anomaly detection problems because fraudulent transactions account for less than 0.2% of all transactions.

This project detects suspicious transactions using Isolation Forest, an unsupervised anomaly detection algorithm that isolates abnormal transactions without relying on fraud labels during training.

Unlike traditional supervised classifiers, Isolation Forest learns the behavior of normal transactions and identifies anomalies based on how easily they are isolated.

The project includes an interactive Streamlit dashboard for:

  • Dataset exploration
  • Data preprocessing summary
  • Fraud detection visualization
  • Model performance analysis
  • Real-time transaction prediction

✨ Features

  • ✅ Real Credit Card Fraud Dataset
  • ✅ Isolation Forest Anomaly Detection
  • ✅ Hyperparameter Optimization
  • ✅ Precision-Recall Evaluation
  • ✅ Interactive Streamlit Dashboard
  • ✅ Jupyter Notebook
  • ✅ Deployment Ready
  • ✅ Clean Modular Code Structure

🛠 Tech Stack

Technology Purpose
Python Programming Language
Pandas Data Processing
Scikit-Learn Isolation Forest Model
Streamlit Interactive Dashboard

📊 Model Performance

Metric Value
Precision-Recall AUC 0.2676
F1 Score 0.3813
Best Hyperparameters n_estimators=300
Contamination 0.005
Decision Threshold 0.0312

Confusion Matrix

Predicted Normal Predicted Fraud
Actual Normal 56,737 127
Actual Fraud 45 53

📂 Dataset

Dataset: Credit Card Fraud Detection Dataset

Source: Kaggle (ULB Machine Learning Group)

https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud

Dataset Statistics

  • Total Transactions: 284,807
  • Fraudulent Transactions: 492
  • Fraud Rate: 0.172%
  • Highly Imbalanced Dataset
  • PCA-transformed Features (V1–V28)
  • Target:
    • 0 → Normal
    • 1 → Fraud

📸 Application Screenshots

🏠 Home

Home


📂 Dataset Preview

Dataset


📊 Data Preprocessing

Preprocessing


🚨 Fraud Detection Results

Fraud Detection


📈 Model Performance

Performance


💳 Predict Transaction

Prediction


📁 Project Structure

CreditCard_Fraud_Detection/

├── assets/
│   └── images/
│
├── dataset/
│   └── creditcard.csv
│
├── models/
│   ├── isolation_forest_model.pkl
│   └── evaluation_results.pkl
│
├── notebooks/
│   └── CreditCard_Fraud_Detection.ipynb
│
├── app.py
├── preprocessing.py
├── fraud_detection.py
├── train_model.py
├── utils.py
├── requirements.txt
├── runtime.txt
├── .python-version
├── .gitignore
└── README.md

⚙️ Installation

Clone the repository

git clone YOUR_GITHUB_REPO_URL
cd CreditCard_Fraud_Detection

Create a virtual environment

python -m venv .venv

Activate the virtual environment

Windows

.venv\Scripts\activate

Linux / macOS

source .venv/bin/activate

Install dependencies

pip install -r requirements.txt

▶️ Running the Project

Train the Model

python train_model.py

This will:

  • Load the dataset
  • Preprocess the data
  • Train Isolation Forest
  • Save trained model
  • Save evaluation metrics

Launch Dashboard

streamlit run app.py

Open

http://localhost:8501

Notebook

jupyter notebook notebooks/CreditCard_Fraud_Detection.ipynb

📊 Dashboard Pages

  • 🏠 Home
  • 📂 Dataset Preview
  • ⚙️ Data Preprocessing Summary
  • 🚨 Fraud Detection Results
  • 📈 Model Performance
  • 💳 Predict Transaction

☁️ Deployment

This project is fully deployable on Streamlit Community Cloud.

Steps

  1. Push project to GitHub.
  2. Login to Streamlit Community Cloud.
  3. Create a new app.
  4. Select the repository.
  5. Set:
Main file

app.py
  1. Deploy.

The application loads the saved model directly and never retrains during startup.


🚀 Future Improvements

  • Improve anomaly threshold calibration
  • Add batch CSV prediction
  • Add downloadable prediction reports
  • Expand hyperparameter search
  • Add model monitoring
  • Add transaction history visualization

📜 License

This project is developed for educational and portfolio purposes.


⭐ If you found this project useful, consider giving it a star.

Built with ❤️ using Python, Scikit-Learn, and Streamlit.