R Project - Detect Credit Card Fraud with Machine Learning in R
Posted by Superadmin on August 22 2020 04:05:36

Detect Credit Card Fraud with Machine Learning in R

BY  · UPDATED · JULY 31, 2019

 

 

This is the 3rd part of the R project series designed by DataFlair. Earlier we talked about Uber Data Analysis Project and today we will discuss the Credit Card Fraud Detection Project using Machine Learning and R concepts. In this R Project, we will learn how to perform detection of credit cards. We will go through the various algorithms like Decision Trees, Logistic Regression, Artificial Neural Networks and finally, Gradient Boosting Classifier. For carrying out the credit card fraud detection, we will make use of the Card Transactions dataset that contains a mix of fraud as well as non-fraudulent transactions.

Machine Learning Project – How to Detect Credit Card Fraud

The aim of this R project is to build a classifier that can detect credit card fraudulent transactions. We will use a variety of machine learning algorithms that will be able to discern fraudulent from non-fraudulent one. By the end of this machine learning project, you will learn how to implement machine learning algorithms to perform classification.

The dataset used in this project is available here – Fraud Detection Dataset

1. Importing the Datasets

We are importing the datasets that contain transactions made by credit cards-

Code:

  1. library(ranger) 
  2.  library(caret) 
  3.  library(data.table) 
  4. creditcard_data <- read.csv("/home/dataflair/data/Credit Card/creditcard.csv")

Input Screenshot:

Importing Libraries

Before moving on, you must revise the concepts of R Dataframes

2. Data Exploration

In this section of the fraud detection ML project, we will explore the data that is contained in the creditcard_data dataframe. We will proceed by displaying the creditcard_data using the head() function as well as the tail() function. We will then proceed to explore the other components of this dataframe –

Code:

  1. dim(creditcard_data) 
  2.  head(creditcard_data,6)

Output Screenshot:

data science project in R

 

Code:

  1. tail(creditcard_data,6)

Output Screenshot:

credit card fraud detection

Code:

  1. table(creditcard_data$Class) 
  2.  summary(creditcard_data$Amount) 
  3.  names(creditcard_data) 
  4.  var(creditcard_data$Amount)

Output Screenshot:

credit card fraud detection

Code:

  1. sd(creditcard_data$Amount)

Output Screenshot:

data exploration

Learn everything about R for FREE and master the technology

3. Data Manipulation

In this section of the R data science project, we will scale our data using the scale() function. We will apply this to the amount component of our creditcard_data amount. Scaling is also known as feature standardization. With the help of scaling, the data is structured according to a specified range. Therefore, there are no extreme values in our dataset that might interfere with the functioning of our model. We will carry this out as follows:

Code:

  1. head(creditcard_data)

Output Screenshot:

credit card fraud detection

Code:

  1. creditcard_data$Amount=scale(creditcard_data$Amount) 
  2. NewData=creditcard_data[,-c(1)] 
  3.  head(NewData)

Output Screenshot:

credit card fraud detection

4. Data Modeling

After we have standardized our entire dataset, we will split our dataset into training set as well as test set with a split ratio of 0.80. This means that 80% of our data will be attributed to the train_data whereas 20% will be attributed to the test data. We will then find the dimensions using the dim() function –

Code:

  1. library(caTools)
  2. set.seed(123)
  3. data_sample = sample.split(NewData$Class,SplitRatio=0.80)
  4. train_data = subset(NewData,data_sample==TRUE)
  5. test_data = subset(NewData,data_sample==FALSE)
  6. dim(train_data)
  7. dim(test_data)

Output Screenshot:

credit card fraud detection

5. Fitting Logistic Regression Model

In this section of credit card fraud detection project, we will fit our first model. We will begin with logistic regression. A logistic regression is used for modeling the outcome probability of a class such as pass/fail, positive/negative and in our case – fraud/not fraud. We proceed to implement this model on our test data as follows –

Code:

  1. Logistic_Model=glm(Class~.,test_data,family=binomial())
  2. summary(Logistic_Model)

Output Screenshot:

credit card fraud detection

After we have summarised our model, we will visual it through the following plots –

Code:

  1. plot(Logistic_Model)

Input Screenshot:

Data Modeling

Output:

 

logistic regression model output

Output:

logistic output 3

 

Output:

logistic regression output

 

Output:

logistic output

 

In order to assess the performance of our model, we will delineate the ROC curve. ROC is also known as Receiver Optimistic Characteristics. For this, we will first import the ROC package and then plot our ROC curve to analyze its performance.

Code:

  1. library(pROC)
  2. lr.predict <- predict(Logistic_Model,train_data, probability = TRUE)
  3. auc.gbm = roc(test_data$Class, lr.predict, plot = TRUE, col = "blue")

Output Screenshot:

credit card fraud detection project

 

Output:

fraud detection - R project

6. Fitting a Decision Tree Model

In this section, we will implement a decision tree algorithm. Decision Trees to plot the outcomes of a decision. These outcomes are basically a consequence through which we can conclude as to what class the object belongs to. We will now implement our decision tree model and will plot it using the rpart.plot() function. We will specifically use the recursive parting to plot the decision tree.

Code:

  1. library(rpart)
  2. library(rpart.plot)
  3. decisionTree_model <- rpart(Class ~ . , creditcard_data, method = 'class')
  4. predicted_val <- predict(decisionTree_model, creditcard_data, type = 'class')
  5. probability <- predict(decisionTree_model, creditcard_data, type = 'prob')
  6. rpart.plot(decisionTree_model)

Input Screenshot:

ML project - decision tree

 

Output:

machine learning project - decision tree

 

7. Artificial Neural Network

Artificial Neural Networks are a type of machine learning algorithm that are modeled after the human nervous system. The ANN models are able to learn the patterns using the historical data and are able to perform classification on the input data. We import the neuralnet package that would allow us to implement our ANNs. Then we proceeded to plot it using the plot() function. Now, in the case of Artificial Neural Networks, there is a range of values that is between 1 and 0. We set a threshold as 0.5, that is, values above 0.5 will correspond to 1 and the rest will be 0. We implement this as follows – 

Code:

  1. library(neuralnet)
  2. ANN_model =neuralnet (Class~.,train_data,linear.output=FALSE)
  3. plot(ANN_model)
  4. predANN=compute(ANN_model,test_data)
  5. resultANN=predANN$net.result
  6. resultANN=ifelse(resultANN>0.5,1,0)

Input Screenshot:

Neural Networks

Output:

Artificial Neural network in R

8. Gradient Boosting (GBM)

Gradient Boosting is a popular machine learning algorithm that is used to perform classification and regression tasks. This model comprises of several underlying ensemble models like weak decision trees. These decision trees combine together to form a strong model of gradient boosting. We will implement gradient descent algorithm in our model as follows –

Code:

  1. library(gbm, quietly=TRUE)
  2. # Get the time to train the GBM model
  3. system.time(
  4. model_gbm <- gbm(Class ~ .
  5. , distribution = "bernoulli"
  6. , data = rbind(train_data, test_data)
  7. , n.trees = 500
  8. , interaction.depth = 3
  9. , n.minobsinnode = 100
  10. , shrinkage = 0.01
  11. , bag.fraction = 0.5
  12. , train.fraction = nrow(train_data) / (nrow(train_data) + nrow(test_data))
  13. )
  14. )
  15. # Determine best iteration based on test data
  16. gbm.iter = gbm.perf(model_gbm, method = "test")

Input Screenshot:

gradient boosting algorithm in R

 

Code:

  1. model.influence = relative.influence(model_gbm, n.trees = gbm.iter, sort. = TRUE)
  2. #Plot the gbm model
  3. plot(model_gbm)

Input Screenshot:

R project - gradient boosting

 

Output:

Gradient Boosting

Output:

gradient boostingCode:

  1. # Plot and calculate AUC on test data
  2. gbm_test = predict(model_gbm, newdata = test_data, n.trees = gbm.iter)
  3. gbm_auc = roc(test_data$Class, gbm_test, plot = TRUE, col = "red")

Output Screenshot:

gbm input

Code:

  1. print(gbm_auc)

Output Screenshot:

Credit card fraud detection

Summary

Concluding our R Data Science project, we learnt how to develop our credit card fraud detection model using machine learning. We used a variety of ML algorithms to implement this model and also plotted the respective performance curves for the models. We learnt how data can be analyzed and visualized to discern fraudulent transactions from other types of data.