-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelling
More file actions
509 lines (371 loc) · 16.1 KB
/
Copy pathModelling
File metadata and controls
509 lines (371 loc) · 16.1 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# Import necessary libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, roc_curve, auc
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
# Example dataset (replace with your actual dataset)
# churn = pd.read_csv('your_dataset.csv')
# Define features and target variable
churn_feature = churn.drop(['Exited','Complain'], axis=1)
churn_label = churn['Exited']
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(churn_feature, churn_label, test_size=0.2, random_state=42)
# Define classifiers
classifiers = [
DecisionTreeClassifier(),
RandomForestClassifier(),
GradientBoostingClassifier(),
LogisticRegression(max_iter=1000) # Ensure logistic regression converges
]
# Classifier names
classifier_names = ['Decision Tree', 'Random Forest', 'GBM', 'Logistic Regression']
# Initialize storage for metrics
results = []
# Fit, evaluate, and visualize classifier models with a for loop
for clf, name in zip(classifiers, classifier_names):
# Train the model
clf.fit(X_train, y_train)
# Predict the response for the test dataset
y_pred = clf.predict(X_test)
y_prob = clf.predict_proba(X_test)[:,1] # Get probability estimates for ROC/AUC
# Create a confusion matrix
cm = confusion_matrix(y_test, y_pred)
# Visualize the confusion matrix
plt.figure(figsize=(6,6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title(f'Confusion Matrix: {name}')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()
# Print evaluation metrics
print(f"Model: {name}")
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
print("Precision:", metrics.precision_score(y_test, y_pred))
print("Recall:", metrics.recall_score(y_test, y_pred))
print("F1 score:", metrics.f1_score(y_test, y_pred))
# Display feature importance if available
if hasattr(clf, 'feature_importances_'):
importances = clf.feature_importances_
indices = np.argsort(importances)
plt.figure(figsize=(12,8))
plt.title(f'Feature Importances: {name}')
plt.barh(range(len(indices)), importances[indices], align='center')
plt.yticks(range(len(indices)), [churn_feature.columns[i] for i in indices])
plt.xlabel('Relative Importance')
plt.show()
elif hasattr(clf, 'coef_'):
importances = np.abs(clf.coef_[0])
indices = np.argsort(importances)
plt.figure(figsize=(12,8))
plt.title(f'Feature Importances: {name}')
plt.barh(range(len(indices)), importances[indices], align='center')
plt.yticks(range(len(indices)), [churn_feature.columns[i] for i in indices])
plt.xlabel('Coefficient Magnitude')
plt.show()
# Calculate ROC/AUC
fpr, tpr, _ = roc_curve(y_test, y_prob)
roc_auc = auc(fpr, tpr)
# Plot ROC curve
plt.figure(figsize=(8,6))
plt.plot(fpr, tpr, lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title(f'Receiver Operating Characteristic: {name}')
plt.legend(loc="lower right")
plt.show()
# Evaluate and append results
results.append({
'Classifier': name,
'Precision': metrics.precision_score(y_test, y_pred),
'Recall': metrics.recall_score(y_test, y_pred),
'F1': metrics.f1_score(y_test, y_pred),
'AUC': roc_auc
})
# Create the comparison table
comparison_table = pd.DataFrame(results)
comparison_table = comparison_table.set_index('Classifier')
print(comparison_table)
------------------------
# Visualize ROC Curve for Logistic Regression (as an example)
log_reg = LogisticRegression(max_iter=1000)
log_reg.fit(X_train, y_train)
y_prob = log_reg.predict_proba(X_test)[:,1]
fpr, tpr, _ = roc_curve(y_test, y_prob)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(8,6))
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
----------
from sklearn.metrics import roc_curve, auc
# Predict probabilities
y_prob = log_reg.predict_proba(X_test)[:,1]
# Compute ROC curve and AUC
fpr, tpr, _ = roc_curve(y_test, y_prob)
roc_auc = auc(fpr, tpr)
# Plot ROC curve
plt.figure(figsize=(8,6))
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
# Display feature importance if available
if hasattr(clf, 'feature_importances_'):
importances = clf.feature_importances_
indices = np.argsort(importances)
plt.figure(figsize=(12,12))
plt.title(f'Feature Importances: {name}')
plt.barh(range(len(indices)), importances[indices], color='b', align='center')
plt.yticks(range(len(indices)), [churn_feature.columns[i] for i in indices])
plt.xlabel('Relative Importance')
plt.show()
elif hasattr(clf, 'coef_'):
importances = np.abs(clf.coef_[0])
indices = np.argsort(importances)
plt.figure(figsize=(12,12))
plt.title(f'Feature Importances: {name}')
plt.barh(range(len(indices)), importances[indices], color='b', align='center')
plt.yticks(range(len(indices)), [churn_feature.columns[i] for i in indices])
plt.xlabel('Coefficient Magnitude')
plt.show()
-------------------------------
# Define features and target variable
churn_feature = churn.drop(['Exited','Complain'], axis=1)
churn_label = churn['Exited']
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(churn_feature, churn_label, test_size=0.2, random_state=42)
# # Split the data into training and test sets
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Define classifiers
classifiers = [
DecisionTreeClassifier(),
RandomForestClassifier(),
GradientBoostingClassifier(),
LogisticRegression()
]
# Fit, evaluate and visualise classifier models with a for loop
classifier_names = ['Decision Tree', 'Random Forest', 'GBM', 'Logistic Regression']
# Initialize storage for metrics
results = []
for clf, name in zip(classifiers, classifier_names):
# Train the model
clf.fit(X_train, y_train)
# Predict the response for the test dataset
y_pred = clf.predict(X_test)
# Create a confusion matrix
cm = confusion_matrix(y_test, y_pred)
# Visualise the confusion matrix
plt.figure(figsize=(6,6))
sns.heatmap(cm, annot=True, fmt='d')
plt.title(f'Confusion Matrix: {name}')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()
# Print evaluation metrics
print(f"Model: {name}")
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
print("Precision:", metrics.precision_score(y_test, y_pred))
print("Recall:", metrics.recall_score(y_test, y_pred))
print("F1 score:", metrics.f1_score(y_test, y_pred))
# Display feature importance
importances = clf.feature_importances_
indices = np.argsort(importances)
plt.figure(figsize=(12,12))
plt.title(f'Feature Importances: {name}')
plt.barh(range(len(indices)), importances[indices], color='b', align='center')
plt.yticks(range(len(indices)), [churn_feature.columns[i] for i in indices])
plt.xlabel('Relative Importance')
plt.show()
# Evaluate and append results
results.append({
'Classifier': name,
'Precision': metrics.precision_score(y_test, y_pred),
'Recall': metrics.recall_score(y_test, y_pred),
'F1': metrics.f1_score(y_test, y_pred)
})
# Create the comparison table
comparison_table = pd.DataFrame(results)
comparison_table = comparison_table.set_index('Classifier')
print(comparison_table)
------------------------------------
# Hyperparameter Tuning
**Questions for Discussion:**
**Hyperparameter Tuning (HT)**:
* What are hyperparameters in machine learning models?
* Give one hyperparameter example for each of the ML models you've used so far.
* Linear Regression
* Decision Tree
* Random Forest
* GBM
* What does it mean to `tune` the hyperparameters and why do we do this?
* How do we implement HT using sklearn?
**Example answers:**
What are hyperparameters in machine learning models?
**Recipe Settings:** Think of a machine learning model as a recipe for making predictions about baking muffins. Hyperparameters are like the adjustable settings on the recipe: number of eggs, oven temperature, etc. Different settings lead to different outcomes.
**Not Learned By the Model:** The machine learning algorithm doesn't figure out the best hyperparameter settings by itself. That's our job as data scientists!
Give one hyperparameter example for each of the ML models you've used so far.
**Linear Regression:** One key hyperparameter is the type of **regularization** used. This helps prevent the model from getting too focused on small details in the training data, which can hurt its performance on new loan applications.
**Decision Tree:** A big one is **max_depth**. This controls how many questions the tree can ask about a customer before making a prediction. Deeper trees can learn more complex patterns, but risk becoming too specific to the training data.
**Random Forest:** **n_estimators** is important. This is the number of individual decision trees in your "forest." Usually, more trees are better, but they take longer to train!
**GBM:** **learning_rate** is crucial. This determines how much each new tree in the GBM focuses on correcting past mistakes. A smaller learning rate might need more trees to do well.
What does it mean to tune the hyperparameters and why do we do this?
**Finding the 'Sweet Spot':** Tuning is like systematically trying different recipe settings to find the combination that leads to the _tastiest_ muffin.
**Why This Matters:** The 'perfect' hyperparameter settings depend on your specific dataset. There's no one-size-fits-all recipe! Experimenting helps us pick the right settings for our particular baking project.
How do we implement HT using sklearn?
**Grid Search:** It's like testing a _grid_ of different settings automatically. We say, "Hey scikit-learn, try 5, 6, and 7 for 'max_depth', try 10 and 20 for 'n_estimators', etc."
**The Search Tool:** Scikit-learn has tools like `GridSearchCV` to run through all the combinations and tell us which ones worked best.
----------------
# Import necessary libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
# Read the data from CSV file
bankloan = pd.read_csv('bankloan.csv')
# Exclude 'ID' and 'ZIP.Code' columns
bankloan = bankloan.drop(['ID', 'ZIP.Code'], axis=1)
# Define features and target variable
X = bankloan.drop('Personal.Loan', axis=1)
y = bankloan['Personal.Loan']
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
----------
# Decision tree hyperparameter tuning
from sklearn.model_selection import GridSearchCV
import time
param_grid = {
'max_depth': np.arange(3, 15),
'min_samples_split': np.arange(2, 15),
'min_samples_leaf': np.arange(1, 15),
}
clf = DecisionTreeClassifier()
start_time = time.time()
grid_search = GridSearchCV(estimator = clf, param_grid = param_grid,
cv = 3, n_jobs = -1, verbose = 2)
grid_search.fit(X_train, y_train)
end_time = time.time()
elapsed_time = end_time - start_time
grid_search.best_params_
# Train the model with best parameters
best_grid = grid_search.best_estimator_
best_grid.fit(X_train, y_train)
# Visualise the decision tree
plt.figure(figsize=(15,7.5))
plot_tree(best_grid, filled=True, rounded=True,
feature_names=X.columns,
class_names=['No Loan','Loan'])
plt.show()
# Predict the response for the test dataset
y_pred = best_grid.predict(X_test)
# Create a confusion matrix
cm = confusion_matrix(y_test, y_pred)
# Print evaluation metrics
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
print("Precision:", metrics.precision_score(y_test, y_pred))
print("Recall:", metrics.recall_score(y_test, y_pred))
print("F1 score:", metrics.f1_score(y_test, y_pred))
print(f"Hyperparameter tuning took: {elapsed_time:.2f} seconds")
=-----------------------------------
# Random forest hyperparameter tuning
from sklearn.model_selection import GridSearchCV
import time
# Define the parameter grid
param_grid = {
'n_estimators': [50, 100, 200],
'max_depth': [None, 5, 10],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}
# Create a Random Forest Classifier with the parameter grid
rf_clf = RandomForestClassifier()
start_time = time.time()
# Instantiate the grid search model
grid_search = GridSearchCV(estimator=rf_clf, param_grid=param_grid, cv=3)
# Fit the grid search to the data
grid_search.fit(X_train, y_train)
end_time = time.time()
elapsed_time = end_time - start_time
# Get the best parameters
best_params_rf = grid_search.best_params_
print(f'Best parameters for Random Forest: {best_params_rf}')
# Train the model with best parameters
best_grid = grid_search.best_estimator_
best_grid.fit(X_train, y_train)
# Predict the response for the test dataset
y_pred = best_grid.predict(X_test)
# Create a confusion matrix
cm = confusion_matrix(y_test, y_pred)
# Print evaluation metrics
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
print("Precision:", metrics.precision_score(y_test, y_pred))
print("Recall:", metrics.recall_score(y_test, y_pred))
print("F1 score:", metrics.f1_score(y_test, y_pred))
print(f"Hyperparameter tuning took: {elapsed_time:.2f} seconds")
---------------------------
# GBM hyperparameter tuning
from sklearn.model_selection import GridSearchCV
import time
# Define the parameter grid
param_grid = {
'n_estimators': [50, 100, 200],
'learning_rate': [0.01, 0.1, 1],
'max_depth': [None, 5, 10],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}
# Create a GBM Classifier with the parameter grid
gbm_clf = GradientBoostingClassifier()
start_time = time.time()
# Instantiate the grid search model
grid_search = GridSearchCV(estimator=gbm_clf, param_grid=param_grid, cv=3)
# Fit the grid search to the data
grid_search.fit(X_train, y_train)
end_time = time.time()
elapsed_time = end_time - start_time
# Get the best parameters
best_params_gbm = grid_search.best_params_
print(f'Best parameters for GBM: {best_params_gbm}')
# Train the model with best parameters
best_grid = grid_search.best_estimator_
best_grid.fit(X_train, y_train)
# Predict the response for the test dataset
y_pred = best_grid.predict(X_test)
# Create a confusion matrix
cm = confusion_matrix(y_test, y_pred)
# Visualize the confusion matrix
sns.heatmap(cm, annot=True)
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()
# Print evaluation metrics
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
print("Precision:", metrics.precision_score(y_test, y_pred))
print("Recall:", metrics.recall_score(y_test, y_pred))
print("F1 score:", metrics.f1_score(y_test, y_pred))
print(f"Hyperparameter tuning took: {elapsed_time:.2f} seconds")