In [1]:
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/project.zip"
!unzip -o project.zip
!rm project.zip
Importing Libraries
In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
In [3]:
churn=pd.read_csv("Churn Model.csv")
In [4]:
churn
Out[4]:
In [5]:
churn.columns
Out[5]:
Checking Null values
In [6]:
churn.isnull().sum() #to check any null values
Out[6]:
Creating heat map
In [7]:
plt.figure(figsize=(14,14))
sns.heatmap(churn.corr(), annot=True, cmap="coolwarm")
Out[7]:
Replacing Categorical with Numerical
In [8]:
churn['Gender'].replace('Female',0,inplace=True)
churn['Gender'].replace('Male',1,inplace=True)
In [9]:
churn
Out[9]:
In [10]:
X = churn.iloc[:, 3:13].values
y = churn.iloc[:, 13].values
In [11]:
print(X)
In [12]:
print(y)
In [13]:
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
X[:, 2] = le.fit_transform(X[:, 2])
In [14]:
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [1])], remainder='passthrough')
X = np.array(ct.fit_transform(X))
In [15]:
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
In [16]:
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
In [17]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D,Input
from tensorflow.keras.layers import LeakyReLU,PReLU,ELU
In [18]:
model = Sequential()
In [19]:
model.add(Dense(units = 6, kernel_initializer = 'he_uniform', activation = 'relu'))
model.add(Dense(units = 12, kernel_initializer = 'he_uniform', activation = 'relu'))
model.add(Dense(units = 24, kernel_initializer = 'he_uniform', activation = 'relu'))
model.add(Dense(units = 36, kernel_initializer = 'he_uniform', activation = 'relu'))
model.add(Dense(units = 48, kernel_initializer = 'he_uniform', activation = 'relu'))
model.add(Dense(units = 54, kernel_initializer = 'he_uniform', activation = 'relu'))
model.add(Dense(units = 60, kernel_initializer = 'he_uniform', activation = 'relu'))
model.add(Dense(units = 1, kernel_initializer = 'glorot_uniform', activation = 'sigmoid'))
In [30]:
model.summary()
In [24]:
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
In [26]:
y
Out[26]:
In [27]:
X
Out[27]:
In [28]:
churn.info()
In [29]:
history= model.fit(X_train,y_train, epochs = 25,validation_data=(X_test, y_test))
In [31]:
model.evaluate(X_test,y_test)
Out[31]:
Graph Plotting
In [32]:
plt.plot(history.history['accuracy'])
plt.title('Train model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.show()
In [33]:
plt.plot(history.history['loss'])
plt.title('Train Model loss')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.show()
In [34]:
plt.plot(history.history['val_accuracy'])
plt.title(' Val model Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.show()
In [35]:
plt.plot(history.history['val_loss'])
plt.title(' Val Model Loss')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.show()
In [36]:
loss_train = history.history['loss']
loss_val = history.history['val_loss']
plt.plot(loss_train, 'g', label='Training loss')
plt.plot(loss_val, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
In [37]:
accuracy_train = history.history['accuracy']
accuracy_val = history.history['val_accuracy']
plt.plot(accuracy_train, 'g', label='Training accuracy')
plt.plot(accuracy_val, 'b', label='Validation accuracy')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
In [38]:
y_pred = model.predict(X_test)
y_pred = (y_pred>0.5) #Threshold value
In [39]:
from sklearn.metrics import confusion_matrix, accuracy_score
a=confusion_matrix(y_test, y_pred)
In [40]:
print(a)
In [41]:
m = confusion_matrix(y_test, y_pred)
a= pd.DataFrame(m, index=[0, 1], columns=[0, 1])
figure = plt.figure(figsize=(10, 10))
sns.heatmap(a, annot=True)
Out[41]:
Prediction
In [42]:
country = input("Please enter the country of the customer:")
credit = input("Please enter the credit score of the customer:")
gender= input("Please enter the gender of the customer:")
age= input("Please enter the age of the customer:")
tenure = input("Please enter the tenure of the customer:")
balance = input("Please enter the balance of the customer:")
products = input("Please enter the no of products of the customer:")
card = input("Please enter if customer has a credit card:")
member = input("Please enter if customer is a active member:")
salary = input("Please enter the salary of the customer:")
In [43]:
print(model.predict(sc.transform([[1, 0, 0, 699,0, 39, 1,0, 2, 0, 0, 93826.63]])) > 0.5)
print("------------This customer will not leave the bank------------")
Predicited which customer will leave bank
In [44]:
y_pred[0:100]
Out[44]:
We can clearly observe the first person to leave the bank will be the 6th person and so on.
In [45]:
model.save("churn_model_new.h5")
deepCC¶
In [46]:
!deepCC churn_model_new.h5
In [ ]: