Credit Card Fraud Detection — Random Forest Classifier

Using the Random Forest Classifier model we will make a model that will predict whether a person will pay the dues on time (with source code).

Aviral Bhardwaj
3 min readApr 30, 2023

As in one of my previous article, I have given you an introduction to . Now, Random Forest in this article, I’ll demonstrate how to build a Random Forest Classifer model using a few lines of code.

Click on this link to learn more about the Random Forest model.

So let’s start

The first step is we need to download the dataset and then apply the dataset to the model. You can download or copy data from the URL —

https://raw.githubusercontent.com/aviralb13/git-codes/main/datas/credit.csv

Importing the libraries

Now we will import pandas and NumPy as shown below. If your system does not have these libraries installed, you may get them using the pip command.

Data preparation

Now we’ll read the data using Pandas and save it in a variable called data so we don’t have to call it again and again. Using the head command, we can view the first 5 components of the data; if you want to see more, enter the number inside the bracket.

Defining X and Y

And now I have created a list in which I think it will be the deciding factor for the predicting the risk of a fraud i.e (Gender, age, debt, and etc.) and assign it to variable feature. Now I will pass these features in the dataset and store it as x and the result of diagnosis as y.

I believe that these x parameters are more appropriate, and if you want to modify the parameters because you believe they are relevant, you can do so.

Splitting the Dataset

We must first import the test train split from sklearn model selection before splitting our model dataset into a train and test dataset.

Model

Here, we would be making a random forest classifier because we have to classify our values into yes or no. To do this, we would import the random forest classifier from sklearn.ensemble and then fit our training data into the model to train the model.

And finally, our model is ready now we are all set to predict from our model.

Accuracy

Now, we will see how to get our model accuracy here our model is 90% accurate which means it has guessed 90 values correct out of 100 which is a very good accuracy.

Source code

You can go check the link for full code.

Well, if you like this article you can check out my articles for more interesting articles in the field of artificial intelligence and machine learning.

Conclusion

If you found this article useful please appreciate it by giving claps and follow me for more interesting articles. Well, I have good news for you I would be bringing more articles to explain machine learning concepts and models with codes so leave a comment and tell me how excited are you about this.

--

--

Aviral Bhardwaj

One of the youngest writer and mentor on AI-ML & Technology.