Credit Card Fraud Detection with Machine Learning
In this article we will show how to detect credit card fraud with Machine Learning algorithms. Cyber fraud refers to those scams that use the network to carry out…
A learning project shared by the community. The article documents its team, methods and results; it does not establish that the prototype is ready for real-world use.

This archive includes automatic translations. Audio, video and text inside images remain in their original language.
In this article we will show how to detect credit card fraud with Machine Learning algorithms.
Cyber fraud refers to those scams that use the network to carry out illicit transactions (Condusef, s.f). Within this type of fraud, credit card fraud is the order of the day due to the popularity of online purchases. Some user data is enough for a hacker to commit the crime. Credit card fraud involves the unauthorized use of a person's credit card information for the purpose of charging purchases to the victim's account or withdrawing funds from their account (Cornell Law School, n.d.). In other words, it is a type of identity theft.
In this scenario, fraud detection with Machine Learning for credit card transactions is of utmost importance. Currently, large amounts of money are lost due to this type of fraud, which represents a great problem for financial institutions and their users.
Faced with this type of situation, companies usually replace cards, undertake investigations into the cases and offer telephone support to customers, which implies a significant expense. Even with these attentions, a poor reputation and image of the company can arise, which is eventually reflected in a loss of clients and income.
How to avoid or minimize the costs of fraudulent transactions?
Artificial intelligence provides machine learning algorithms capable of identifying patterns in transactions and detecting whether a certain transaction is fraudulent or not.
Project Description
The general objective of the project is to develop a fraud detection method using Machine Learning algorithms capable of effectively classifying between legitimate and fraudulent transactions.
Imagen recuperada de: IEEE Innovation at work
Data collection
For confidentiality reasons, (real) credit card transactions cannot be shared publicly. Due to the scarcity of data, a data simulator of legitimate and fraudulent transactions will be created.
The essential characteristics that summarize a transaction are: transaction ID, transaction date and time, customer ID, terminal ID, transaction amount, and a label that shows whether the transaction is fraudulent or not.
The simulation consists of five steps:
1. Customer Profiling: Every customer is different in their spending habits. This will be simulated by defining some properties for the customer such as their geographic location, their spending frequency, and their spending amounts.
2. Terminal Profiling: Here, the only property that will be taken into account is the geographical location.
3. Association of customer profiles and terminals: it is assumed that customers only transact on terminals that are within a radius r, of their geographical locations.
4. Transaction generation: the simulator will go through the set of customer profiles and generate transactions based on their properties.
5. Generation of fraud scenarios: transactions are labeled as legitimate or fraudulent, using the following fraud scenarios.
a. Scenario 1: Any transaction whose amount is greater than 220 is labeled as fraudulent. (Scenario not inspired by reality, the value is chosen according to the distribution that the amounts variable follows).
b. Scenario 2: Every day two terminals are randomly removed. All transactions on these terminals in the next 28 days will be labeled as fraudulent. (Phishing).
c. Scenario 3: Every day 3 clients are randomly drawn. In the next 14 days, 1/3 of your transactions have their amounts multiplied by 5 and are labeled as fraudulent. (Card not present fraud).
Thus, we have the following set of data:

This simulator generated 1,078,699 transactions, of which 8,590 (0.796%) are fraudulent. Approximately 12,000 transactions are generated per day, approximately 103 fraudulent transactions per day and approximately 88 fraudulent cards per day.
The dataset presents an imbalance, less than 1% of transactions are fraudulent.
The transaction amounts have a mean of 53.69, with a standard deviation of 42.13 and a maximum amount of 1 248.85.
The first transaction was made on 05-01-2021 at 00:01:02 hours and the last transaction was recorded on 07-29-2021 at 23:59:43 hours.
Data preparation
For fraud detection, Machine Learning algorithms generally require numerical and ordered characteristics, that is, the type of variable must be an integer or real number where the order of the values is significant.
In this dataset, the only numerical and ordered features are the transaction amount and the fraud label. The datetime is a timestamp, therefore it is not numeric. IDs for transactions, clients, and terminals are numerical, but not ordered. So, new features should be designed based on the features mentioned above.
The first transformation consists of generating two new binary features, from the date and time, that characterize potentially relevant periods.
1. If a transaction occurs during a business day (0) or weekend (1).
2. Whether a transaction occurs during the day or night. (The night between 0 p.m. and 6 a.m.)
The second transformation has to do with the customer ID, two characteristics are calculated at 3 frequencies. The first characteristic is the number of transactions that occur within a frequency. The second characteristic is the average amount spent on these transactions, at each frequency. The frequency is set to 1, 7 and 30 days. This generates 6 new features.
Finally, the last transformation is done with the terminal ID. The objective is to extract the number of transactions on the terminal at each frequency and a risk score, which evaluates the exposure of a terminal to fraudulent transactions. (The risk score is defined as the average number of fraudulent transactions on a terminal over a period of time.)
Model training
Once the dataset is ready, the dataset is partitioned into training data (60%) for development and test data (40%) for model validation.
Since we are working with a binary classification problem, the following algorithms are used: Naive Bayes, Logistic Regression, KNN, Decision Tree, Random Forest, XGBoost and AdaBoost.

Model evaluation and validation
After training the algorithms, the results are evaluated with different metrics and the following information is obtained:

The metric that best evaluates the performance of the algorithms, without causing overfitting, is the ROC AUC Score. This metric indicates that the best model for predicting credit card fraud is the Decision Tree with an accuracy of 83%.
Another way to check this is to use the AUC-ROC curve, which indicates how much the model is able to distinguish between classes. ROC is a probability curve and AUC represents the degree or measure of separability. The following graph indicates the AUC-ROC curves for all algorithms:

The area under the ROC curve is the best overall indicator of the precision of a test, for the Decision Tree the AUC score is 0.836.
The results of the model can be expressed using a confusion matrix:

It can be seen that the model has identified:
- 426,669 cases of true negatives, for transactions NOT fraudulent and that the model predicted as NOT fraudulent.
- 2,394 cases of true positives, for fraudulent transactions and that the model predicted as fraudulent.
- 1,099 cases of false negatives, for fraudulent transactions and that the model predicted as NOT fraudulent.
- 1,318 cases of false positives, for transactions that were NOT fraudulent and that the model predicted as fraudulent.

A learning curve shows the relationship between the training score and the cross-validated test score for an estimator with a varying number of training samples.
This curve shows high test variability (1.00) and an AUC ROC score converging to around 0.83.
Conclusions
- The best Machine Learning model for fraud detection is the Decision Tree Classifier with an accuracy of 83%.
- Due to the amount of transaction data, in the training phase it is essential to have computational power to experiment with different algorithms and evaluate their results in a reasonable time.
- The developed credit card fraud detection method can be effectively applied in financial institutions, however, it is important to consider other fraud scenarios to optimize the model and not compromise the predictive power.
References
Barrios, J. (2019). The confusion matrix and its metrics. Recovered from: https://bit.ly/3C3ibLE
Condusef (s.f). Types of fraud. Recovered from: https://bit.ly/3kbcmpz
Cornell Law School (n.d.). Credit Card Fraud. Recovered from: https://bit.ly/3lgOCQ2
Gebejes, A. & Khokhlova, A. (2021). Learning Curve Theory. Data of Learning. VALAMIS. Recovered from: https://bit.ly/3lhipbp
Le Borgne, Y. & Bontempi, G. (2021). Machine Learning for Credit Card Fraud Detection — Practical Handbook. Université Libre de Bruxelles. Recovered from: https://bit.ly/3k7tbBH
McKinney, T. (s.f). Fraud Detection in Python. Recovered from: https://bit.ly/3lb40xiSaturdays.AI
WRITTEN BY
Martin Mercado
More Artificial Intelligence!
The mission of Saturdays.ai is to make artificial intelligence more accessible (#ai4all) through intensive courses and programs where projects for good are carried out (#ai4good).
Explore our AI master’s programme at https://saturdays.ai/master-ia-online/
If you want to learn more about artificial intelligence join our community at community.saturdays.ai or visit us on our website www.saturdays.ai Join us!
A history of the Saturdays.AI community. Originally published on Nov 19, 2021.
← More community stories



