Building your own Artificial Neural Network from scratch on Churn Modeling Dataset using Keras in Python

Redo more Epochs.Importing the Keras libraries and packagesimport kerasFor building the Neural Network layer by layerfrom keras.models import SequentialTo randomly initialize the weights to small numbers close to 0(But not 0)from keras.layers import DenseInitializing the ANN…So there are actually 2 ways of initializing a deep learning model— — — 1)Defining each layer one by one— — — 2)Defining a GraphWe did not put any parameter in the Sequential object as we will be defining the Layers manuallyclassifier = Sequential()Adding the input layer and the first hidden layer…This remains an unanswered question till date that how many nodes of the hidden layer do we actually need?There is no thumb rule but you can set the number of nodes in Hidden Layers as an Average of the number of Nodes in Input and Output Layer Respectively.(Works in 90% of the cases!!) — →Here avg= (11+1)/2==>6 So set Output Dim=6 — →Init will initialize the Hidden Layer weights uniformly — →Activation Function is Rectifier Activation Function(Relu)Input dim tells us the number of nodes in the Input Layer.This is done only once and wont be specified in further layers.classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))Adding the second hidden layer…classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))Adding the output layer…classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))Sigmoid activation function is used whenever we need Probabilities of 2 categories or less(Similar to Logistic Regression)Switch to Softmax when the dependent variable has more than 2 categories.Compiling the ANN…classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])Fitting the ANN to the Training setclassifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)Part 3 — Making the predictions and evaluating the modelPredicting the Test set resultsy_pred = classifier.predict(X_test)y_pred = (y_pred > 0.5)if y_pred is larger than 0.5 it returns true(1) else false(2)print(y_pred)This Model when trained on the train data and when tested on the test data gives us an accuracy of around 86% in both of the cases.Which from our point of view is Great!!!Making the Confusion Matrixfrom sklearn.metrics import confusion_matrixcm = confusion_matrix(y_test, y_pred)print(cm)Obtained from Confusion Matrix.You may change values as per what is obtained in your confusion matrix.print(accuracy)Congratulations!.you just wrote your own Neural Network for theBank which had given you this task..You may now find out the Top 10% customers which are most likely to leave the bank(which is quite important for the bank).The Bank may now take corrective measures to avoid losing it’s customers and would not have done this without your help :PI hope you liked my Tutorial!.I am attaching the github(Now Gitlab :P) links to the code and the dataset.Sid-darthvader/Artificial-Neural-Network-on-Churn-Modeling-Dataset-for-a-BankArtificial-Neural-Network-on-Churn-Modeling-Dataset-for-a-Bank – Artificial Neural Network on Churn Modeling Dataset…github.comReferences-Machine Learning course by Kirill Eremenko and Hadelin de Ponteves. More details

Leave a Reply