Detecting Credit Card Fraud using Tensorflow | by Sarah Beshr | Towards Data Science

In this article, we’ll be leveraging the power of deep learning to solve a key issue that credit card companies often have to address, namely detecting fraudulent transactions. For this task, a Deep Neural Network (DNN) will be trained in order to do exactly that. We’ll walk through the following steps:
Data OverviewData PreprocessingDNN model buildingDNN model evaluationConclusion
Data Overview
Data Preprocessing
DNN model building
DNN model evaluation
Conclusion
Without further ado, let’s get started!
We got the dataset we’re using today from Kaggle and it contains two days’ worth of transactions by European cardholders. It’s important to note that due to the confidential nature of the data, a PCA transformation was done on 28 features and we have no information on what those feature names are. The only features that haven’t undergone this transformation and we can identify are ‘Time’, ‘Amount’, and ‘Class’. ‘Time’ represents the seconds elapsed between each transaction and the first transaction in the dataset. ‘Amount’ denotes the amount of each transaction and ‘Class’ refers to our target variable with 0 referring to a normal transaction and 1 referring to a fraudulent one. Our dataset also contains a total of 284,807 rows.
One thing we’d expect (and hope) from this dataset is that the target variable’s instances are imbalanced. This makes sense, right? It should as fraudulent transactions typically represent a minority of the cases. We can confirm this using the code below. We’ll also be changing the target variable’s column name so that it’s more intuitive.
#Rename Classdata.rename(columns={"Class": "isFraud"}, inplace=True)#Percentage of fraudfraud_per = data[data.isFraud == 1].isFraud.count() / data.isFraud.count()print(fraud_per)
This indeed turned out to be the case as only 0.17% of our transactions are fraudulent (whew)! While a low percentage of credit card fraud is certainly good news for a credit card company, it actually threatens the predictive performance of our network, especially for the fraudulent cases, so we’ll remedy this later using SMOTE. We’ll now investigate whether our dataset contains missing data.
# Looking for missing dataprint(data.isnull().any().sum())
The output shows that our dataset contains no missing values. Next, a correlation matrix can help give us an all-rounded understanding of how the variables in our dataset relate to each other.
#Correlation Plotplt.figure(figsize = (14,10))plt.title('Correlation Plot', size = 20)corr = data.corr()sns.heatmap(corr,xticklabels=corr.columns,yticklabels=corr.columns,linewidths=.1,cmap="Blues",fmt='.1f',annot=True)plt.show()
It’s now time to prepare our data so that it’s ready for training.
The first thing we’ll be doing is defining our features and our target variable.
# Defining x and yy = data["isFraud"]x = data.drop(["isFraud"], axis = 1)
One of the best practices for training a Neural Network is standardizing your data as it speeds up the training process. We’ll do so with the following code.
#Standardizationscaler = StandardScaler()x = scaler.fit_transform(x)
We’re close to the most exciting part which is training our network but not just yet, we still need to define our training and testing dataset.
# Train-Test splitX_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0)
But wait, haven’t we mentioned earlier that we’re dealing with a highly imbalanced dataset? Well, we did, and we’ll address this using Synthetic Minority Oversampling Technique (SMOTE). It might sound complicated but all it really is is just an oversampling technique that creates artificial minority class samples. In our case, it creates synthetic fraud instances and so corrects the imbalance in our dataset.
# SMOTEX_train_SMOTE, y_train_SMOTE = SMOTE().fit_sample(X_train, y_train)#SMOTE plotpd.Series(y_train_SMOTE).value_counts().plot(kind="bar")plt.title("Balanced Dataset")plt.show()
Aaand now it’s time to train our DNN. Firstly, ‘input_dim’ represents the number of features that we’re using for our training and ‘units’ denotes the number of neurons in each layer. We’ve come to this number of neurons and layers in our network using a trial and error approach. We also used ReLU as our activation function for the hidden layers and a sigmoid function for our output layer. An in-depth explanation of why these activation functions are best for our use case can be found here. Lastly, we used multiple dropout layers to prevent our network from overfitting.
# DNNmodel = keras.Sequential([layers.Dense(input_dim = 30, units = 128, activation = "relu"),layers.Dense(units= 64, activation = "relu"),layers.Dropout(0.2),layers.Dense(units= 32, activation = "relu"),layers.Dropout(0.2),layers.Dense(units= 32, activation = "relu"),layers.Dropout(0.2),layers.Dense(units= 16, activation = "relu"),layers.Dropout(0.2),layers.Dense(units=1, activation = "sigmoid")])model.summary()
With our model architecture ready, it’s time to compile, train and evaluate the model.
# Metricsmetrics = [    metrics.Accuracy(name="Accuracy"),    metrics.Precision(name="Precision"),    metrics.Recall(name="Recall")]# Compiling and fiting the modelmodel.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = metrics)model.fit(X_train_SMOTE, y_train_SMOTE, batch_size = 32, epochs = 100)print("Evaluate on test data")score = model.evaluate(X_test, y_test)print("test loss, test accuracy, test precision, test recall:", score)
We used ‘adam’ as our optimizer as it’s computationally efficient and is well suited for problems with a high number of parameters and ‘binary_crossentropy’ as our loss function as it’s most appropriate for our binary classification problem. For our evaluation, we’ll not only focus on accuracy as a metric but we’ll assess precision and recall too. Now let’s have a look at how the last 10 epochs went and how well our model performed on our test data.
We can clearly see that our model has performed well on our test data!
With this, we can round up our demonstration of how a DNN can be trained to predict credit card fraud. To recap, we’ve had a quick walkthrough for our data structure. We’ve assessed our target class imbalance and addressed it using SMOTE and we’ve standardized our data so that our training process is faster. We’ve trained and assessed our model and had results of 98% accuracy, 85% precision, and 82% recall on our test data! Annd that’s it. I would love to hear any questions or comments that you may have.