Professional Documents
Culture Documents
Building An Artificial Neural Network With Keras
Building An Artificial Neural Network With Keras
If you know the customers that will churn, you can provide these
customers with better offers. So you can retain them. We will use
machine learning to determine customers that are likely to churn. We
have a sample dataset from a bank. We will predict the customers that
will stop banking with this bank.
Prerequisites
To follow along with this tutorial, you need to have:
Table of contents
• Import Libraries
• Data preprocessing
• Build and visualize the Artificial Neural Network
• Training the ANN
• Evaluating the model
Import libraries
Most of the libraries we will be using have been pre-installed on Google
Colab. So, we import them into our code:
import numpy as np
import pandas as pd
import tensorflow as tf
Output
'2.5.0'
Let us load our dataset. If you are running the codes with Google Colab,
then upload the dataset first. Click on the folder icon on the left panel.
Data preprocessing
Not all the features in our dataset are helpful. We do not need the row
number, customer id, and customer names. These features will not
help us predict if the customer will churn. Hence, we can get rid of
them. We use the code below to separate the features and the label.
X = dataset.iloc[:, 3:-1].values
y = dataset.iloc[:, -1].values
print(X)
print(y)
Next, using the code below, we split our dataset into training and
testing set:
# split the dataset into train and test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size = 0.2, random_state = 0)
To keep things simple, we use two hidden layers. The initial hidden
layer has 12 nodes, while the next layer has 8 nodes. In the hidden
layers, we use the relu activation function.
Finally, we add the output layer. We use a single node at the output
layer since we have only two categories. We also use the sigmoid
activation function at the output layer. It will give us the probability of
a customer churning.
# Initializing the ANN
ann = tf.keras.models.Sequential()
# Add the input layer and first hidden layer
ann.add(tf.keras.layers.Dense(units=12, activation='relu',
input_shape=X_train[0].shape))
# Add the second hidden layer
ann.add(tf.keras.layers.Dense(units=8, activation='relu'))
# Add the output layer
ann.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
Now that we have created our model, let’s use the code below to
visualize it:
from tensorflow.keras.utils import plot_model
plot_model(ann,
to_file="model.png",
show_shapes=True,
show_layer_names=True,
)
Output
Record Details
Country Spain
Gender Male
Age 40 years
Tenure 3 years
Output
[[0.04270527]]
We can add a threshold of 0.5. The customer will leave the bank if the
predicted probability is above 0.5. In extreme situations, we can
increase the threshold. That is if we want our model to
predict True only if it is very confident.
print(ann.predict(sc.transform([[0, 0, 1, 600, 1, 40, 3,
60000, 2, 1, 1, 50000]])) > 0.5)
Output
[[False]]
This is great news for the bank! This customer will not churn. Let us
assess our model using the test set:
y_pred = ann.predict(X_test)
y_pred = (y_pred > 0.5)
pd.DataFrame(list(zip(y_test, y_pred)), columns=['Actual',
'Predicted'])
It looks like our model got most of the predictions right. But, it made a
mistake for the second customer in our test set. We can check the
accuracy score, and build a confusion matrix.
from sklearn.metrics import confusion_matrix, accuracy_score
print(confusion_matrix(y_test, y_pred))
print(accuracy_score(y_test, y_pred))
Output
[[1506 89]
[193 212]]
0.859
The accuracy score is 85.9%. Out of 2000 cases, our model predicted
1718 cases correctly. The confusion matrix shows the number of True
Positives, False Positives, False Negatives, and True Negatives.
Conclusion
In this guide, we learned how to build, visualize and train an ANN
using Keras. We made a model that shows the customers that will
leave a bank.