Churn Prediction : Implementation of neural network


Neural Network, Projects / Saturday, January 13th, 2018

Hello all!

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”

dataset=pd.read_csv(“Churn_Modelling.csv”)

Let’s look into data using info function of Pandas dataframe

dataset.info()

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”
field.

X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values

The next step will be preprocessing the data.

Preprocessing:

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 = [1])
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:

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 .

import keras
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

classifier=Sequential()

Now we will add the input layer and hidden layer to the model.

Adding Layers:

#first hidden layer
classifier.add(Dense(units=6,kernel_initializer=’uniform’,activation=’relu’,input_dim=11))

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

classifier.add(Dense(units=6,kernel_initializer=’uniform’,activation=’relu’))

Adding the output layer:

classifier.add(Dense(units=1,kernel_initializer=’uniform’,activation=’sigmoid’))

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.

classifier.compile(optimizer=’adam’,loss=’binary_crossentropy’,metrics=[‘accuracy’])

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.

classifier.fit(X_train,y_train,batch_size=10,epochs=100)

Now our neural network will learn from the training data by using forward propagation, gradient descent and backward propagation.

Prediction:

Now the final step is to predict that test data and check the accuracy of our model two new data.

y_pred=classifier.predict(X_test)
y_pred=(y_pred > 0.5)

Now let’s make the confusion matrix for the model.

from sklearn.metrics import confusion_matrix
cm=confusion_matrix(y_test,y_pred)

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.

Happy Learning!

3 Replies to “Churn Prediction : Implementation of neural network”

  1. Its like you read my mind! You appear to know a lot about this, like you wrote the
    book in it or something. I think that you could do with a few pics to drive
    the message home a bit, but instead of that, this is
    wonderful blog. A great read. I will definitely be back.

Leave a Reply

Your email address will not be published. Required fields are marked *