-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandard_recurrent_neural_network_ipynb(complete).py
More file actions
422 lines (296 loc) · 11.5 KB
/
standard_recurrent_neural_network_ipynb(complete).py
File metadata and controls
422 lines (296 loc) · 11.5 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# -*- coding: utf-8 -*-
"""Standard_Recurrent_Neural_Network_ipynb(Complete).ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1iJoun31PYHiYMSEcmz2z0kUn9I8oUtKQ
###Recurrent Neural Network
### Importing the libraries
"""
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import tensorflow as tf
tf.__version__
"""## Data Preprocessing
### Importing the dataset
"""
dataset = pd.read_csv('/content/Churn_Modelling.csv')
dataset.sample(5)
dataset.shape
"""# Data Cleaning
"""
dataset.drop('RowNumber' ,axis='columns',inplace=True)
dataset
dataset.drop('CustomerId' ,axis='columns',inplace=True)
dataset.drop('Surname' ,axis='columns',inplace=True)
dataset.sample(5)
Ext_N0 = dataset[dataset.Exited==0].Tenure
Ext_YES = dataset[dataset.Exited==1].Tenure
plt.xlabel('Tenure')
plt.ylabel('No. of customers')
plt.title('Customer Prediction')
plt.hist([Ext_N0,Ext_YES], color=['green','red'],label=['Exited=No','Exited=Yes'])
plt.legend()
"""### Encoding categorical data
Label Encoding the "Gender" column
"""
dataset['Gender'].replace({'Female':1, 'Male':0},inplace=True)
dataset.sample(2)
"""###Split Data"""
#Dependent Var is Y
#Independent Var is X
X = dataset.iloc[:,:-1]
X
Y = dataset.iloc[:,-1]
Y
"""One Hot Encoding the "Geography" column"""
one_hot = pd.get_dummies(dataset['Geography'], drop_first=False).astype(int)
one_hot
dataset
dataset=dataset.drop('Geography',axis=1)
dataset
dataset = dataset.join(one_hot)
dataset
X
Y
"""###Spliting the dataset into features (X) and target (Y)"""
# Split the dataset into features (X) and target (Y)
X = dataset.drop('Exited', axis=1)
Y = dataset['Exited']
X
Y
"""###Handling Imbalanced Data"""
# Calculate the IQR for each feature
Q1 = dataset.quantile(0.25)
Q3 = dataset.quantile(0.75)
IQR = Q3 - Q1
# Define the lower and upper bounds for outlier detection
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
# Remove outliers
dataset = dataset[~((dataset < lower_bound) | (dataset > upper_bound))].dropna()
# Print the updated dataset
print(dataset)
print(dataset.head())
from imblearn.combine import SMOTEENN
# Apply SMOTEENN (SMOTE + Edited Nearest Neighbors) to balance the classes
smoteenn = SMOTEENN()
X_sm, Y_sm = smoteenn.fit_resample(X, Y)
"""###Checking for Outliers"""
from scipy import stats
# List of columns to check for outliers
columns = ['CreditScore', 'Age', 'Tenure', 'Balance', 'NumOfProducts', 'HasCrCard', 'IsActiveMember', 'EstimatedSalary']
# For each column, calculate the z-scores and print the number of outliers
for col in columns:
z_scores = np.abs(stats.zscore(dataset[col]))
outliers = z_scores > 3
print(f"{col} has {outliers.sum()} outliers")
"""### Removing Outliers from 'Age' Column Using IQR"""
Q1 = dataset['Age'].quantile(0.25)
Q3 = dataset['Age'].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
from scipy import stats
# List of columns to check for outliers
columns = ['CreditScore', 'Age', 'Tenure', 'Balance', 'NumOfProducts', 'HasCrCard', 'IsActiveMember', 'EstimatedSalary']
# For each column, calculate the z-scores and print the number of outliers
for col in columns:
z_scores = np.abs(stats.zscore(dataset[col]))
outliers = z_scores > 3
print(f"{col} has {outliers.sum()} outliers")
# Remove outliers
dataset = dataset[(dataset['Age'] >= lower_bound) & (dataset['Age'] <= upper_bound)]
# Print the updated dataset shape
print(f"Dataset shape after removing outliers: {dataset.shape}")
"""### Checking for Outliers in 'Age' Column Again"""
z_scores = np.abs(stats.zscore(dataset['Age']))
outliers = z_scores > 3
print(f"Age column has {outliers.sum()} outliers after removal")
"""### Splitting the dataset into Training and Testing set"""
from sklearn.model_selection import train_test_split
X_sm_train, X_sm_test, Y_sm_train, Y_sm_test = train_test_split(X_sm, Y_sm, test_size = 0.2, random_state = 0)
X_sm_train
X_sm_test
"""###Feature Scaling"""
from sklearn.preprocessing import RobustScaler
# Create a scaler object
scaler = RobustScaler()
# Fit and transform the data
X_train_scaled = scaler.fit_transform(X_sm_train)
X_test_scaled = scaler.transform(X_sm_test)
"""###Checking for data balance
"""
# Check Class Distribution before Balancing
class_counts = Y.value_counts()
print("Class distribution before balancing:")
print(class_counts)
# Check Class Distribution After Balancing
class_counts_sm = Y_sm.value_counts()
print("Class distribution after balancing:")
print(class_counts_sm)
ratio_sm = class_counts_sm[1] / class_counts_sm[0]
print(f"Ratio of class 1 to class 0 after balancing: {ratio_sm:.2f}")
"""### Using Grid Search algorithm to find the best parameters to build our RNN model"""
# Function to create model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
def create_model():
rnn_model = Sequential()
rnn_model.add(SimpleRNN(units=64, activation='relu', input_shape=(1, X_train_scaled.shape[1])))
rnn_model.add(Dropout(0.2))
rnn_model.add(Dense(units=32, activation='relu'))
rnn_model.add(Dropout(0.2))
rnn_model.add(Dense(units=1, activation='sigmoid'))
rnn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return rnn_model
from sklearn.base import BaseEstimator, ClassifierMixin
# Custom KerasClassifier class
class KerasClassifier(BaseEstimator, ClassifierMixin):
def __init__(self, create_model_fn, batch_size=32, epochs=100):
self.create_model_fn = create_model_fn
self.batch_size = batch_size
self.epochs = epochs
self.model = None
def fit(self, X_train_scaled, Y_sm_train):
self.model = self.create_model_fn()
self.model.fit(X_train_scaled, Y_sm_train, batch_size=self.batch_size, epochs=self.epochs)
return self
def predict(self, X_train_scaled):
return (self.model.predict(X_train_scaled) > 0.5).astype(int)
# Create a KerasClassifier
model = KerasClassifier(create_model)
from sklearn.model_selection import train_test_split, GridSearchCV
# Define the grid search parameters
param_grid = {
'batch_size': [6, 12, 32, 64],
'epochs': [50, 100, 150, 200]
}
# Perform grid search
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, scoring='accuracy', cv=3)
grid_result = grid_search.fit(X_train_scaled, Y_sm_train)
# Print the best parameters and score
print("Best parameters: ", grid_result.best_params_)
print("Best score: ", grid_result.best_score_)
# Reshape the data for RNN model
X_train_rnn = np.reshape(X_train_scaled, (X_train_scaled.shape[0], 1, X_train_scaled.shape[1]))
X_test_rnn = np.reshape(X_test_scaled, (X_test_scaled.shape[0], 1, X_test_scaled.shape[1]))
"""###Building the RNN model with best parameters"""
# Import the necessary module
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dropout, Dense
# Define the RNN model
rnn_model = Sequential()
rnn_model.add(SimpleRNN(units=64, activation='relu', input_shape=(1, X_train_scaled.shape[1])))
rnn_model.add(Dropout(0.2))
rnn_model.add(Dense(units=32, activation='relu'))
rnn_model.add(Dropout(0.2))
rnn_model.add(Dense(units=1, activation='sigmoid'))
# Compile the RNN model
rnn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the RNN model
rnn_history = rnn_model.fit(X_train_rnn, Y_sm_train, validation_data=(X_test_rnn, Y_sm_test), epochs=50, batch_size=32)
"""### Making predictions from the model"""
Y_pred = rnn_model.predict(X_test_rnn)
Y_pred = (Y_pred > 0.5)
print(np.concatenate((Y_pred.reshape(len(Y_pred),1), np.array(Y_sm_test).reshape(len(Y_sm_test),1)),1))
print(Y_pred)
"""##Predicting the result of a single observation
Using our ANN model to predict if the customer with the following informations will leave the bank:
1.Geography: France
2.Credit Score: 600
3.Gender: Male
4.Age: 40 years old
5.Tenure: 3 years
6.Balance: $ 60000
7.Number of Products: 2
8.Does this customer have a credit card? Yes
9.Is this customer an Active Member: Yes
10.Estimated Salary: $ 50000
"""
import numpy as np
import pandas as pd
# Assuming 'scaler' is your RobustScaler and 'features' is a list of feature names
features = [ 'CreditScore', 'Gender', 'Age', 'Tenure', 'Balance', 'NumOfProducts', 'HasCrCard', 'IsActiveMember', 'EstimatedSalary', 'France', 'Germany', 'Spain']
# Create a DataFrame with the correct feature names
X = pd.DataFrame([[600, 1, 40, 3, 60000, 2, 1, 1, 50000, 1, 0, 0]], columns=features)
# Use the scaler to transform the data
X_test_rnn = scaler.transform(X)
# Reshape the data to match the RNN model's input shape
X_test_rnn = np.reshape(X_test_rnn, (X_test_rnn.shape[0], 1, X_test_rnn.shape[1]))
# Use the RNN to predict the transformed data
print(rnn_model.predict(X_test_rnn) )
"""###Evaluating the model using various metrics"""
from sklearn.metrics import precision_score, recall_score, accuracy_score
# Calculate precision
precision = precision_score(Y_sm_test, Y_pred)
print("Precision:", precision)
# Calculate recall
recall = recall_score(Y_sm_test, Y_pred)
print("Recall:", recall)
# Calculate accuracy
accuracy = accuracy_score(Y_sm_test, Y_pred)
print("Accuracy:", accuracy)
from sklearn.metrics import f1_score
f1 = f1_score(Y_sm_test, Y_pred)
print("F1 Score:", f1)
"""### Making the Confusion Matrix and Heatmap"""
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
# Create confusion matrix
cm = confusion_matrix(Y_sm_test, Y_pred)
print(cm)
# Create heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.title('Confusion matrix')
plt.show()
"""###Heatmap for accuracy,precision, recall, f1 score"""
# Calculate confusion matrix
cm = confusion_matrix(Y_sm_test, Y_pred)
print(cm)
# Calculate metrics for each class
accuracy_0 = cm[0, 0] / (cm[0, 0] + cm[0, 1])
accuracy_1 = cm[1, 1] / (cm[1, 0] + cm[1, 1])
precision_0 = precision_score(Y_sm_test, Y_pred, pos_label=0)
precision_1 = precision_score(Y_sm_test, Y_pred, pos_label=1)
recall_0 = recall_score(Y_sm_test, Y_pred, pos_label=0)
recall_1 = recall_score(Y_sm_test, Y_pred, pos_label=1)
f1_0 = f1_score(Y_sm_test, Y_pred, pos_label=0)
f1_1 = f1_score(Y_sm_test, Y_pred, pos_label=1)
# Create a dictionary with the metrics
metrics = {
'Accuracy': [accuracy_0, accuracy_1],
'Precision': [precision_0, precision_1],
'Recall': [recall_0, recall_1],
'F1 Score': [f1_0, f1_1]
}
# Convert the dictionary to a DataFrame for easier plotting
metrics_df = pd.DataFrame(metrics, index=['Class 0', 'Class 1'])
# Set up the figure
plt.figure(figsize=(12, 6))
# Define a color palette
cmap = sns.diverging_palette(220, 20, as_cmap=True)
# Create the heatmap
sns.heatmap(metrics_df, annot=True, cmap=cmap, cbar=True, fmt='.2f', annot_kws={"size": 14}, linewidths=2, linecolor='white')
# Customize the aesthetics
plt.title('Model Evaluation Metrics Heatmap for Each Class', fontsize=18, fontweight='bold', color='darkblue', pad=20)
plt.xlabel('Metrics', fontsize=14, labelpad=10)
plt.ylabel('Class', fontsize=14, labelpad=10)
plt.xticks(fontsize=12, rotation=0)
plt.yticks(fontsize=12, rotation=0)
# Show the heatmap
plt.tight_layout()
plt.show()
"""###Loss vs Accuracy"""
# Plotting Loss and accuracy
plt.plot(rnn_history.history['loss'], label='Loss')
plt.plot(rnn_history.history['accuracy'], label='Accuracy')
plt.title('Loss and Accuracy')
plt.ylabel('Loss')
plt.xlabel('Accuracy')
plt.legend()
plt.show()