Skip to main content

Bank customers survival or not classification | churn modeling



 # ANN using Stochastic Gradiant Descent

# %%  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')
x = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values

# %% Encoding categorical data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

le= LabelEncoder()

x[:, 1] = le.fit_transform(x[:, 1])
x[:, 2] = le.fit_transform(x[:, 2])

# %%  Creating Dataframe

df = pd.DataFrame(x,columns=dataset.columns[3:13])


# %%  ColumnTransformer , OneHotEncoding

from sklearn.compose import ColumnTransformer

transformer = ColumnTransformer(
    transformers=[
        ("OneHot",        # Just a name
         OneHotEncoder(), # The transformer class
         [1]              # The column(s) to be applied on.
         )
    ],
    remainder='passthrough' # do not apply anything to the remaining columns
)
x = transformer.fit_transform(x.tolist())
x = x.astype('float64')

# %%

# one_hot_encoder = OneHotEncoder(categorical_features=[1])
# x = one_hot_encoder.fit_transform(x).toarray()

x = x[:, 1:]

# %%  Splitting the dataset into the Training set and Test set

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

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

# %%  Importing the keras library and packages

import keras
from keras.models import Sequential # to initialize the neural network
from keras.layers import Dense #to create (hidden) layers

# %%  Initializing the ANN

classifier = Sequential()

# %%  Adding the input layer and the first hidden layer 

# (relu -> rectifier function, uniform -> distribute weights uniformly)

classifier.add(
    Dense(units=6, kernel_initializer='uniform', activation='relu', input_dim=11)
)

# %%  Adding the second hidden layer

classifier.add(Dense(units=6, kernel_initializer='uniform', activation='relu'))

# %% Adding the output layer

# If target variable has for an example 3 categories so in units 3 will be written
# and for the activation function use 'softmax'

classifier.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))

# %%  Compiling the ANN
# adam is the type of stochastic gradient descent
# loss = it is needed in Stochastic gradient to optimize the weight (Like MSE for Linear regression)
# for loss it will be same as logistic linear regression-> logarithmic loss function
# in logarithmic if target variable has two categories -> binary_crossentropy,
# If more than two -> categorical_crossentropy
# metrics-> to increase to accuracy we are adding accuracy only , it contains list.
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# %%  Fitting the ANN to the Training set
classifier.fit(x_train, y_train, batch_size=10, epochs=100)

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

# %%

cl = []
for i in y_pred:
    if i > 0.5 :
        i = 'Survived'
        cl.append(i)
    else:
        i = 'Not Survived'
        cl.append(i)


y_pred = np.array(cl)
y_pred = y_pred.reshape(-1,1)


# %%  Creating confusion matrix for the model
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test.reshape(-1,1), y_pred.reshape(-1,1))

# %% How to check terms in are come in comfusion matrix

true = []
false = []


# True
for i in y_pred:
    if i == True:
        true.append(i)
print('Prediction true: ', len(true))

# False
for i in y_pred:
    if i == False:
        false.append(i)
print('Prediction false: ', len(false))



# %%  Confision Matrix terms

True_Positive = cm[0,0]
True_Negative = cm[1,1]
False_Positive = cm[1,0]
False_Negative = cm[0,1]

Actual_Positive = True_Positive + False_Negative
Actual_Negative = True_Negative + False_Positive
Predicted_Positive = True_Positive + False_Positive
Predicted_Negative = True_Negative + False_Negative

Total_Population = True_Positive + True_Negative + False_Positive + False_Negative

# %%  Accuracy , Precision, Sensitvity, Specificity,

Accuracy = (True_Positive + True_Negative)*100 / Total_Population 
Accuracy
# %%  Precision (Reduce Type 1 error)

Precision = True_Positive*100 / Predicted_Positive
Precision
# %%

Negative_Rate = True_Negative*100 / Predicted_Negative
Negative_Rate
# %%  Sensitivity or Recall should be high (Reduce Type 2 error)

Recall = True_Positive*100 / Actual_Positive
Recall

# %%  Miss Rate should be low

Miss_Rate = False_Negative*100 / Actual_Positive
Miss_Rate


# %%  F1 Score

F1 = 2* Precision * Recall / (Precision + Recall)
F1

# %%

Comments

Popular posts from this blog

Python program to check if variable is of integer or string

Let's say if you want to input something of any datatype and want to get datatype only of it. So... Whenever you input some data whether it is string, integer or float like this: i = input('enter something here: ') means without int, str or float put before the syntax, that time your given input is always consider as string  or if you make it like this to add int before syntax; i = int(input('enter something here: '))  it always consider as integer and gives value error when you input string and same thing happens with float, So here is a program to solve this problem of input and get datatype var = input('input to check if variable is of integer or string: ') if var.isdigit() == False:     print(type(var)) else:     var1 = int(var)     print(type(var1))

Multiple classification from many of directories

  # %%  Import nessacary libraries import  numpy  as  np import  pandas  as  pd import  cv2 import  matplotlib.pyplot  as  plt import  os import  glob # %%   Keras Tensorflow libraries from  keras  import  layers from  keras.models  import  Model from  keras.optimizers  import  RMSprop , Adam , Nadam from  keras.preprocessing.image  import  ImageDataGenerator from  keras.layers  import  Input, BatchNormalization, Dense, Dropout, Conv2D, Flatten, GlobalAveragePooling2D, LeakyReLU from  keras.preprocessing.image  import  ImageDataGenerator, img_to_array, load_img # %%  Path path  =   r 'G:/Machine Learning/Project/Lego Mnifigures Classification/dataset' open_dir  =  os....

Digit Recognition

Here you can import digit dataset from scikit learn library which is in-built, So you don't need to download from other else Note: If you use visual code, I recommend you to turn your color theme to Monokai because it has a few extra and important keyword and attractive colors than other theme.   # %%  Import libraries import  numpy  as  np import  pandas  as  pd import  matplotlib.pyplot  as  plt import  random  # %%   Load dataset from  sklearn.datasets  import  load_digits dataset  =  load_digits() dataset.keys() output: d ict_keys(['data', 'target', 'target_names', 'images', 'DESCR']) You have to check all to direct print them Here DESCR is a description of dataset # %%   divide the dataset into input and target inputs  =  dataset.data target  =  dataset.target # %% ...