Develop Online Payments Fraud Detection system using Artificial Intelligence

Every year, billions of Rupees are lost due to online fraud, causing huge losses for users and the financial industry. This kind of illicit activity is perhaps the most common and the one that causes most concerns in the finance world. In recent years great attention has been paid to the search for techniques to avoid this significant loss of money. In this project, we will build an application named as “Online Payments Fraud Detection system using Artificial Intelligence” by using an imbalanced dataset that contains transactions. Application the Online Payments Fraud Detection system about to detect fraud while customer pay online for shopping. It specifically is very risky to basically kind of buy item and literally really pay online there definitely for all intents and purposes so we will build this application to detect fraud in advance. In this application we use machine learning techniques for detection of fraud customer in a subtle way.

Table of Content:

  • Introduction

  • Aim and Objective

  • Install and Import 

  • Dataset

  • Data Analysis

  • Pre-processing

  • Pycaret
    • Model Selection
    • Model Training
    • Model Saving 
  • Development
    • app.py
    • Procfile
    • stepup.sh

Introduction :

                  In modern day’s Online Transaction plays an important role in every person’s daily activity. Customer purchases their needs with their online transitions. Banks and financial institutes consider denying the applications of customers to avoid the risk of defaulters. Risk is the rise of debt on the customer who fails to make the billing payment for some period. The purpose of the project is how to reduce the defaulters among the list of customers, and make a background check on whether to provide the loan or not and to find the promising customers. These predictive models would benefit the lending institutions and to the customers as it would make them more aware of their potential defaulting rate. The dataset is unbalanced so the focus was on the precision and recall more than the accuracy metrics. After comparison with 13 models and random forest is the best model based on the False Negative value of confusion metrics.

Aim and Objective:

        The problem is to classify the defaulters and non-defaulters on the credit payment of the customers. This project is helpful for solving the real problem by using various classification techniques. Moreover, any user can access GUI and add their gender, education, marital status and payment details to check next month in which category they fall (defaulter or non-defaulter). The core objectives: Find whether the customer could pay back his next credit amount or not and Identify some potential customers for the bank who can settle their credit balance. The steps followed to manage these goals:

  • Selection of dataset
  • Display some graphical information and visualize the features.
  • Check Null values in the dataset
  • Data pre-processing using one-hot encoding and remove extra parameters
  • Train with classifiers
  • Evaluate the model with test data
  • Compare the accuracy, precision and recall finding the optimal model.
  • Created a Graphical User Interface to check with real time customer data and predict defaulter for their next month payment

Install & Import :

Code:

 !pip install pycaret
  from pycaret.utils import version 
  version()
# only run this cell if you are using google colab
  from pycaret.utils import enable_colab
  enable_colab()
# run this cell to install pycaret in Google Colab
#import Libraries
  import numpy as np
  import pandas as pd
%matplotlib inline
  import matplotlib.pyplot as plt
  import matplotlib.lines as mlines
  from mpl_toolkits.mplot3d import Axes3D
  import seaborn as sns
  from pycaret.classification import *

 

Dataset:

             This dataset is presently only one of four on Kaggle with information on the rising risk of digital financial fraud, emphasizing the difficulty in obtaining such data. The main technical challenge it poses to predicting fraud is the highly imbalanced distribution between positive and negative classes in 6 million rows of data. Another stumbling block to the utility of this data stems from the possible discrepancies in its description [1], [2], [3]. The goal of this analysis is to solve both these issues by a detailed data exploration and cleaning followed by choosing a suitable machine-learning algorithm to deal with the skew. I show that an optimal solution based on feature-engineering and extreme gradient-boosted decision trees yields an enhanced predictive power of 0.997, as measured by the area under the precision-recall curve. Crucially, these results were obtained without artificial balancing of the data making this approach suitable to real-world applications

You can downlaod the dataset form Click here

data = pd.read_csv("/content/drive/MyDrive/PS_20174392719_1491204439457_log.csv")
data.head()

plt.figure(figsize=(15,8))
sns.boxplot(hue = 'isFraud', x = 'type', y = 'amount', data = data[data.amount < 1e5])

Data Analysis:

print("Number of records:\t\t",data.shape[0])

print("Number of features per record:\t",data.shape[1])
print("Any missing data?",data.isnull().sum().any())
print("No of Valid transactions:",data.isFraud.value_counts()[0],'which is ',round(data.isFraud.value_counts()[0]/data.shape[0] * 100,2),'%')
print("No of Fraud transactions:",data.isFraud.value_counts()[1],'which is ',round(data.isFraud.value_counts()[1]/data.shape[0] * 100,2),'%')

data.isFraud.value_counts()
print("Any transaction with amount less than or equal to 0?")
print(len(data[data.amount<=0]))
print("What type of transactions are they?")
print(data[data.amount<=0]['type'].value_counts().index[0])
print("Are all these marked as Fraud Transactions?")
data[data.amount<=0]['isFraud'].value_counts()[1] == len(data[data.amount<=0])
data_temp = data[data.isFlaggedFraud==1]
print("How many frauds transactions are Flagged?:")
print("\t",len(data_temp))

print("What type of transactions are they?")
print("\t",data_temp['type'].value_counts().index[0])

print("Are all these flagged also marked as Fraud Transactions?")
print("\t",data_temp['isFraud'].value_counts()[1] == len(data_temp))

print("Minumum amount transfered in these transactions")
print("\t",data_temp.amount.min())

print("Maximum amount transfered in these transactions")
print("\t",data_temp.amount.max())
d = data.groupby('type')['amount'].sum()
plt.figure(figsize=(10,8))
ax = sns.barplot(x=d.index,y=d.values)
for p in ax.patches:
ax.annotate(str(format(int(p.get_height()), ',d')), (p.get_x()+0.24, p.get_height()*1.01))
plt.title("Total amount in each transaction type")
plt.yticks([])
plt.xlabel("Transaction Type")
plt.show()

 

plt.figure(figsize=(10,8))
plt.pie(data.type.value_counts().values,labels=data.type.value_counts().index, autopct='%.0f%%')
plt.title("Transaction Type")
plt.show()

Pre-processing:
  • The first step is data preprocessing. Data preprocessing used to convert the raw data into a clean data set.
  • ID column dropped as its unnecessary for our modeling.
  • Numeric attributes converted to nominal.
  • One hot encoding which is a process by which categorical variables converted into a dummy form that provided to algorithms to do a better job in prediction. One hot encoder used to perform linearization of data.
  • For change of categorical data into numeric form we use factorize function form pandas
    • 1 for CASH_OUT
    • 2 for CASH_IN
    • 3 for PAYMENT
    • 4 for TRANSFER
    • 0 for DEBIT

 

data.drop(['step','nameOrig','nameDest','isFlaggedFraud','oldbalanceDest','newbalanceDest'],axis=1,inplace=True)
data['type'] = pd.factorize(data['type'], sort=True)[0]

data.head()

 

plt.figure(figsize=(12,8))
sns.pairplot(data[['amount', 'oldbalanceOrg', 'type', 'isFraud']], hue='isFraud')

data.describe()

plt.figure(figsize=(12,10))
cor = data.corr()
sns.heatmap(cor, annot=True, cmap=plt.cm.Reds)
plt.show()

Pycaret:

  • For Normalization with z score we use built in funcition from pycaret
  • For Feature Seleciton we use built in funcition from pycaret
  • In pycaret the be defalut training size is * 0.7 * left dataset is for testing

from pycaret.classification import *
clf = setup(data, target = "isFraud", session_id=1498, normalize = True, feature_selection = True)

Model Selection:

best = compare_models()

best

Model Training:

lr = create_model('rf')

Performance Metric:

predict_model(lr)

Confusion Matrix:

plot_model(lr, plot = 'confusion_matrix')
plot_model(lr, plot = 'class_report')

Saving Model:

save_model(lr, 'rfmodel')
f

Development:

app.py

import streamlit as st
import pandas as pd
import numpy as np
from pycaret.regression import load_model , predict_model
saved_lr = load_model('rfmodel')

def predict(model , input_df):
prediction_df= predict_model(estimator=saved_lr , data = input_df)
prediction = prediction_df['Label'][0]
return prediction


def run():
st.sidebar.image("Untitled.png",caption='Virtual University Of Pakistan')
st.title("Online Payments Fraud Detection system")
st.sidebar.subheader('Under the Supervision of Dr. Mushtaq Hussain ')
get= st.selectbox("Type",['CASH_OUT','CASH_IN','PAYMENT', 'TRANSFER','DEBIT'])
if get == 'CASH_OUT':
Type = 1
if get == 'CASH_IN':
Type = 2
if get == 'PAYMENT':
Type = 3
if get == 'TRANSFER':
Type = 4
if get == 'DEBIT':
Type = 5
amount = st.number_input("Amount" )
oldbalanceOrg= st.number_input("Initial balance before the transaction" , value= 1)
newbalanceOrig= st.number_input("Customer's balance after the transaction" , value= 1)
output = ""
input_dict = {'type': Type , 'amount': amount , 'oldbalanceOrg':oldbalanceOrg,'newbalanceOrig':newbalanceOrig}
input_df=pd.DataFrame([input_dict])
st.sidebar.bar_chart(input_df)
if st.button('Predict'):
output=predict(model=saved_lr , input_df= input_df)
if output == 1:
output='You do a Fraud Transaction'
if output == 0:
output = 'Your Transaction is Not Fraud'
st.success(output)

run()

Procfile:

Make a text file with name Procfile and write this code in it

web: sh setup.sh && streamlit run app.py

Setup.sh

also make this file add this code 

mkdir -p ~/.streamlit/

echo "\
[server]\n\
headless = true\n\
port = $PORT\n\
enableCORS = false\n\
\n\
" > ~/.streamlit/config.toml

After this all the data upload on github and it connect with Heroku you  code will deploy on could


Process








References 

  

Last modified: Friday, 10 February 2023, 6:57 PM