Fraud Detection in Finance: How Machine Learning Models Can Help

Introduction
Fraud is a major problem in the financial industry, and detecting fraudulent activities is essential to prevent them. With the advent of machine learning, it has become easier to detect fraudulent activities with greater accuracy and speed. In this tutorial, we will learn how to build a fraud detection system using machine learning models in Python.
Data Description
We will be using a credit card transaction dataset from Kaggle, which contains a mix of fraudulent and non-fraudulent transactions. The dataset consists of 30 features, including the amount of the transaction, the time of the transaction. Features V1, V2, … V28 are the principal components obtained with PCA, since the original features cannot be provided due to confidentiality issues. The target variable is a binary variable indicating whether a transaction is fraudulent or not.
Data Preparation
Before building the model, we need to prepare the data. We will start by importing the necessary libraries and reading the data from a CSV file.
Descriptive Analysis
Let's analyze the distribution of the outcome variable. Based on this we can observe that the most of the transactions (99.83%) are non fraudulent transitions. Therefore this is an unbalanced classification problem. It is not good to use metrics like accuracy, when the outcome variable is highly unbalanced. Therefore, we will use AUC value to compare the models.
Next, we are going to check whether there is a correlation between the predictors. The correlation heatmap below shows that the correlation between the predictors are not very high. This implies that there are no correlated predictors (i.e. there are no multicollinearity between predictors) .

Model Building
We will use three machine learning models for fraud detection: Logistic Regression, Random Forest and Gradient boosting. We will start with Logistic Regression.
Logistic Regression
We will fit Logistic Regression models with regularization. The regularization coefficient and other hyperparameters were tuned using RandomizedSearchCV approach. We use AUC values to identify the optimal parameters, as mentioned before.
Next, we identified the optimal parameters and evaluated the model based on those parameters, using test data. The AUC value based on test data is 0.86
Random Forest
Next, we will build a random forest model. We explored following settings for the hyperparameters using RandomizedSearchCV function.
Based on model training, we identified the optimal values to the hyperparameters.
we fit another model based on optimal values to the hyperparameters. As before. this model was evaluated based on the test data, by calculating the AUC value. The AUC value was around 0.87.
So the AUC value has increased from 0.86 to 0.87 by shifting from logistic regression model to random forest model.
Then we obtained the variable importance plot based on the final model of the random forest approach. The output of the variable importance shows below.

Based on the above plot we can see that not all predictors are important when predicting the outcome. The most important predictors when deciding a transaction is fraudulent or not, as follows: V3, V4, V7, V9, V10, V11, V12, V14, V16, V17, V18 and V26.
Next we will fit a gradient boosting model by only using those predictors.
Gradient boosting
First, we are going to split the data into training and testing based on new set of predictor variables.
Then we will fit the gradient boosting model by exploring following settings for the hyperparameters using RandomizedSearchCV function.
Then we identified the optimal values to the hyperparameters, as before. The final model was fitted based on these optimal hyperparameters.
The AUC value of the final model is around 0.89. This AUC value is larger than what we observed previously. At the same time, we used only 12 predictors (out of 30) to build this model. Therefore we will use this model as the final model.
Conclusion
In this tutorial, we learned how to build a fraud detection system using machine learning models in Python. We compared different models machine learning models and finally identified the best model.



Comments