Deep Learning Neural Network Model using Keras in Python

Alaa Nfissi
4 min readJan 23, 2019

--

Data and Business Problem:

Our basic aim is to predict customer churn for a certain bank which customer is going to leave this bank service. Dataset is small(for learning purpose) and contains 10000 rows with 14 columns. You can download data from this Link.

Prerequisite: I am using Spyder by Anaconda, and you need to install Tensorflow, Theano and Keras libraries in spyder.

Step 1: Importing data.

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('Churn_Modelling.csv')

Step 2: Create matrix of features and matrix of target variable. In this case we are excluding column 1 & 2 as those are ‘row_number’ and ‘customerid’ which are not useful in our analysis. Column 14, ‘Exited’ is our Target Variable

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

Step 3: We will make analysis simpler by encoding string variables.

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])

Now the Country names are replaced by 0,1 and 2 while male and female are replaced by 0 and 1.

Step 4: Create dummy variable in python. LabelEncoder has replaced France with 0, Germany 1 and Spain 2 but Germany is not higher than France and France is not smaller than Spain so we need to create a dummy variable for Country.

onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]

Step 5: Divide our data into training and testing part as 80:20.

# Splitting the dataset into the Training set and Test setfrom sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

Step 6: We will fit and transform our train data and test data.

# Feature Scalingfrom sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

We are done with our data pre-processing now we will start with our model.

Building our Neural Network :

Step 7: Importing required Modules.

# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

Step 8: We are giving the name of model as Classifier as our business problem is the classification of customer churn.

#Initializing Neural Network
classifier = Sequential()

Input Layer and the first layer :

# Adding the input layer and the first hidden layerclassifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))

Second Layer:

# Adding the second hidden layer
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))

Output layer :

# Adding the output layer
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))

Step 10: Compiling Neural Network

# Compiling Neural Network
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

Step 11: We will now train our model on training data

# Fitting our model 
classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)

Step 12: Predicting the test set result.

# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)

Step 13: The final step is evaluating our model performance.

# Creating the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

Result:

The Accuracy of our model can be calculated as:

Accuracy= 1550+175/2000=0.8625

Thanks,

Alaa Nfissi.

Source: Udemy Machine Learning Course by kirill eremenko.

--

--

Alaa Nfissi
Alaa Nfissi

Written by Alaa Nfissi

Data Science Lead | Ph.D. Researcher.

No responses yet