Developing a credit card fraud detection system using Anomaly Detection and Supervised Machine Learning techniques.
Credit card fraud detection is one of the most important applications of Machine Learning in the financial industry. Fraudulent transactions are rare, but they can generate significant financial losses and operational risks for banks, fintechs, and payment companies.
This project develops a Credit Card Fraud Detection System using multiple anomaly detection techniques and a supervised learning benchmark model. The project evaluates Isolation Forest, Adjusted Isolation Forest, Local Outlier Factor (LOF), One-Class SVM, and Random Forest to identify fraudulent transactions within a highly imbalanced dataset.
The objective is to compare unsupervised anomaly detection approaches against supervised machine learning and understand their effectiveness in identifying suspicious transactions while minimizing false positives.
Financial institutions process millions of transactions every day, making manual fraud detection impractical and inefficient.
Fraudulent transactions represent only a very small fraction of all transactions, creating a highly imbalanced classification problem. Missing fraudulent transactions can result in financial losses, while excessive false positives may negatively impact customer experience and increase operational costs.
The objective of this project is to build Machine Learning models capable of identifying fraudulent transactions and supporting fraud prevention strategies.
Reliable fraud detection systems can support:
- Financial Loss Reduction
- Fraud Monitoring
- Risk Management
- Customer Protection
- Transaction Screening
- Data-Driven Decision Making
The dataset contains credit card transactions made by European cardholders.
The dataset contains:
- Transaction Features
- PCA-Transformed Variables
- Transaction Amount
- Transaction Time
- Fraud Labels
- 284,807 transactions
- 492 fraudulent transactions
- 284,315 legitimate transactions
- Fraud ratio of approximately 0.17%
Dataset Source:
https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud
To run this project, download the dataset from Kaggle and place the CSV file inside the data/ folder using the following structure:
data/
└── creditcard.csv
- Python
- Pandas
- NumPy
- Matplotlib
- Seaborn
- Scikit-Learn
- Isolation Forest
- Local Outlier Factor (LOF)
- One-Class SVM
- Random Forest Classifier
- Jupyter Notebook
- Data Loading
- Initial Data Exploration
- Missing Values and Statistical Summary
- Class Distribution Analysis
- Transaction Amount Analysis
- Correlation Analysis
- Data Cleaning and Preprocessing
- Feature Scaling
- Isolation Forest Modeling
- Adjusted Isolation Forest Modeling
- Local Outlier Factor Modeling
- One-Class SVM Modeling
- Random Forest Benchmark
- Cross Validation
- ROC Curve Analysis
- Precision-Recall Curve Analysis
- Feature Importance Analysis
- Model Performance Comparison
- Business Insights and Conclusions
Before model development, the dataset was inspected for missing values and descriptive statistics.
No missing values were identified, which simplified the preprocessing stage and allowed the analysis to focus on feature scaling, exploratory analysis, and model development.
The statistical summary also confirmed the highly imbalanced nature of the dataset, with fraudulent transactions representing only a very small fraction of the total observations.
The class distribution reveals one of the main challenges in fraud detection: extreme class imbalance.
Fraudulent transactions account for approximately 0.17% of all observations, while legitimate transactions represent more than 99% of the dataset.
This imbalance demonstrates why traditional metrics such as Accuracy can be misleading. A model could classify almost all transactions as legitimate and still achieve high Accuracy while failing to detect fraud.
For this reason, this project focuses on Precision, Recall, and F1-Score as the primary evaluation metrics.
The transaction amount distribution presents a highly right-skewed pattern.
Most transactions involve relatively small amounts, while a limited number of transactions reach significantly higher values. This behavior is common in financial transaction datasets and highlights the presence of extreme outliers.
Understanding the amount distribution is important because unusually large transactions may attract additional attention during fraud monitoring processes. However, fraud detection cannot rely solely on transaction value, since fraudulent transactions may occur across a wide range of amounts.
Comparing transaction amounts between legitimate and fraudulent transactions reveals that fraud does not occur exclusively among high-value transactions.
Although legitimate transactions exhibit a broader range of values and contain the largest outliers, fraudulent transactions are distributed across multiple transaction amounts as well.
This result suggests that transaction amount alone is insufficient for fraud detection and reinforces the need for Machine Learning models capable of identifying complex behavioral patterns across multiple features.
Correlation analysis was performed to identify variables most strongly associated with fraudulent transactions.
Several variables, including V17, V14, V12, V10, V11, and V4, showed stronger relationships with the target variable than the remaining features.
Although the original meaning of these variables is unknown due to PCA transformation, the results suggest that they contain important fraud-related information and contribute significantly to model performance.
To evaluate model stability, a 5-fold Cross Validation procedure was applied to the Random Forest benchmark model.
| Metric | Value |
|---|---|
| Mean F1-Score | 0.84 |
| Standard Deviation | 0.04 |
The low standard deviation indicates consistent performance across multiple validation folds.
This result suggests that the model generalizes well and is not overly dependent on a specific train-test split.
The ROC Curve evaluates the model's ability to distinguish between legitimate and fraudulent transactions across different classification thresholds.
The Random Forest model achieved a ROC-AUC score of approximately 0.93, indicating strong discriminative power and significantly outperforming random classification.
The curve remains close to the upper-left corner, demonstrating a high True Positive Rate while maintaining a low False Positive Rate.
Because fraud cases are extremely rare, Precision-Recall analysis provides a more realistic evaluation than ROC analysis alone.
The curve shows that the Random Forest model maintains high Precision across a broad range of Recall values, indicating that the majority of transactions flagged as fraud are indeed fraudulent.
This behavior is particularly important in real-world fraud detection systems, where excessive false positives can create operational challenges and increase investigation costs.
Feature importance analysis revealed the variables that contributed most to fraud detection performance.
- V14
- V4
- V10
- V12
- V17
Several of these variables also appeared among the strongest correlations observed during exploratory analysis.
Interestingly, transaction amount contributed less than many PCA-transformed variables, suggesting that fraud detection depends more heavily on transaction behavior patterns than on transaction value alone.
Multiple anomaly detection techniques were evaluated and compared against a supervised Random Forest benchmark.
| Model | Precision | Recall | F1-Score |
|---|---|---|---|
| Isolation Forest | 0.25 | 0.25 | 0.25 |
| Isolation Forest Adjusted | 0.14 | 0.41 | 0.21 |
| Local Outlier Factor | 0.12 | 0.12 | 0.12 |
| One-Class SVM | 0.01 | 0.02 | 0.01 |
| Random Forest | 0.97 | 0.71 | 0.82 |
The Random Forest model achieved the strongest performance across all evaluation metrics.
Among the anomaly detection approaches, the Adjusted Isolation Forest produced the highest Recall, demonstrating that increasing model sensitivity can improve fraud detection rates, although at the cost of more false positives.
Random Forest significantly outperformed all anomaly detection models because it was trained using historical fraud labels.
While anomaly detection algorithms identify observations that appear unusual, they do not know which anomalies actually represent fraud. As a result, they may flag unusual legitimate transactions or miss fraudulent transactions that resemble normal behavior.
Random Forest learns directly from labeled fraud examples, allowing it to capture complex fraud patterns more effectively.
The model achieved:
- Precision: 97%
- Recall: 71%
- F1-Score: 82%
- ROC-AUC: 0.93
These results demonstrate the effectiveness of supervised learning when high-quality labeled fraud data is available.
- Fraudulent transactions represent only approximately 0.17% of all observations.
- Accuracy alone is not an appropriate metric for fraud detection.
- Transaction amount alone is not sufficient for identifying fraudulent transactions.
- Fraudulent transactions occur across multiple transaction values, not only among large purchases.
- Increasing anomaly detection sensitivity improves Recall but increases false positives.
- Adjusted Isolation Forest achieved the strongest Recall among anomaly detection models.
- Random Forest significantly outperformed all unsupervised approaches.
- Transaction behavior patterns are more informative than transaction value alone.
- Supervised learning provides stronger fraud detection performance when labeled fraud data is available.
- Combining anomaly detection and supervised learning could improve real-world fraud monitoring systems.
Although the results are promising, some limitations should be considered:
- The dataset is anonymized through PCA transformation, limiting direct business interpretation of individual features.
- The data represents a specific historical period and may not capture evolving fraud patterns.
- One-Class SVM required sampling due to computational constraints.
- Hyperparameter optimization was not extensively explored.
- External information such as merchant, customer, device, and geographic data was not available.
Potential enhancements for future versions include:
- Hyperparameter optimization using GridSearchCV or Optuna.
- Testing advanced models such as XGBoost, LightGBM, and CatBoost.
- Applying resampling techniques such as SMOTE.
- Developing a fraud risk scoring system.
- Creating a real-time fraud monitoring dashboard.
- Integrating customer behavior and merchant information.
- Building an end-to-end fraud detection API.
- Exploring Explainable AI techniques for model interpretability.
This project developed a complete fraud detection pipeline using both Anomaly Detection and Supervised Machine Learning techniques.
The results demonstrated that fraud detection is a highly imbalanced problem where traditional metrics such as Accuracy can be misleading. More informative metrics such as Precision, Recall, F1-Score, ROC-AUC, and Precision-Recall analysis provide a clearer understanding of model effectiveness.
Among the anomaly detection approaches, the Adjusted Isolation Forest achieved the highest Recall, successfully identifying a larger proportion of fraudulent transactions. However, this improvement came at the cost of additional false positives.
The Random Forest classifier achieved the strongest overall performance, reaching a Precision of 97%, Recall of 71%, F1-Score of 82%, and ROC-AUC of 0.93. These results highlight the advantage of supervised learning when historical fraud labels are available.
From a business perspective, the most effective strategy would be to combine both approaches:
- Use anomaly detection to identify unusual and previously unseen fraud patterns.
- Use supervised models to detect known fraud behaviors learned from historical data.
- Prioritize high-risk transactions for manual review or automated monitoring systems.
This project demonstrates how Machine Learning can support financial institutions in reducing fraud-related losses, improving transaction monitoring, and strengthening risk management processes.
Credit-Card-Fraud-Detection/
│
├── data/
│ └── creditcard.csv
│
├── images/
│ ├── class_distribution.png
│ ├── transaction_amount_distribution.png
│ ├── transaction_amount_by_class.png
│ ├── correlation_analysis.png
│ ├── roc_curve.png
│ ├── precision_recall_curve.png
│ ├── feature_importance.png
│ └── model_comparison.png
│
├── notebooks/
│ └── credit_card_fraud_detection.ipynb
│
├── README.md
├── requirements.txt
└── .gitignoreTalita Niqian Lin Wu
LinkedIn: https://www.linkedin.com/in/talita-niqian-lin-wu/
GitHub: https://github.com/TalitaNLinWu







