Data Science Project – Detect Credit Card Fraud with Machine Learning in R

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.

Stay updated with latest technology trends
Join DataFlair on Telegram!!

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 hereFraud Detection Dataset

1. Importing the Datasets

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


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 –



Output Screenshot:

data science project in R




Output Screenshot:

credit card fraud detection



Output Screenshot:

credit card fraud detection



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:



Output Screenshot:

credit card fraud detection



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 –


data_sample = sample.split(NewData$Class,SplitRatio=0.80)
train_data = subset(NewData,data_sample==TRUE)
test_data = subset(NewData,data_sample==FALSE)

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 –



Output Screenshot:

credit card fraud detection

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



Input Screenshot:

Data Modeling



logistic regression model output


logistic output 3



logistic regression 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.


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

Output Screenshot:

credit card fraud detection project



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.


decisionTree_model <- rpart(Class ~ . , creditcard_data, method = 'class')
predicted_val <- predict(decisionTree_model, creditcard_data, type = 'class')
probability <- predict(decisionTree_model, creditcard_data, type = 'prob')

Input Screenshot:

ML project - decision tree



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 – 


ANN_model =neuralnet (Class~.,train_data,linear.output=FALSE)


Input Screenshot:

Neural Networks


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 –


library(gbm, quietly=TRUE)

# Get the time to train the GBM model
       model_gbm <- gbm(Class ~ .
               , distribution = "bernoulli"
               , data = rbind(train_data, test_data)
               , n.trees = 500
               , interaction.depth = 3
               , n.minobsinnode = 100
               , shrinkage = 0.01
               , bag.fraction = 0.5
               , train.fraction = nrow(train_data) / (nrow(train_data) + nrow(test_data))
# Determine best iteration based on test data
gbm.iter = gbm.perf(model_gbm, method = "test")

Input Screenshot:

gradient boosting algorithm in R



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


Input Screenshot:

R project - gradient boosting



Gradient Boosting


gradient boostingCode:

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

Output Screenshot:

gbm input



Output Screenshot:

Credit card fraud detection


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.

So, now you are ready to detect the fraud. Machine Learning and R are the important technologies of this decade and will last forever. What are you waiting for? Start learning the machine learning concepts for FREE with the help of DataFlair’s Machine Learning Tutorial Series.

Hope you enjoyed the above R project. Share your experience and queries through comments.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google | Facebook

30 Responses

  1. Mb says:

    Good Project, a very good way to analyse

  2. Michele Ferraz Figueiró says:

    Hello, thanks for the great material! I have a doubt about the Logistic Regression model fitted,
    wouldn´t this model be implemented on the training data? Because it has done on the test data.

  3. B. says:

    Where can we download creditcard.csv?

  4. Sandy says:

    How can I pass few transactions to validate those are fraudulent or not.

  5. Nidhi Hassan says:

    All this is good. But I wonder how you derived values: V1 to V28 from the credit card transaction data.

    • DataFlair Team says:

      Hi Nidhi,
      The values v1 to v28 are the results of PCA dimension reductions to protect sensitive information like user identities.

  6. Amir Haddad says:

    i think that the logistic model must be predicted on the test set
    lr.predict <- predict(Logistic_Model,test_data, probability = TRUE)
    auc.gbm = roc(test_data$Class, lr.predict, plot = TRUE, col = "blue")

  7. Vinu Yesudas says:

    Good project, can i get the paper for this project. ?

  8. Arun Kumar Sahoo says:

    Thanks for a complete project. Every other steps you followed for completing a DS project. But your analysis for a certain steps are incomplete like you drew the ROC curve but did not explain your obersavation regarding ROC curve, why you need this techniqe why not anything else. Please clarify

  9. Divya says:

    Without using seed function how we predict the logistic regression model

  10. Manideep j says:

    can i get the whole source code file.

  11. rose says:

    can you describe about the result obtained in the last code?please summarize according to the code

    • DataFlair Team says:

      Hello Rose,

      In the last section of the project, we calculate and plot an ROC curve measuring the sensitivity and specificity of the model. The print command plots the curve and calculates the area under the curve. The area of a ROC curve can be a test of the sensivity and accuracy of a model.

      I hope this answer will help you.


    Hi a pretty nice project though . will this project be enough as a final year project and can there be bit more better description on the graphs .

  13. Himani Singh says:

    Very nice project,,,Awesome Explanation and Very Informative, Thanks for sharing this code

  14. Ali Mehdizadeh says:

    Hi! Very nice project. May I ask what is the original source of the data? or whether it is actually a real data?

  15. Chirag Bellara says:

    Hey. It is a very nice project indeed. I was wondering if we can add a confusion matrix for each of the ML models and compare all the results in the end? Is it possible? If so can you help me with it?

  16. Panida says:

    hi I’m a student sorry for my english skil.
    I wonder why I can not run this code in my computer
    creditcard_data <- read.csv("/home/dataflair/data/Credit Card/creditcard.csv")
    I'm already dowmload cvsfile and change directory but it dosen't work
    Error in file(file, "rt") : cannot open the connection
    In addition: Warning message:
    In file(file, "rt") :
    cannot open file '/home/dataflair/data/Credit Card/creditcard.csv': No such file or directory
    could you please suggest me what to do thank you

    • Hamid Ghaedi says:

      Regardless of why you are getting the error:
      1- initialize an R instance
      2- use getwd() to find out your working directory
      3- Download and copy “creditcard.csv” to your working directory
      4- Use the command as follows: creditcard_data <- read.csv("creditcard.csv")
      This should work.

  17. Akanksha says:

    Could you please elaborate the final outcome. Why did we do gbm. ?? By applying ANN and Logistic regression both do we have to identify the best fitted model also ? You have applied so many things but the purpose of all the algos and final outcome is not clear.

  18. Hamid Ghaedi says:

    Nice Job,
    But I think it would far better to interpret the output of each step! It is hard to follow when you have no idea about what you got!
    Also as others mentioned above, in this code chunk:

    “lr.predict <- predict(Logistic_Model,train_data, probability = TRUE)
    auc.gbm = roc(test_data$Class, lr.predict, plot = TRUE, col = "blue")", please verify whether predict function should intake train_data as input or test_data? Using the code as it is would arise error because of difrent data dimentionality!

  19. Noob says:

    What is the name of the tool needed to run this project??

  20. Narendra Kharche says:

    This project is very good and knowledge base. One query is very questionable if we get proper usecase study/flow-chart with documentation. Really it is very helpful for interview purpose also.

  21. N Karthik says:

    Can u give a clear explanation about V1 to V28 ?

  22. Eid Heba says:

    How many observations is it? and how many variables? Can you post the full rmd file please. thank you!

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.