-
Notifications
You must be signed in to change notification settings - Fork 182
/
NN_churn_prediction.py
63 lines (52 loc) · 1.97 KB
/
NN_churn_prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Importing the libraries"""
from keras.models import Sequential
from keras.layers import Dense
import pandas as pd
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
"""Loading the data"""
dataset = pd.read_csv("/home/amogh/Downloads/Churn_Modelling.csv")
# filtering features and labels
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values
"""Preprocessing the data"""
# encoding the Gender and Geography
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1]) # Column 4 [France, Germany, Spain] => [0, 1, 2]
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2]) # Column 5 [Male, Female] => [0, 1]
# splitting the data into training and testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)
# scaling features
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)
"""Building the neural network"""
# initializing the neural network
model = Sequential()
# input and first hidden layer
model.add(Dense(6, input_dim=10, activation='relu'))
# second hidden layer
model.add(Dense(6, activation='relu'))
# output layer - probability of churning
model.add(Dense(1, activation='sigmoid'))
# compiling the model
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
"""Running the model on the data"""
# fitting the model
model.fit(X_train, y_train, batch_size=10, epochs=100)
y_pred = model.predict(X_test)
y_pred = (y_pred > 0.5) # converting probabilities into binary
"""Evaluating the results"""
# generating the confusion matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
# determining the accuracy
accuracy = accuracy_score(y_test, y_pred)
print(accuracy)
# generating the classification report
cr = classification_report(y_test, y_pred)
print(cr)