Credit Card Fraud Detection Using Python

credit card fraud detection 1 638

A credit card is a payment card issued to users to enable the cardholder to pay a merchant for goods and services based on the cardholder’s promise to the card issuer to pay them for the amounts plus the other agreed charges

How does a credit card work?

Credit cards offer you a line of credit that can be used to make purchases, balance transfers and/or cash advances and requiring that you pay back the loan amount in the future. When using a credit card, you will need to make at least the minimum payment every month by the due date on the balance.

Debit Cards vs. Credit Cards

Debit cards make it more difficult to overspend since you’re limited to only the amount available in your checking account.

With a credit card, you run the risk of spending beyond your means. Just because your credit limit is $1,000 doesn’t mean you can afford that sort of spending in your monthly budget.

Plus, debit cards offer the same convenience as credit without requiring you to borrow money or pay interest or fees on your purchases. Choosing debit is great for managing your money and helping you live within your means.

On the other hand, some credit cards offer additional insurance on purchases and can make it easier to request a refund or a return. However, many companies are reducing or withdrawing these benefits.

You should carefully read the disclosure information for your credit card to understand the benefits.

Finally, credit cards can help cover you in an emergency, giving you a month to come up with the cash before the bill comes due. This safety net could be helpful if you find yourself needing to pay for something big before a check comes in but beware: depending on credit for emergency spending sets you up for expensive interest if you can’t pay in full by the due date. A better solution is to keep an emergency fund  on hand.

WHAT IS CREDIT CARD FRAUD?

Credit card fraud is an inclusive term for fraud committed using a payment card, such as a credit card or debit card. The purpose may be to obtain goods or services, or to make payment to another account which is controlled by a criminal.

CREDIT CARD FRAUD DETECTION USING PYTHON

WHAT IS PYTHON PROGRAMMING LANGUAGE

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python’s simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.

Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn’t catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python’s introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.

Steps Involved

  1. Importing the required packages into our python environment.
  2. Importing the data
  3. Processing the data to our needs and Exploratory Data Analysis
  4. Feature Selection and Data Split
  5. Building six types of classification models
  6. Evaluating the created classification models using the evaluation metrics

We are using python for this project because it is really effortless to make use of a bunch of methods, has an extensive amount of packages for machine learning, and can be learned easily. In recent days, the job market for python is seamlessly higher than any other programming language and companies like Netflix are using python for data science and many other applications. With that, let’s dive into the coding part.

Importing the Packages

For this project, our primary packages are going to be Pandas to work with data, NumPy to work with arrays, scikit-learn for data split, building and evaluating the classification models, and finally the xgboost package for the xgboost classifier model algorithm. Let’s import all of our primary packages into our python environment.

Python Implementation:https://medium.com/media/01928010e7500f2b2dc728b2e36f9347

Importing Data

About the data: The data we are going to use is the Kaggle Credit Card Fraud Detection dataset (click here for the dataset). It contains features V1 to V28 which are the principal components obtained by PCA. We are going to neglect the time feature which is of no use to build the models. The remaining features are the ‘Amount’ feature that contains the total amount of money being transacted and the ‘Class’ feature that contains whether the transaction is a fraud case or not.

Now let’s import the data using the ‘read_csv’ method and print the data to have a look at it in python.

Python Implementation:https://medium.com/media/0268a32287d8bba89c22ee1bc8201b29

Output:

1*ejLcgPsYUqkzEJOUzTyXYw
Image by Author

In the next process, we are going to do some data processing and Exploratory Data Analysis (EDA).

Data Processing and EDA

Let’s have a look at how many fraud cases and non-fraud cases are there in our dataset. Along with that, let’s also compute the percentage of fraud cases in the overall recorded transactions. Let’s do it in python!

Python Implementation:https://medium.com/media/5e0b5f006514f10432870ff9950a3d56

Output:

Image by Author

We can see that out of 284,807 samples, there are only 492 fraud cases which is only 0.17 percent of the total samples. So, we can say that the data we are dealing with is highly imbalanced data and needs to be handled carefully when modeling and evaluating.

Next, we are going to get a statistical view of both fraud and non-fraud transaction amount data using the ‘describe’ method in python.

Python Implementation:https://medium.com/media/e340cc539c8ef24d3124f809f4d91851

Output:

1*6ZGHuJ78
Image by Author

While seeing the statistics, it is seen that the values in the ‘Amount’ variable are varying enormously when compared to the rest of the variables. To reduce its wide range of values, we can normalize it using the ‘StandardScaler’ method in python.

Python Implementation:https://medium.com/media/fda265ebc3e978c2842abe03e31c6f20

Output:

1*oaSZuvuyAjTpRcH2bqKA9g
Image by Author

Feature Selection & Data Split

In this process, we are going to define the independent (X) and the dependent variables (Y). Using the defined variables, we will split the data into a training set and testing set which is further used for modeling and evaluating. We can split the data easily using the ‘train_test_split’ algorithm in python.

Python Implementation:https://medium.com/media/3b2558dc01e883f89499285a5be0d656

Output:

1*MISA732YJpGsFcGXOLh8Hg
Image by Author

Now that we have all the required components to build our classification models. So let’s proceed to it.

Modeling

In this step, we will be building six different types of classification models namely Decision Tree, K-Nearest Neighbors (KNN), Logistic Regression, Support Vector Machine (SVM), Random Forest, and XGBoost. Even though there are many more models which we can use, these are the most popular models used for solving classification problems. All these models can be built feasibly using the algorithms provided by the scikit-learn package. Only for the XGBoost model, we are going to use the xgboost package. Let’s implement these models in python and keep it in mind that the algorithms used might take time to get implemented.

Python Implementation:https://medium.com/media/203ef35a1470209ffc4b3a75bd748614

In the above code, we have built six different types of classification models starting from the Decision tree model to the XGBoost model. Now let’s breakdown the code.

Starting with the decision tree, we have used the ‘DecisionTreeClassifier’ algorithm to build the model. Inside the algorithm, we have mentioned the ‘max_depth’ to be ‘4’ which means we are allowing the tree to split four times and the ‘criterion’ to be ‘entropy’ which is most similar to the ‘max_depth’ but determines when to stop splitting the tree. Finally, we have fitted and stored the predicted values into the ‘tree_yhat’ variable.

Next is the K-Nearest Neighbors (KNN). We have built the model using the ‘KNeighborsClassifier’ algorithm and mentioned the ‘n_neighbors’ to be ‘5’. The value of the ‘n_neighbors’ is randomly selected but can be chosen optimistically through iterating a range of values, followed by fitting and storing the predicted values into the ‘knn_yhat’ variable.

There is nothing much to explain about the code for Logistic regression as we kept the model in a way more simplistic manner by using the ‘LogisticRegression’ algorithm and as usual, fitted and stored the predicted variables in the ‘lr_yhat’ variable.

We built the Support Vector Machine model using the ‘SVC’ algorithm and we didn’t mention anything inside the algorithm as we managed to use the default kernel which is the ‘rbf’ kernel. After that, we stored the predicted values into the ‘svm_yhat’ after fitting the model.

The next model is the Random forest model which we built using the ‘RandomForestClassifier’ algorithm and we mentioned the ‘max_depth’ to be 4 just like how we did to build the decision tree model. Finally, fitting and storing the values into the ‘rf_yhat’. Remember that the main difference between the decision tree and the random forest is that, decision tree uses the entire dataset to construct a single model whereas, the random forest uses randomly selected features to construct multiple models. That’s the reason why the random forest model is used versus a decision tree.

Our final model is the XGBoost model. We built the model using the ‘XGBClassifier’ algorithm provided by the xgboost package. We mentioned the ‘max_depth’ to be 4 and finally, fitted and stored the predicted values into the ‘xgb_yhat’.

With that, we have successfully built our six types of classification models and interpreted the code for easy understanding. Our next step is to evaluate each of the models and find which is the most suitable one for our case.

Evaluation

As I said before, in this process we are going to evaluate our built models using the evaluation metrics provided by the scikit-learn package. Our main objective in this process is to find the best model for our given case. The evaluation metrics we are going to use are the accuracy score metric, f1 score metric, and finally the confusion matrix.

1. Accuracy score

Accuracy score is one of the most basic evaluation metrics which is widely used to evaluate classification models. The accuracy score is calculated simply by dividing the number of correct predictions made by the model by the total number of predictions made by the model (can be multiplied by 100 to transform the result into a percentage). It can generally be expressed as:

Accuracy score = No.of correct predictions / Total no.of predictions

Let’s check the accuracy score of the six different classification models we built. To do it in python, we can use the ‘accuracy_score’ method provided by the scikit-learn package.

Python Implementation:https://medium.com/media/8bda23184617b51c3ba4fa30f74f6ce9

Output:

Image by Author

According to the accuracy score evaluation metric, the KNN model reveals to be the most accurate model and the Logistic regression model to be the least accurate model. However, when we round up the results of each model, it shows 0.99 (99% accurate) which is a very good score.

2. F1 Score

The F1 score or F-score is one of the most popular evaluation metrics used for evaluating classification models. It can be simply defined as the harmonic mean of the model’s precision and recall. It is calculated by dividing the product of the model’s precision and recall by the value obtained on adding the model’s precision and recall and finally multiplying the result with 2. It can be expressed as:

F1 score = 2( (precision * recall) / (precision + recall) )

The F1 score can be calculated easily in python using the ‘f1_score’ method provided by the scikit-learn package.

Python Implementation:https://medium.com/media/57b5c632f5c6b5cb46bd82e38da08e81

Output:

1*FBj9ble4ZcH03POQ5npgBQ
Image by Author

The ranking of the models is almost similar to the previous evaluation metric. On basis of the F1 score evaluation metric, the KNN model snatches the first place again and the Logistic regression model remains to be the least accurate model.

3. Confusion Matrix

Typically, a confusion matrix is a visualization of a classification model that shows how well the model has predicted the outcomes when compared to the original ones. Usually, the predicted outcomes are stored in a variable that is then converted into a correlation table. Using the correlation table, the confusion matrix is plotted in the form of a heatmap. Even though there are several built-in methods to visualize a confusion matrix, we are going to define and visualize it from scratch for better understanding. Let’s do it in python!

Python Implementation:https://medium.com/media/62910ed1e36087ba836a56df0fd79b2a

Output:

1*jcSUh 78QZMeb2QKF0nG6Q
1*jhnEAaSFB366nMvKg6u5jw
1*ua4pgBnDVkg5gXDjkF8UzA
1*emBuV5Ij2Szck6wB QY0zw
Images by Author

Understanding the confusion matrix: Let’s take the confusion matrix of the XGBoost model as an example. Look at the first row. The first row is for transactions whose actual fraud value in the test set is 0. As you can calculate, the fraud value of 56861 of them is 0. And out of these 56861 non-fraud transactions, the classifier correctly predicted 56854 of them as 0 and 7 of them as 1. It means, for 56854 non-fraud transactions, the actual churn value was 0in the test set, and the classifier also correctly predicted those as 0. We can say that our model has classified the non-fraud transactions pretty well.

Let’s look at the second row. It looks like there were 101 transactions whose fraud value was 1. The classifier correctly predicted 79 of them as 1, and 22 of them wrongly as 0. The wrongly predicted values can be considered as the error of the model.

Like this, while comparing the confusion matrix of all the models, it can be seen that the K-Nearest Neighbors model has performed a very good job of classifying the fraud transactions from the non-fraud transactions followed by the XGBoost model. So we can conclude that the most appropriate model which can be used for our case is the K-Nearest Neighbors model and the model which can be neglected is the Logistic regression model.

Drop your comment

0Shares

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *