<jupyter_start><jupyter_text><jupyter_code># This notebook going to demonstrate a step by step overview of Bank Customer Continuity Prediction, 
# here we are going to perform the Predictive Analysis  with the Artficial Neural Network and 
# finally we will aslo perform the hyper parameter tunning.
# Reference: 
# - https://github.com/krishnaik06/Complete-Deep-Learning/blob/master/ANN/ann.py
# - https://www.youtube.com/watch?v=OTTOglLJxLU&list=PLZoTAELRMXVPGU70ZGsckrMdr0FteeRUi&index=17

# Steps:
# - Download the dataset from https://github.com/biswa-13/DataScience/blob/master/datasets/bank_customer_details.csv
# - Explore the data and remove the columns which are not going to help in our prediction
# - Then convert the categorical data into numerical data
# - Prepare the training and testing dataset
# - Create an ANN model
# - Compile and Train the model
# - Test the model
# - Perform HyperPArameter tunning
!pip install keras
# importing required libraries for handling the data
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sbn
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, accuracy_score

# required libraries for ANN
import keras
from keras.models import Sequential
from keras.layers import Layer, Dense, LeakyReLU, Dropout

print("Finish: Importing of libraries...")
# Load the dataset
dataset3 = pd.read_csv("sample_data/bank_customer_details.csv")
print("Finish: Load the dataset...")
# Expolre the dataset
dataset3.info()
# dataset.head()
# We can find that we have below mentioned columns 
## RowNumber	CustomerId	Surname	CreditScore	Geography	
## Gender	Age	Tenure	Balance	NumOfProducts	HasCrCard	
## IsActiveMember	EstimatedSalary	Exited
# among which "Exited" is the dependent variable and others are independent variables.
# here we have clean non-null data in every column therefore we dont need perform the Imputation,
# instead we can dircetly jump towards removing the unwanted columns 
# and converting the categorical feature into numerial feature.
sbn.countplot(x=("Geography"),hue="Gender", data=dataset3)
# Formatting data
new_geography = pd.get_dummies(dataset3['Geography'], drop_first=True)
new_geography.info()
# new_geography.head()
new_gender = pd.get_dummies(dataset3["Gender"], drop_first=True)
new_gender.info()
# new_gender.head()
# removing the unwanted columns like "CustomerId", "Surname"
dataset3.drop(["RowNumber","CustomerId", "Surname", "Geography", "Gender"], axis=1, inplace= True)
dataset3.info()
print("Finish: Formatting data...")
# preparing the final dataset

dataset3 = pd.concat([dataset3, new_geography, new_gender], axis=1)
dataset3.info()
dataset3.head()
print("Finish: preparing the final dataset..." )
# preparing training and testig dataset
train_input,  test_input, train_output, test_output = train_test_split(
    dataset3.drop(["Exited"], axis=1),
    dataset3["Exited"],
    test_size = .30,
    random_state = 101
)
train_input.info()
test_input.info()
train_output.head()
test_output.head()
print("Finish: preparing training and testig dataset...")
# Feature Scaling
sc = StandardScaler()
train_input = sc.fit_transform(train_input)
test_input = sc.fit_transform(test_input)
print("Finish: Feature Scaling...")
# Creating the ANN model

model = Sequential()
# adding the first hidden layer
model.add(Dense(units=6, input_dim=11, activation="relu", kernel_initializer='he_uniform' ))

# adding the second hidden layer
model.add(Dense(units=6, activation="relu", kernel_initializer="he_uniform"  ))

# adding the output layer
model.add(Dense(units=1, activation = "sigmoid", kernel_initializer= "glorot_uniform"))

print("Finish: Creating the ANN model...")
# Compiling the model
summary = model.compile(
    optimizer="Adamax",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)
print("Finish:  Compiling the model...")
# Training the model
history = model.fit(
    x = train_input,
    y = train_output,
    batch_size = 20,
    epochs=90,
    validation_split=.23,

)

print("Finish: # Training the model...")
# Plotting the accuracy graph
def plotGraph(history, string):
  plt.xlabel("epochs")
  plt.ylabel(string)
  plt.plot(history.history[string])
  plt.plot(history.history["val_"+string])
  plt.legend(['train','test'])
  plt.show()
# Demonstrating the accuracy of the model
plotGraph(history,"accuracy")
plotGraph(history,"loss")


# Accuracy measurement of the model
pred_reslt = model.predict(test_input)
pred_reslt = (pred_reslt > 0.5)
# Confusion Matrix
conf_mtrx = confusion_matrix(test_output, pred_reslt)

# Accuracy Score
acc_score = accuracy_score(test_output, pred_reslt)

print("Confusion Matrix -->",conf_mtrx)
print("Accuracy Score -->",acc_score)<jupyter_output>Confusion Matrix --> [[2286   92]
 [ 356  266]]
Accuracy Score --> 0.8506666666666667
{'fraction_non_alphanumeric': 0.10782111496834797, 'fraction_numerical': 0.014702879313865632, 'mean_word_length': 5.574496644295302, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '640768', 'license': 'Apache-2.0', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1502, 'n_words': 444, 'path': 'DS2_ANN_with_keras.ipynb', 'repo_name': 'biswa-13/DataScience', 'size': '4897'}