# Machine Learning: Online Payments Fraud Detection.

Online payment systems have made ease for humans to make transactions. But at the same time, there are so many frauds that are happening around, especially on the credit card. That is why Online payment fraud detection is important, especially to products and services that customers have never buyed for. Let's get into the step-by-step procedure for the task of online payment fraud detection with machine learning with Python.

To identify online payment fraud with machine learning we need to train the machine learning model for classifying fraudulent and non-fraudulent payments. for this, we need a dataset containing information about online payment fraud, so that we can understand what type of transactions lead to fraud.

Steps:

The first step is we need to import the libraries that will be needed to load the dataset and do the analysis part.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204271305/c86d2115-4c84-484c-bb0d-014a5672cd3a.png align="center")

Then we need to import the dataset through the pandas library and then display the library.

By the dataset, we can see that different categories has been divided.

1. step: represents a unit of time where 1 step equals 1-hour.
    
2. type: type of online transaction.
    
3. amount: the amount of the transaction.
    
4. nameOrig: customer starting the transaction.
    
5. oldbalanceOrg: balance before the transaction.
    
6. newbalanceOrig: balance after the transaction.
    
7. nameDest: recipient of the transaction.
    
8. oldbalanceDest: initial balance of the recipient before the transaction.
    
9. newbalanceDest: the new balance of the recipient after the transaction.
    
10. isFraud: fraud transaction.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204309848/7575d7b6-3d8f-4f67-9a5f-564d07ecabb3.png align="center")

Check the dataset whether there are any null values present in the dataset or not. and sum it up to see the results.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204329712/16d48fdf-8b96-4eff-96ab-24f4a3741598.png align="center")

value\_counts will give you the unique values from the original Series as the index and the count of each unique value as the corresponding values.

By the value counts you can see that there are sub-divisions that are

Cash\_out, Payment, Cash\_in, Transfer and debit.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204349569/616dda6a-e111-40c7-9b9f-704f179a7fff.png align="center")

Through the values count, the dataset it into a different form which is transaction and quantity.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204368526/e49f3dbe-f081-4fcd-82af-a8ef66c8cd1d.png align="center")

Plotting it through the library which is plotly. express

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204531808/10685db5-aa81-4cf5-a178-dd3a054a3029.png align="center")

Now, checking the correlation and then doing the sort\_values which gives the isFraud total amount.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204571683/63e0fa78-5cda-43e1-87c6-f7bd42a0c6a9.png align="center")

Now, using the map command to divide the variety of different categories.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204599036/94f98a61-4d0c-478b-80a1-a86805ff5969.png align="center")

importing the train\_test\_split for dividing the dataset into the two forms

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204618141/82fe184e-d441-4e0a-a473-51800a5c6a42.png align="center")

Now, dividing the total dataset into 2 categories x and y.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204644352/39756d59-d8e2-4d06-96be-8fee7ec5a604.png align="center")

Now, with the help of train\_test\_split getting the X\_train, x\_test, y\_train\_y\_test.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204664198/e959fb04-c3eb-4303-80f3-1a3324f09543.png align="center")

Finally, the most important part is to make the model using an Algorithm here we are using the algorithm DecisionTreeClassifier.

Then fitting the model with the X\_train and y\_train.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204683175/a604b9bc-a161-4db9-a10a-fb67a2a6c771.png align="center")

Getting the score of the model through the model.score

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204700863/cdaa5fab-cd83-4932-a203-10c5e5502c47.png align="center")

Now, we are doing the prediction of the new features. giving the new features and and model will predict it with the new features.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696204734285/c84054f4-37d2-4a6e-ad0d-58b4db95bf96.png align="center")

Thanks for reading,

Mohammed MuqafamUddin
