In this blog post I will implement an artificial neural network using keras package to do churn prediction. In the previous blog post I talked about neural network representation and learning. I suggest you to read them before reading this blog post(click here to read).
The code is written in jupyter notebook, to access the data and notebook click here.
What is Churn prediction?
Churn prediction is one of the most popular Big Data use cases in business. It consists of detecting customers who are likely to cancel a subscription to a service.Churn prediction can be extremely useful for customer retention and by predicting in advance customers that are at risk of leaving.
Let’s solve the problem using an ANN!
To start with any machine learning problem we first completely understand the given data we are feeding to the network. The first step is importing the data and cleaning it.
Importing and Cleaning the Dataset :
To import and manipulate the data we will use the most popular packages in Python for data manipulation, pandas and numpy .
import pandas as pd
import numpy as np
Pandas is a data manipulation package and numpy is the fundamental package for scientific computing with Python.
Let’s read the data set(which is in csv format) into a variable called “dataset”
Let’s look into data using info function of Pandas dataframe
In this date data set we have 14 columns and 10,000 rows. The column contains 13 independent variables and one dependent variable (“Exited” column). We will predict dependent variable by feeding the independent variables to the network.
Now let’s separate dataset into X(independent variables) and y(dependent variable). In X we’ll slice the data frame from third column to 13th(since we don’t need “RowNumber”,”CustomerId” and “Exited”) and in y we only need “Exited”
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values
The next step will be preprocessing the data.
Since we have some categorical variables(“Country” and “Gender” variable) in our data and therefore we need to encode them.
To encode the variables using labelencoder and onehotencoder which are present in the scikit-learn package , we will apply the following code:
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
# creating dummy variable for country variable
onehotencoder = OneHotEncoder(categorical_features = )
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]
Note that I removed a dummy variable of “Country” to avoid falling into dummy variable trap.
Now we will split the data into a test set and training set using train_test_split function
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)
Feature scaling is an important part in deep learning also to ease the calculations.
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
Now our data is well preprocessed and now we will build the artificial neural network.
Building the ANN
To start with building the ANN we will first import the required libraries and packages .
from keras.models import Sequential
from keras.layers import Dense
The sequential module is required to initialize the ann and dense module is required to add layers to it.
Now we will initialize the deep learning model as a sequence of layers
Now we will add the input layer and hidden layer to the model.
#first hidden layer
I have defined 6 units in the first hidden layer and used rectifying linear function(ReLu) as the activation function. And since input dimensions(no. of features) are 11 we will define input_dim=11.
Now we will add second hidden layer
Adding the output layer:
It will have one output unit and we will use sigmoid function since it’s a binary classification.
Now since we have defined the model of the neural network, we will compile the model.
Fitting the training data:
Now to train the neural network we have to fit the training data or feed the train data to the network .It is pretty simple, we can do this by using fit function.
Now our neural network will learn from the training data by using forward propagation, gradient descent and backward propagation.
Now the final step is to predict that test data and check the accuracy of our model two new data.
y_pred=(y_pred > 0.5)
Now let’s make the confusion matrix for the model.
from sklearn.metrics import confusion_matrix
This confusion Matrix will validate our data model and tell us how well our model works on the data provided to it.
It was my first project on implementing a neural network so please let me know what you think about it. Also please subscribe to the blog to get updated on every new posts , do share it . And I would love to hear your feedback . You can also contact me if you have any suggestions or ideas or anything. I’ve also added a forum to the site , so if u have any problem or doubt regarding any topic , you can ask there or help others if you know the answer of any question there.