A full-stack machine learning application for detecting fraudulent credit card transactions using XGBoost. Features a FastAPI backend, interactive web interface, and Docker deployment.
- Overview
- Features
- Tech Stack
- Project Structure
- Installation
- Usage
- API Documentation
- Docker Deployment
- Model Details
- Contributing
- License
This fraud detection system uses machine learning to identify potentially fraudulent credit card transactions in real-time. The system analyzes transaction patterns including amount, location, cardholder demographics, and temporal features to predict fraud with high accuracy.
- Real-time predictions via REST API
- Interactive web interface for easy testing
- XGBoost classifier trained on historical transaction data
- Feature engineering including time-based and behavioral features
- Containerized deployment with Docker
- Transparent predictions with confidence scores and feature visibility
- ๐ Fast API Backend - High-performance REST API built with FastAPI
- ๐จ Modern Web UI - Clean, responsive interface for transaction testing
- ๐ค ML-Powered Detection - XGBoost model for accurate fraud prediction
- ๐ Feature Transparency - View generated features used in predictions
- ๐ณ Docker Ready - Easy deployment with Docker and Docker Compose
- ๐ Confidence Scoring - Get probability scores for each prediction
- โก Real-time Processing - Instant fraud detection results
- FastAPI - Modern Python web framework
- XGBoost - Gradient boosting machine learning library
- Pandas - Data manipulation and analysis
- Scikit-learn - Machine learning preprocessing
- Joblib - Model serialization
- HTML5/CSS3 - Responsive web interface
- JavaScript (Vanilla) - Interactive client-side functionality
- Docker - Containerization
- Docker Compose - Multi-container orchestration
- Uvicorn - ASGI server
fraud-detection/
โโโ app.py # FastAPI application
โโโ index.html # Web interface
โโโ Dockerfile # Docker configuration
โโโ docker-compose.yml # Docker Compose configuration
โโโ requirements.txt # Python dependencies
โโโ .dockerignore # Docker ignore file
โโโ models/ # Trained ML models and preprocessors
โ โโโ xgbmodel.jb
โ โโโ preprocessor_pipeline.jb
โ โโโ target_encoder.jb
โโโ data/ # test Dataset
โ โโโ fraudTest.csv
โโโ README.md # Information about the project
- Python 3.9+
- Docker (optional, for containerized deployment)
- pip (Python package manager)
- Clone the repository
git clone https://github.com/aadedolapo/Fraud-detection.git
cd Fraud-detection
- Create a virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies
pip install -r requirements.txt
- Run the application
uvicorn app:app --reload
- Open the web interface
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Open
index.htmlin your browser for the UI
- Open
index.htmlin your web browser - Fill in the transaction details:
- Category: Transaction category (e.g., gas_transport, grocery_pos)
- Amount: Transaction amount in USD
- Gender: Cardholder gender (M/F)
- Job: Cardholder occupation
- Distance: Distance from home (km)
- Cardholder Age: Age of the cardholder
- Day of Week: Transaction day
- Click "Analyze Transaction"
- View the prediction result (FRAUD/LEGIT) with confidence score
- Click "Show Generated Features" to see additional features used in prediction
POST /predict
Request body:
{
"category": "gas_transport",
"amt": 50.0,
"gender": "M",
"job": "Engineer",
"distance": 5.0,
"cardholder_age": 30,
"dayofweek": "Monday"
}
Response:
{
"prediction": 0,
"label": "LEGIT",
"probability": 0.9234,
"generated_features": {
"city_pop": 1250,
"trans_hour": 14,
"night_trans": 0,
"trans_30min": 3,
"amt_30min": 4500,
"time_since_last_trans": 450,
"amt_vs_avg_user_amt": 12.5
}
}
curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{
"category": "gas_transport",
"amt": 50.0,
"gender": "M",
"job": "Engineer",
"distance": 5.0,
"cardholder_age": 30,
"dayofweek": "Monday"
}'
FastAPI provides interactive API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
| Endpoint | Method | Description |
|---|---|---|
/ |
GET | Health check |
/jobs |
GET | Get available job titles |
/predict |
POST | Predict transaction fraud status |
# Build and start the container
docker-compose up --build
# Run in detached mode
docker-compose up -d
# View logs
docker-compose logs -f
# Stop the container
docker-compose down
# Build the image
docker build -t fraud-detection-api .
# Run the container
docker run -p 8000:8000 \
-v $(pwd)/models:/app/models \
-v $(pwd)/data:/app/data \
--name fraud-api \
fraud-detection-api
# Stop the container
docker stop fraud-api
docker rm fraud-api
The application is configured with:
- Resource Limits: 2 CPU cores, 2GB RAM (adjustable in docker-compose.yml)
- Health Checks: Automatic monitoring of container health
- Volume Mounts: Models and data directories mounted for easy updates
- Restart Policy: Automatically restarts on failure
XGBoost (eXtreme Gradient Boosting)
- Ensemble learning method using gradient boosting
- Highly effective for imbalanced classification problems
- Fast training and prediction speeds
- Transaction category
- Transaction amount
- Cardholder gender
- Cardholder job
- Distance from home
- Cardholder age
- Day of week
- City population
- Transaction hour
- Night transaction flag (0-6 AM)
- Transactions in last 30 minutes
- Amount spent in last 30 minutes
- Time since last transaction
- Amount vs average user amount ratio
- Training Data: Historical credit card transactions
- Preprocessing: StandardScaler, Target Encoding
- Class Imbalance Handling: Built into XGBoost
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Follow PEP 8 style guide for Python code
- Add tests for new features
- Update documentation as needed
- Ensure Docker builds successfully
This project is licensed under the MIT License - see the LICENSE file for details.
Adedolapo
- GitHub: @aadedolapo
- Dataset: Credit card fraud detection dataset.
- XGBoost: The gradient boosting library
- FastAPI: Modern Python web framework
- Community contributors
For support, please open an issue in the GitHub repository or contact the maintainer.
Made with โค๏ธ by Adedolapo