Enhancing Financial Security: A Machine Learning Approach to Credit Card Fraud Detection

Oluwakemi Helen Deniran
16 min readDec 14, 2023

--

Representative Image: Credit to the Creators of this Image.

In today’s ever-changing financial transaction landscape, protecting the integrity of credit card usage has become critical. Financial security is a non-negotiable part of our linked society, from empowering startups to fortifying established organizations. Machine learning appears as a formidable answer in the age of digital banking, particularly in the identification of credit card fraud.

This project delves into the complexities of using advanced machine learning models to detect and prevent fraudulent credit card transactions. We hope to demonstrate the critical role that machine learning plays in reinforcing financial ecosystems and providing a secure, trustworthy environment for every transaction by unraveling potential risks and vulnerabilities. Let’s look at how these models protect financial integrity by offering insights that go beyond traditional security measures.

INTRODUCTION

Credit card fraud is a widespread problem for financial institutions, organizations, and customers worldwide. The “Approach to Financial Security” initiative takes center stage in the never-ending pursuit of improved security, using the power of machine learning to revolutionize credit card fraud detection.

Dataset Overview:

Our journey begins with a rich dataset extracted from credit card transactions, a critical battleground where companies must distinguish between legitimate and fraudulent activities. This dataset, sourced from European credit card transactions in September 2013, provides a snapshot of the complex landscape where 492 frauds intertwine with 284,807 transactions. The challenge at hand is formidable; frauds constitute a mere 0.172%, emphasizing the highly unbalanced nature of the data.

Understanding the data:

Principal Component Analysis (PCA) numerical features show patterns within the transactions, with ‘Time’ and ‘Amount’ standing out as non-transformed variables. The ‘time’ feature represents the number of seconds since the first transaction, while the ‘amount’ feature represents the transaction fee. Our response variable is ‘Class,’ with a value of 1 for fraud and 0 otherwise.

Championing Imbalance:

Class imbalance adds a degree of complication, prompting a recommendation to quantify accuracy using the Area Under the Precision-Recall Curve (AUPRC). In the face of such an imbalance, traditional accuracy measurements weaken, necessitating a more sophisticated approach.

The Mission—FriendPay Empowerment:

As we put ourselves in the shoes of FriendPay, a PayPal competitor dealing with fraudulent transaction identification, our goal is clear: to strengthen their credit card fraud detection system. The stakes are high, as missed transaction fees might result in significant economic losses.

Criteria for Success:

Our success is dependent on delivering tangible results at future meetings, encapsulating the progress of our fraud detection algorithm through data graphics, and completing reports. The true litmus test is the model’s impact on the company, as measured by its ability to distinguish genuine transactions from fraudulent ones and, ultimately, increase revenue.

What You Can Expect:

Prepare for a thorough presentation that is interwoven with data-driven insights, assumptions, and implications. The tour will reveal the inner workings of our fraud detection model, including not only theoretical prowess but also practical, deployable code that our DevOps team can easily incorporate into production.

As we embark on this transformative journey, we intend to change the paradigm of credit card security, secure transactions, and nurture a future where financial interactions are protected by the full embrace of machine learning.

ASSUMPTIONS:

Certain assumptions are critical for guiding the model-building and evaluation procedures in the context of this credit card fraud detection project. One essential assumption is that the dataset presented is indicative of the overall credit card transaction environment. This assumption states that the patterns and features found in the dataset are suggestive of probable fraudulent activity in real-world circumstances. Furthermore, it is expected that the PCA transformation features, specifically V1 to V28, will properly capture the important information for distinguishing between legal and fraudulent transactions. Furthermore, the underlying assumption is that the dataset's imbalance ratio necessitates the use of the Area Under the Precision-Recall Curve (AUPRC) as a more meaningful evaluation tool than traditional accuracy metrics. Recognizing and effectively communicating these assumptions is critical throughout the project's life cycle because it is critical for aligning expectations, promoting effective communication, and driving decision-making.

DATA COLLECTION AND CLEANING:

Data Collection:
The dataset utilized in this project consists of credit card transactions done in September 2013 by European cardholders. It includes a two-day snapshot of 284,807 transactions, with 492 incidents of fraud. The data comes from credit card firms, and its release for research purposes adds to the global drive to improve machine learning models and data analysis methodologies.

First 20 rows of the dataset

Data Cleaning:

Several preprocessing methods were used to verify the dataset’s quality. Missing values were handled, and missing numerical values were filled with their corresponding meanings for demonstration purposes. To avoid redundancy, duplicate rows were deleted. The original features ‘Time’ (seconds elapsed between transactions) and ‘Amount’ (transaction amount) were kept, as were the features resulting from the Principal Component Analysis (PCA) transformation, indicated as V1 to V28. The ‘Class’ feature tells if the transaction is fraudulent (1) or not fraudulent (0). Due to confidentiality issues, detailed details on the original features and further background information have been withheld. The imbalance ratio of the dataset highlights the importance of using the Area Under the Precision-Recall Curve (AUPRC) as a valid evaluation tool instead of standard accuracy metrics.

Statistical Information of the dataset
Data Cleaning

Histogram Subplots:

In the exploratory data analysis phase, histogram subplots were utilized to visualize the distribution of numerical features. The subplots, created using the Seaborn library, depict the frequency distribution of key numerical attributes such as ‘V1,’ ‘V2,’ ‘V3,’ ‘V4,’ ‘V5,’ ‘V6,’ ‘V7,’ ‘V8,’ ‘V9,’ and ‘V10.’ Each subplot provides insights into the spread and concentration of values within these features, aiding in the identification of potential patterns or anomalies. The collective display of these histograms contributes to a comprehensive understanding of the numerical features’ distribution, serving as a crucial step in data exploration and model preparation for credit card fraud detection.

Histogram of the dataset

The Scatter Matrix:

The scatter matrix was critical in the exploratory data analysis because it provided a visual depiction of pairwise correlations between major numerical variables in the dataset. The scatter matrix, which was created with the Seaborn library, presents a grid of scatterplots, allowing for a fast assessment of correlations and potential patterns between variables. We were able to spot any noticeable trends or clusters by inspecting the scatterplots, which aided in feature selection and understanding feature interactions. The matrix was especially useful in the area of credit card fraud detection, where discovering correlations between variables is critical for developing efficient machine learning models.

Scatter Matrix

Refined Correlation Heat Map:
The revised correlation heat map was a more concentrated representation that focused on the ‘Time,’ ‘Amount,’ and ‘Class’ variables. This subset is critical in detecting credit card fraud because it reveals potential patterns relating to transaction timing, amounts, and their association with illicit actions. The color-coded correlation coefficients demonstrated the strength and direction of the correlations. The improvement enabled a more focused examination, highlighting the impact of temporal and transactional factors on fraud occurrences. This visualization was a helpful guide for determining the value of features and making decisions in following modeling processes.

Heat Map

Notes on the Class Distribution Plot:

The class distribution plot offers a clear visual depiction of the imbalance in the dataset between fraudulent and non-fraudulent transactions. The plot demonstrates the difficulty of dealing with imbalanced classes, with the bulk of transactions identified as non-fraudulent (Class 0) and a much lower proportion designated as fraudulent (Class 1). Understanding this distribution is critical for choosing proper evaluation criteria and building models capable of capturing the minority class (frauds). The substantial difference in class frequencies highlights the importance of strategies like resampling or modifying class weights during model training to achieve robust fraud detection performance.

Fraudulent (1) non-fraudulent (0) classes

Outliers in the Box Plot:

The box plot with outliers is an excellent tool for understanding the distribution of numerical features in a dataset. Each box represents the data's interquartile range (IQR), with a line inside the box indicating the median. Individual points outside of this range are deemed outliers and are shown separately. The whiskers extend to the minimum and maximum values within a particular range. The box plot is useful in discovering probable trends or discrepancies in the distribution of attributes between fraudulent and non-fraudulent transactions in the context of this project. Outliers, illustrated by points outside the whiskers, may indicate odd or anomalous transactions that can help spot fraudulent activity. Using box plots to examine the distribution of features assists in feature selection and preprocessing processes to improve the effectiveness of fraud detection models.

Box Plots

The relationships between ‘Time’ and ‘Amount’:

The scatter plot displaying the relationship between time and amount elucidates the distribution and potential patterns between these two numerical characteristics. Each point represents a transaction in this graphic, with the x-axis representing time and the y-axis representing the transaction amount. The scatter plot aids in visually determining whether or not there is a noticeable link or trend between the time of the transaction and the related amount. Understanding this link can help you spot potential time-dependent patterns in fraudulent transactions, such as certain time periods associated with larger or lower transaction amounts. Furthermore, the scatter plot aids in the investigation of any outliers or clusters that may reveal anomalous behavior. This data is useful for feature engineering and selecting important features for constructing successful fraud detection systems.

Scatter/Pair plots

Box Plot for Numerical Characteristics:

The box plot is an effective visualization tool that summarizes the distribution of numerical features in a dataset. Each box indicates the feature’s interquartile range (IQR), with the middle line being the median. The whiskers extend to the minimum and maximum values within a given range, which is often defined as 1.5 times the IQR. Individual points are used to represent outliers beyond the whiskers. We can determine the central tendency, distribution, and potential outliers in each numerical feature using this representation. Box plots can help detect fraud by revealing any distinguishing patterns or discrepancies in the distribution of attributes between fraudulent and non-fraudulent transactions.

Box plots showing the different columns of the cleaned dataset.

MACHINE LEARNING

Using preprocessed, cleaned data:

  • After thorough data cleaning, missing value handling, and feature engineering, the dataset is now in a suitable form for machine learning.
  • The cleaned dataset contains relevant numerical features and the target variable ‘Class’ indicating whether a transaction is fraudulent (1) or not (0).

Splitting the Data for Testing:

  • The dataset is split into two subsets: training data and testing data.
  • The training set is used to train the machine learning model, allowing it to learn patterns and relationships in the data.
  • The testing set is kept separate and is not used during the training phase. It serves as a completely unseen dataset to evaluate the model’s performance.
  • A common split ratio is used, such as 80% for training and 20% for testing, to ensure a balance between model training and evaluation.

These steps set the stage for training and evaluating machine learning models for credit card fraud detection.

# Split data into features (X) and targets (y)
X = cleaned_dataset.drop('Class', axis=1)
y = cleaned_dataset['Class']

Split the data into training and testing sets separately
X_train, X_test = train_test_split(X, test_size = 0.2, random_state = 42)
y_train, y_test = train_test_split(y, test_size=0.2, random_state=42)

Verify the shapes of the split data
X_train.shape, X_test.shape, y_train.shape, y_test.shape

Using the Standard Scaler:

The features are transformed using a standard scaler to ensure that they have a mean of 0 and a standard deviation of 1.
When features have varying sizes, scaling is necessary to avoid specific features dominating the learning process.
Standard-Scaler is used in this project to reliably scale both the training and testing data.
The modified data is now ready for use in machine learning models, allowing for more stable and successful model training.

Fraud Detection Models:

Logistic Regression Model:

Logistic regression is used as the approach for developing the fraud detection model due to its appropriateness for binary classification problems. The model is trained on cleaned and scaled data, with a focus on correcting the dataset’s intrinsic imbalance through the use of balanced class weights. During the training process, these weights are calculated to give more weight to the minority class, specifically fraudulent transactions. The goal is to improve the model’s ability to detect patterns and relationships in data, with a focus on the accurate classification of fraudulent cases. The model is evaluated using key metrics such as accuracy, a confusion matrix, and a detailed classification report. Precision, recall, and F1-score all play important roles in evaluating the model’s performance, notably in assuring proper identification of true positives. This logistic regression model serves as an initial assessment of the fraud detection system’s capabilities.

 Train the logistic regression model with balanced class weights
balanced_logreg = LogisticRegression(class_weight='balanced')
balanced_logreg.fit(X_train_scaled, y_train)
The logistic regression model’s result

The logistic regression model had an accuracy of around 97.76%. It properly recognized 55,395 non-fraudulent transactions (class 0) and 80 fraudulent transactions (class 1) in the confusion matrix. However, 1,261 false-positives and 10 false-negatives occurred.

The categorization report contains more information. Class 0 (non-fraudulent transactions) has a high precision, indicating a low risk of false positives. The recall for class 1 (fraudulent transactions) is likewise strong, indicating that true positives are effectively identified. The precision for class 1 is, however, low, indicating a significant rate of false positives. The F1-score for class 1, which balances precision and recall, is lower, suggesting the difficulty of successfully recognizing fraudulent cases.

The weighted average accounts for class imbalance and provides a more accurate representation of overall model performance. The model’s weighted average precision, recall, and F1-score are all strong, highlighting its ability to handle unbalanced data.

Logistic regression’s usage of class weights was critical in obtaining balance. The model prioritized learning patterns related to fraud by allocating higher weights to the minority class (fraudulent transactions), resulting in increased recall for class 1. This balance is critical for a fraud detection system because the goal is to catch as many fraudulent cases as possible while minimizing false negatives.

Random Forest Model:

The Random Forest Classifier performed admirably, with an accuracy of 99.95%. It properly recognized 56,656 non-fraudulent transactions (class 0) and 66 fraudulent transactions (class 1) in the classification report. Class 1 has high precision, indicating a low rate of false positives. However, the recall for class 1 is slightly lower, implying that some fraudulent cases were missed.

The F1-score, which balances precision and recall, is high for both classes, highlighting the model’s overall efficacy. The weighted average measures take class imbalance into account, and both precision and recall are outstanding.

The Random Forest Classifier’s exceptional performance demonstrates its capacity to handle complex relationships in data. The model displayed near-perfect accuracy and significant skills in recognizing both fraudulent and non-fraudulent transactions. The small trade-off between precision and recall in class 1 emphasizes the significance of taking individual company goals and objectives into account when evaluating models. Overall, the Random Forest model is a solid choice for detecting fraud in this scenario.

Random Forest Model

The Support Vector Machine Model:

The Support Vector Machine (SVM) classifier was 99.84% accurate. The classification report, on the other hand, demonstrates difficulties in correctly recognizing fraudulent transactions (class 1). Class 1 has exceptionally low precision, recall, and F1-score, indicating a substantial amount of false negatives.

In practice, the SVM model has difficulty distinguishing between fraudulent and non-fraudulent transactions. The model’s low recall for class 1 shows that it missed a significant percentage of genuine fraud incidents. This could have major consequences for a fraud detection system because it signals a large number of unreported fraudulent transactions.

While SVMs can be useful in a variety of situations, their efficacy can vary based on the data’s features. The SVM model did not perform as well in this situation as other models, such as the Random Forest Classifier. Further exploration and fine-tuning of parameters may be needed to improve its performance for this specific fraud detection task.

Support Vector Model

The XGBoost Classifier Model:

The XGBoost classifier performed admirably, with an accuracy of 99.95%. The classification report shows that both classes have good precision, recall, and F1-score, showing strong performance in discriminating between fraudulent (class 1) and non-fraudulent (class 0) transactions.

The recall for class 1 is very high when compared to other models, implying that the XGBoost model accurately recognized a higher proportion of genuine fraudulent transactions. Class 1 precision is similarly high, meaning that when the model flags a transaction as fraudulent, it is most likely correct.

The XGBoost Classifier’s overall good performance makes it a promising choice for use in a credit card fraud detection system. Its capacity to strike a balance between precision and recall is critical in such applications where both minimizing false positives and false negatives are important for the system’s effectiveness and user trust.

XGBoost Model

The K-Nearest Neighbors Model:

The K-Nearest Neighbors (KNN) Classifier achieved an impressive accuracy of 99.85%. However, when examining the classification report, it’s crucial to note the performance metrics for class 1 (fraudulent transactions).

The precision for class 1 is perfect, indicating that when the model predicts a transaction as fraudulent, it is indeed fraudulent. However, the recall for class 1 is relatively low, implying that the KNN model might have missed a significant portion of actual fraudulent transactions.

This trade-off between precision and recall should be carefully considered in the context of credit card fraud detection. While achieving high precision is essential to avoid false positives, maintaining an acceptable level of recall is equally crucial to identifying as many fraudulent transactions as possible.

KNN Model

In summary, the KNN model shows excellent precision but could benefit from improvements in recall, making it a candidate for further optimization and tuning.

Implications:

Choosing the "best" model for fraud detection is influenced by a number of factors, including the detection system's specific goals and the trade-offs between various performance metrics. In the context of imbalanced datasets, such as credit card fraud detection, where the number of non-fraudulent transactions far outnumbers the number of fraudulent ones, metrics such as precision, recall, and the area under the precision-recall curve (AUPRC) are frequently more informative than accuracy.

Let us summarize the performance of the models we have tested:

  1. Logistic Regression with Balanced Class Weights:
  2. AUPRC: 0.69
  3. Random Forest Classifier:
  4. AUPRC: 0.78
  5. Support Vector Machine Classifier:
  6. AUPRC: 0.50
  7. XGBoost Classifier:
  8. AUPRC: 0.72
  9. K-Nearest Neighbors (KNN) Classifier:
  10. AUPRC: Not provided

Considering the AUPRC metric (which is particularly useful for imbalanced datasets), the Random Forest Classifier seems to perform the best among the models you’ve tested. It strikes a good balance between precision and recall, making it a solid choice for fraud detection.

However, keep in mind that the “best” model can also depend on other factors, such as interpretability, computational efficiency, and the specific requirements of the business. It might be worthwhile to further fine-tune hyperparameters, perform feature engineering, or explore ensemble methods to boost the performance of the selected model. Additionally, experimenting with advanced techniques like anomaly detection or deep learning architectures could be considered.

Transaction Data Simulator:

We created a synthetic dataset using a transaction data simulator to augment our analysis. This simulator generated both legitimate and fraudulent transactions, allowing us to explore how well our fraud detection models perform on synthetic data. This approach helps in testing the robustness of the models and ensures they generalize well beyond the original dataset.

Visualization of Synthetic Data:

To gain insights into the synthetic dataset, we visualized it through histograms. The visualizations provided a clear representation of the distribution of features in both legitimate and fraudulent transactions. Understanding the characteristics of synthetic data is crucial for assessing model behavior and generalizing it to different scenarios. Visualizations also aid in explaining findings to stakeholders and building confidence in the models’ performance.

Transaction Data Simulator Sample

CONCLUSION:

Finally, utilizing machine learning models, this project ventured into the important field of credit card fraud detection. Using a dataset derived from real credit card transactions, we conducted extensive data analysis, preprocessing, and the application of different machine learning models. The dataset’s class imbalance offered a substantial hurdle, which was overcome using approaches such as synthetic data generation. The findings of many models demonstrated their ability to detect fraudulent transactions, with each model having distinct advantages.

RECOMMENDATIONS:

Based on our findings, we suggest using an ensemble strategy that combines models such as logistic regression with balanced class weights, random forest, and XGBoost. Ensemble approaches can improve overall forecast accuracy by leveraging the capabilities of various models. Furthermore, continual monitoring and model retraining on a regular basis are required to respond to developing fraud tendencies. Furthermore, given the importance of false negatives in fraud detection, there is potential for future research into anomaly detection techniques to improve model sensitivity.

ACKNOWLEDGEMENTS:

We extend our gratitude to the contributors to the dataset used in this project: Andrea Dal Pozzolo, Olivier Caelen, Reid A. Johnson, and Gianluca Bontempi. Their pioneering work in the field of credit card fraud detection laid the foundation for this project. The cited research papers and theses have been instrumental in shaping our understanding and approach. Special thanks to the wider community working on fraud detection, as their insights and methodologies have greatly influenced the outcomes of this project.

--

--

Oluwakemi Helen Deniran

Tech Enthusiast | EmpowerHer in Tech💡 | Passionate Data Scientist 📊 | Community Impact Advocate 🌍 | Aspiring Strategic Communicator for Sustainability 🌟