A complete ML project for detecting credit card fraud using four models: Logistic Regression, Random Forest, XGBoost, LightGBM
fraud_detection/
├── app.py # Streamlit main app
├── train.py # Model training pipeline
├── requirements.txt # Python dependencies
├── data/
│ └── generate_data.py # Synthetic data generator
├── models/ # Saved models (created after training)
├── pages/
│ ├── home.py # Overview page
│ ├── train_models.py # Training page
│ ├── predict.py # Single prediction page
│ ├── batch.py # Batch prediction page
│ └── model_insights.py # Charts & model comparison
└── utils/
└── predictor.py # Prediction utilities
python -m venv venv
# Windows
venv\Scripts\activate
# Mac / Linux
source venv/bin/activatepip install -r requirements.txt
⚠️ Requires Python 3.9–3.11. Python 3.12+ may have issues with some libraries.
streamlit run app.pyThe app opens at http://localhost:8501
- Train Models page → Click "Start Training" (takes ~60–120 seconds)
- Single Prediction → Enter transaction features or generate a random sample
- Batch Prediction → Upload a CSV of transactions
- Model Insights → Compare ROC/PR curves, confusion matrices, feature importance
| Model | Key Hyperparameters |
|---|---|
| Logistic Regression | C=0.1, class_weight=balanced |
| Random Forest | 200 trees, max_depth=8, balanced |
| XGBoost | 200 trees, scale_pos_weight=50 |
| LightGBM | 200 trees, class_weight=balanced |
Imbalance handling: SMOTE oversampling on training set + class weights
Features: V1–V28 (PCA components), log(Amount), Hour of day
| Model | ROC-AUC | F1 |
|---|---|---|
| Logistic Regression | ~0.94 | ~0.65 |
| Random Forest | ~0.98 | ~0.80 |
| XGBoost | ~0.98 | ~0.82 |
| LightGBM | ~0.98 | ~0.81 |
- Install the Python extension
- Select your venv interpreter:
Ctrl+Shift+P→ "Python: Select Interpreter" - Open integrated terminal:
Ctrl+`
- The dataset is synthetic (generated automatically). To use real data, place
creditcard.csvin thedata/folder with columns V1–V28, Amount, Time, Class. - Models are saved as
.pklfiles in themodels/folder after training. - Retraining overwrites existing models.