
Back
June 28, 2024
ML Infra, Classifiers & RL
FRAUD DETECTION IN FINANCIAL TRANSACTIONS
Pfactorial
Share

Introduction
One fine day, you receive a message on your phone saying, 'Buy Bitcoin for
1000 rupees.' Upon seeing this message, you might laugh, thinking about this fraudulent message. Since you are technologically literate, you save yourself from fraud. However, what if a technologically illiterate person, who has heard about Bitcoin making people rich but doesn't even know the current price, sees this message? There is a slight chance that he might actually pay 1000 rupees to the fraudster. The fraudster might even go one step further by installing malware through the link opened for payment in the phone to collect different credentials.
This is an example of financial fraud.
Preventing financial fraud is one of the important challenges for financial institutions like banks, aiming to protect their customers from such incidents. The use of an AI model capable of detecting patterns in fraudulent activities could significantly contribute to preventing financial frauds. In the following sections let’s understand about different types of financial fraud and the use of AI in preventing it.
Types of financial transaction frauds
ATM skimming : Criminals install devices on ATMs to capture card information and PINs, allowing them to create cloned cards for unauthorized withdrawals.
Credit card fraud: Criminals obtain credit card information through various means and use it to make unauthorized purchases or withdraw cash.
Phishing: Phishing involves tricking individuals into providing sensitive information by posing as a trustworthy entity through emails, messages, or fake websites.
Identity theft: Identity theft involves stealing personal information, such as bank account details, to impersonate someone else and commit fraud.
Use of AI in preventing financial frauds
Phishing: Machine learning models can be trained to detect patterns in phishing emails, website contents, sms etc..
Pattern recognition: AI models can be trained to detect patterns in fraudulent financial transactions.
Behavior analysis: AI can be used to detect normal behaviour in financial transactions and abnormal transactions.
Fraudulent account creation prevention: AI can identify patterns associated with fraudulent account creation, helping prevent the establishment of fake accounts for illicit activities.
Challenges
Data leak: Your bank might use transaction data to train an AI model to detect fraud transactions. But if the data is stored in a cloud platform for training an AI model ,there is a risk of data leak.
Dynamic nature of fraud: Since Fraudsters come up with new techniques every day, there is a chance for our trained fraud classification model to misclassify fraud as not fraud.
Implementing your own fraud detection system
Until now we discussed the types of frauds in financial transactions and use cases and challenges of AI in preventing financial transaction frauds. Now, it’s time to build a fraud detection system of our own. We will be using online payments fraud detection dataset to train out machine learning model to classify fraudulent transactions.
The dataset contains different types of transactions and their corresponding classification.
Dataset format
features :
step: represents a unit of time where 1 step equals 1 hour
type: type of online transaction amount: the amount of the transaction
nameOrig: customer starting the transaction
oldbalanceOrg: balance before the transaction
newbalanceOrig: balance after the transaction nameDest: recipient of the transaction
oldbalanceDest: initial balance of recipient before the transaction
newbalanceDest: the new balance of the recipient after the transaction
isFraud: fraud transaction.

Data preprocessing
Before training the machine learning model we need to preprocess our data to make it suitable for the algorithm to learn. Here we are one-hot encoding the categorical feature type.
For example of there are 3 categories of types
'PAYMENT', 'TRANSFER', 'CASH_OUT', 'DEBIT', 'CASH_IN'
The one hot encoding for each category will be
[1,0,0,0,0] - payment
[0,1,0,0,0] - transfer
[0,0,1,0,0] - cash out
[0,0,0,1,0] - debit
[0,0,0,0,1] - cash in
We also remove columns from the dataset which are not useful for prediction
Split the data
After preprocessing the data we need to split the dataset into train and test sets. We can test our model’s performance using the test data which will not be used for training. Here we use 80 % of the data for training and the remaining 20% for testing.
Training
Here we use RandomForestClassifier for building our model. Random forest classifier is a classification model which uses multiple decision trees for classification. We call the fit method of the Randomforest classifier to build our model.
After successfully creating the model, the next step is to check our model’s performance. We can use the accuracy metric for calculating the performance of our model. Accuracy calculates the percentage of correct predictions from the test data.
For finding accuracy we compare the predictions on test data with the actual values using the accuracy score function.
Saving the model and the encoder
Always remember to save your model for future use, otherwise your model will be lost when you restart the runtime .Here we also save our encoder which is used to one hot encode the new data .We save the model and the encoder using pickle library
API
Ok, now we have saved our model. What is the next step? The next step is to create an API for our model so that people can access our model using a url.In python we can create apis easily using the flask framework.
Here we first load our model and encoder. Then we use predict function to do the one hot encoding of the type feature and return the predictions.
The ‘classify_fraud’ api endpoint is used to return the predictions from the predict functions. We need to send the data to our api using a post request like this:
Result
Now let’s see the results from our api after sending the input data for prediction.

On the right terminal we can see that our model classified the input as fraud.
Conclusion
In this blog we have explored the implementation of a simple fraud detection model. The process of implementing an AI model for different types of fraud detection might be different ,but the underlying idea is the same, ie, train the model on existing data to prevent future fraudulent activities. I hope you learned something new from our blog. Happy learning
