Anshgajera/credit-card-fraud-detector
Fraud Detection System
This project builds a credit card fraud detection system using machine learning and deploys the final model with Streamlit.
The dataset is highly imbalanced, so the project focuses more on recall and ROC-AUC than plain accuracy.
Project Objective
The goal is to classify a transaction as:
0= Legitimate1= Fraudulent
This is a binary classification problem on the creditcard.csv dataset.
Dataset Summary
- Rows:
284,807 - Columns:
31 - Target column:
Class - Fraud cases:
492 - Legitimate cases:
284,315 - Fraud ratio:
0.1727%
The dataset mostly contains PCA-transformed numeric features (V1 to V28) along with Time, Amount, and Class.
Workflow Used
The notebook follows a practical fraud detection pipeline:
- Data understanding and basic EDA
- Class imbalance detection
- SMOTE on training data only
- Feature scaling for required models
- Feature selection using model importance
- Training multiple models
- Hyperparameter tuning
- Cross validation
- ANN model implementation
- Final model comparison and deployment
Models Considered
- Logistic Regression
- Decision Tree
- Random Forest
- XGBoost
- Artificial Neural Network (ANN)
The final deployed model in this project is a RandomForestClassifier.
Evaluation Focus
For fraud detection, these metrics are more important than accuracy:
- Precision
- Recall
- F1 Score
- ROC-AUC
Recall is especially important because missing a fraud case is more costly than flagging a normal case.
Project Files
creditcard.csv- datasetFraud_Detection_exam.ipynb- main notebook with preprocessing, modeling, ANN, and comparisonapp.py- Streamlit deployment appbest_model.pkl- saved final modelscaler.pkl- saved scaler for input preprocessingrequirements.txt- project dependencies
How To Run
- Install dependencies for the app
pip install -r requirements.txt- Install extra notebook libraries if needed
pip install matplotlib seaborn imbalanced-learn tensorflow kagglehub- Run the notebook
Open Fraud_Detection_exam.ipynb in Jupyter Notebook or JupyterLab and run the cells step by step.
- Run the Streamlit app
streamlit run app.pyStreamlit App Input
The app accepts:
TimeAmountV1toV28
After clicking the predict button, the app shows whether the transaction is:
- Legitimate
- Fraudulent
It also displays prediction probabilities.
Why Random Forest Was Selected
- It handles nonlinear fraud patterns well
- It works well on numeric-heavy data
- It is strong on noisy data
- It gave reliable classification performance for this dataset
- It was better suited for deployment in this project
If ANN performance is lower than classical models, selecting Random Forest is still justified because final selection should depend on recall, ROC-AUC, and generalization.
Short Viva Explanation
SMOTE balances minority fraud samples so the model can learn fraud patterns better.
Recall is important because missing fraudulent transactions is more dangerous than a few false alarms.
Random Forest was selected because it handled the imbalanced numeric dataset well and gave strong fraud detection performance.
Deployment Note
The Streamlit app uses the saved model from best_model.pkl and scales the Time and Amount inputs using scaler.pkl before prediction.
