<jupyter_start><jupyter_text>Importing Libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import style
style.use('dark_background')
df=pd.read_csv('C:\\Users\\91760\\Downloads\\archive (3)\\Churn_Modelling.csv')
df.head()
df.describe()
df.info()
df.shape
df.isna().sum()
values = df.Exited.value_counts()
labels = ['Not Exited', 'Exited']

fig, ax = plt.subplots(figsize = (4, 3), dpi = 100)
explode = (0, 0.09)

patches, texts, autotexts = ax.pie(values, labels = labels, autopct = '%1.2f%%', shadow = True,
                                   startangle = 90, explode = explode)

plt.setp(texts, color = 'grey')
plt.setp(autotexts, size = 8, color = 'white')
autotexts[1].set_color('black')
plt.show()<jupyter_output><empty_output><jupyter_text>20 Percent of the employes have not churned it yet<jupyter_code>fig, ax = plt.subplots(3, 2, figsize = (18, 15))
sns.countplot('Geography',hue='Exited',data=df,ax=ax[0][0])
sns.countplot('Gender',hue='Exited',data=df,ax=ax[0][1])
sns.countplot('Tenure',hue='Exited',data=df,ax=ax[1][0])
sns.countplot('NumOfProducts',hue='Exited',data=df,ax=ax[1][1])
sns.countplot('HasCrCard',hue='Exited',data=df,ax=ax[2][0])
sns.countplot('IsActiveMember',hue='Exited',data=df,ax=ax[2][1])
plt.show()<jupyter_output><empty_output><jupyter_text>From the points above we conclude that* Majority of the customers are from francebut customers from Germany have churned due to lack of resources or somewhat  reasons* The proportion of females customers churning is somewhat greater than the males* The customers from tenure 1 and 9 have churned the most and also have a greater churning rate* Most of the customers having number of products 1 and 2 have churned the most* The customers who have churned are having more CrCard* Surprisingly the no of customers who have churned the most are inactive members and the proportion is also very high<jupyter_code>fig,ax=plt.subplots(2,2, figsize=(16,10))
sns.boxplot(x='Exited',y='CreditScore',data=df,ax=ax[0][0])
sns.boxplot(x='Exited',y='Age',data=df,ax=ax[0][1])
sns.boxplot(x='Exited',y='Balance',data=df,ax=ax[1][0])
sns.boxplot(x='Exited',y='EstimatedSalary',data=df,ax=ax[1][1])
plt.show()<jupyter_output><empty_output><jupyter_text>From the above points we conclude that * There is no significant difference in credit card score distribution between customers who have churned or not* People in the age limit of 38-50 have churned more with respect to 30-40 ie old customers are churning more* Bank is loosing customers with significant bank balance* Estimated Salary of Customers who have churned and not churned are almost the same<jupyter_code>plt.figure(figsize=(15,20))
corr =df.corr()
sns.heatmap(corr,linewidths=1,annot=True,fmt=".2f")
plt.show()<jupyter_output><empty_output><jupyter_text>There is no-colinearity in the dataset<jupyter_code># Droping useless columns
df.drop(columns=['RowNumber','CustomerId','Surname'],axis=1,inplace=True)
df.head()

df.Geography.value_counts()
#Coverting to categorical features
df['Geography']=df['Geography'].map({'France':0,'Germany':1,'Spain':2})
df['Gender']=df['Gender'].map({'Female':0,'Male':1})
df.head()
#Creating features and labels
from tensorflow.keras.utils import to_categorical
X=df.drop('Exited',axis=1)
Y=to_categorical(df.Exited)
#Splitting into train-test split
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.25)
#Scaling data
from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
X_train=sc.fit_transform(X_train)
X_test=sc.fit_transform(X_test)
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import BatchNormalization
#Initializing Ann
model=Sequential()
#First Input Layer and Hidden layer with 10 neurons
model.add(Dense(10,kernel_initializer='normal',activation='relu',input_shape=(10,)))
#Add Dropout
model.add(Dropout(rate=0.1))
model.add(BatchNormalization())
#Adding 3rd and Hidden layer
model.add(Dense(7,kernel_initializer='normal',activation='relu'))
model.add(Dropout(rate=0.1))
model.add(BatchNormalization())
#Adding output layer
model.add(Dense(2,kernel_initializer='normal',activation='sigmoid'))
model.summary()
#Compiling model
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

#Fitting model to the training set
model_history=model.fit(X_train,Y_train,validation_split=0.2,validation_data=(X_test,Y_test),epochs=100)<jupyter_output>Epoch 1/100
188/188 [==============================] - 1s 4ms/step - loss: 0.6301 - accuracy: 0.7207 - val_loss: 0.5760 - val_accuracy: 0.7933
Epoch 2/100
188/188 [==============================] - 1s 3ms/step - loss: 0.4815 - accuracy: 0.7993 - val_loss: 0.4608 - val_accuracy: 0.7933
Epoch 3/100
188/188 [==============================] - 1s 3ms/step - loss: 0.4169 - accuracy: 0.8222 - val_loss: 0.3925 - val_accuracy: 0.8453
Epoch 4/100
188/188 [==============================] - 1s 3ms/step - loss: 0.3918 - accuracy: 0.8350 - val_loss: 0.3803 - val_accuracy: 0.8440
Epoch 5/100
188/188 [==============================] - 1s 3ms/step - loss: 0.3835 - accuracy: 0.8392 - val_loss: 0.3678 - val_accuracy: 0.8553
Epoch 6/100
188/188 [==============================] - 1s 3ms/step - loss: 0.3839 - accuracy: 0.8418 - val_loss: 0.3670 - val_accuracy: 0.8527
Epoch 7/100
188/188 [==============================] - 1s 3ms/step - loss: 0.3820 - accuracy: 0.8405 - val_loss: 0.3696 - val_accuracy: 0.8520[...]<jupyter_text>Visualizing training data and validation loss<jupyter_code>plt.figure(figsize=(12,6))
train_loss=model_history.history['loss']
val_loss=model_history.history['val_loss']
epoch=range(1,101)
sns.lineplot(epoch,train_loss,label='Training Loss')
sns.lineplot(epoch,val_loss,label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Visualizing training and validation accuracy<jupyter_code>plt.figure(figsize=(15,6))
train_loss=model_history.history['accuracy']
test_loss=model_history.history['val_accuracy']
epoch=range(1,101)
sns.lineplot(epoch,train_loss,label='Training accuracy')
sns.lineplot(epoch,val_loss,label='Validation accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
acc = model.evaluate(X_test, Y_test)[1]

print(f'Accuracy of model is {acc}')<jupyter_output>79/79 [==============================] - 0s 801us/step - loss: 0.3582 - accuracy: 0.8544
Accuracy of model is 0.8543999791145325
{'fraction_non_alphanumeric': 0.17397157816005984, 'fraction_numerical': 0.05609573672400898, 'mean_word_length': 7.084643288996372, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '226358', 'license': 'Apache-2.0', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2335, 'n_words': 518, 'path': 'Customer Churn Modelling-checkpoint.ipynb', 'repo_name': 'varunsingh4/CustomerChurnModelling', 'size': '6685'}