-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final_Model_Random_Forest_Classifier.py
737 lines (542 loc) · 26.4 KB
/
Final_Model_Random_Forest_Classifier.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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#!/usr/bin/env python
# coding: utf-8
# # Random Forest Classifier
# The Random Forest Classifier (RFC) is a model made up of many decision trees. Rather than simply averaging the prediction of trees (which we could call a “forest”), the RFC uses two key concepts:
#
# (1) _Random sampling of training data when building trees._ When training, each tree learns from a random sample of the data points. The samples are drawn with replacement, known as bootstrapping, meaning that some samples will be used multiple times in a single tree. By training each tree on different samples, although each tree might have high variance with respect to a particular set of the training data, overall, the _entire forest_ will have lower variance but not at the cost of increasing bias. With the test set, predictions are made by averaging the predictions of each decision tree. This procedure of training each individual learner on different bootstrapped subsets of the data and then averaging the predictions is known as _bagging_.
#
# (2) _Random subsets of features when splitting nodes_. Only a subset of all the features are considered for splitting each node in each decision tree. Generally this is set to 'sqrt'(n_features) for classification. So, with the current data with 15 features, this would be set to just under 4.
#
# #### The Random Forest Classifier performed the best out of all the models which were run. Specifically, the RFC model utilizing under-sampling, due to imbalanced data.
#
# #### _Below are three models_:
#
# #### 1. Baseline
#
# #### 2. Under-Sampled
#
# #### 3. Under-Sampled with hyper-parameters tuned (FINAL)
# ## Install libraries
# In[1]:
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from matplotlib import pyplot
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report, confusion_matrix
# In[2]:
get_ipython().run_cell_magic('javascript', '', 'IPython.OutputArea.auto_scroll_threshold = 9999;')
# ## Read in the data
# In[3]:
pd.set_option('display.max_columns', 50)
LLCP2 = pd.read_csv(r'C:\Users\Nick\Desktop\GitProjects\LLCP_Project\LLCP2.csv')
LLCP2.describe()
# # 1. Baseline RFC Model:
# In[4]:
# Split data by features and target
X = LLCP2[['SEX','_AGE_G','_BMI5CAT','_EDUCAG','_INCOMG','_RFDRHV5','_PACAT1','_RFHLTH','_HCVU651','EMPLOY1',
'VETERAN3','MARITAL','ADDEPEV2','POORHLTH','PHYSHLTH']].values
y = LLCP2['MENTHLTH2'].values
# In[5]:
# Complete a 70/30 train-test-split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# describes info about train and test set
print("Number of rows/columns in X_test dataset: ", X_test.shape)
print("Number of rows/columns in y_test dataset: ", y_test.shape)
print("Number of rows/columns in X_train dataset: ", X_train.shape)
print("Number of rows/columns in y_train dataset: ", y_train.shape)
# In[6]:
# Default options for RFC
# class sklearn.ensemble.RandomForestClassifier(n_estimators=’warn’, criterion=’gini’, max_depth=None,
# min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=’auto’,
# max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, bootstrap=True,
# oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, class_weight=None
# ---BASELINE MODEL---
RFC_baseline = RandomForestClassifier()
RFC_baseline.fit(X_train, y_train)
y_pred = RFC_baseline.predict(X_test)
probs = RFC_baseline.predict_proba(X_test)
probs = probs[:,1]
# In[7]:
from sklearn.model_selection import cross_val_score
from sklearn import metrics
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.metrics import f1_score
from sklearn.metrics import auc
from sklearn.metrics import average_precision_score
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
############################################################################################################
# CLASSIFICATION REPORT ###
print('\n')
print("=== CLASSIFICATION REPORT ===")
print(classification_report(y_test, y_pred))
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("ROC AUC score:",metrics.roc_auc_score(y_test, probs))
confusion = confusion_matrix(y_test, y_pred)
# True Positives
TP = confusion[1, 1]
# True Negatives
TN = confusion[0, 0]
# False Positives
FP = confusion[0, 1]
# False Negatives
FN = confusion[1, 0]
print("Sensitivity:",TP / float(TP + FN))
print("Specificity:",TN / float(TN + FP))
print("Precision:",TP / float(TP + FP))
print('\n')
#####################################################################################################
# CONFUSION MATRIX
import itertools
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion Matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
Source: http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
# Plot the confusion matrix
plt.figure(figsize = (8, 8))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title, size = 25)
plt.colorbar(aspect=5)
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45, size = 12)
plt.yticks(tick_marks, classes, size = 12)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
# Labeling the plot
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt), fontsize = 20,
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.grid(False)
plt.tight_layout()
plt.ylabel('Actual label', size = 15)
plt.xlabel('Predicted label', size = 15)
plt.ylim([1.5, -.5])
cm = confusion_matrix(y_test, y_pred)
plot_confusion_matrix(cm, classes = ['Good Mental Health', 'Poor Mental Health'],
title = 'Confusion Matrix')
############################################################################################
# ROC CURVE PLOT
roc_auc = roc_auc_score(y_test, probs)
fpr, tpr, thresholds = roc_curve(y_test, probs)
plt.figure(figsize=(15,10))
plt.plot(fpr, tpr, label='Baseline Model (AUC = %0.3f)' % roc_auc, color='midnightblue')
#plt.fill_between(fpr, tpr, alpha=0.2, color='orange', **step_kwargs)
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate', size = 15)
plt.ylabel('True Positive Rate', size = 15)
plt.title('Receiver Operating Characteristic (ROC Curve)', size = 25)
plt.legend(loc="lower right")
plt.savefig('ROC')
plt.show()
###########################################################################################
# PRECISION-RECALL CURVE
from sklearn.metrics import precision_recall_curve
import matplotlib.pyplot as plt
from inspect import signature
from sklearn.metrics import average_precision_score
average_precision = average_precision_score(y_test, probs)
precision, recall, _ = precision_recall_curve(y_test, probs)
step_kwargs = ({'step': 'post'}
if 'step' in signature(plt.fill_between).parameters
else {})
plt.figure(figsize=(15,10))
plt.step(recall, precision, color='navy', alpha=0.7,
where='post', label='Baseline Model (AP = %0.3f)' % average_precision)
plt.fill_between(recall, precision, alpha=0.7, color='navy', **step_kwargs)
plt.xlabel('Recall', size=15)
plt.ylabel('Precision', size=15)
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall Curve', size=25)
plt.legend()
plt.show()
# # Dealing with imbalanced data:
#
# ## The data is imbalanced, indicted by two things:
#
# ### (1) MENTHLTH2 value counts show twice as many '0' than '1' rows
#
# ### (2) The accuracy scores in the baseline model for the '1' values are far lower than the '0', showing the model is biased. It's good at predicting 'Good Mental Health', but not 'Poor Mental Health'. I ran the Random Forest with class_weight option, giving 'Poor Mental Health' a higher weight. This improved scores a bit, but not by much.
#
# ### There are various re-sampling methods for dealing with unbalanced data. We will utilize the 'Under-Sampling' technique. This technique drops rows at random from the 'majority class', or the over-represented value. In this case, the '0' rows will be dropped at random until both value's are equal. This can lead to a loss of information, if there is not enough data. Since we have almost 500,000 total rows, this should not be a significant problem. I also tried adjusting class_weight in the baseline model and also used SMOTE-NC for Over-Sampling, however, the Under-Sampling provided the best results.
# In[8]:
# Check value counts for each class of the target
LLCP2.MENTHLTH2.value_counts().plot(kind='bar', title='Count (MENTHLTH2)');
LLCP2['MENTHLTH2'].value_counts()
# ### There are roughly twice as many value counts for the target in 'class 0' compared with 'class 1'...this is why the previous model was better at predicting 'class 0'. We'll now use under-sampling to balance the data.
# In[9]:
# Class count
count_class_0, count_class_1 = LLCP2.MENTHLTH2.value_counts()
# Divide by class
Good_MH = LLCP2[LLCP2['MENTHLTH2'] == 0]
Poor_MH = LLCP2[LLCP2['MENTHLTH2'] == 1]
# In[10]:
Good_MH_under = Good_MH.sample(count_class_1)
LLCP2_under = pd.concat([Good_MH_under, Poor_MH], axis=0)
print('Random under-sampling:')
print(LLCP2_under.MENTHLTH2.value_counts())
LLCP2_under.MENTHLTH2.value_counts().plot(kind='bar', title='Count (MENTHLTH2)');
# ### You can see above that we now have an equal amount of observations for both classes of the target MENTHLTH2. We did lose a lot of information using this method, however, we still have a pretty large dataset to work with.
# # 2.0 Under-Sampled Model
# ## Let's re-run the same model now, using the under-sampled data
# In[11]:
X_under = LLCP2_under[['SEX','_AGE_G','_BMI5CAT','_EDUCAG','_INCOMG','_RFDRHV5','_PACAT1','_RFHLTH','_HCVU651','EMPLOY1',
'VETERAN3','MARITAL','ADDEPEV2','POORHLTH','PHYSHLTH']].values
y_under = LLCP2_under['MENTHLTH2'].values
# In[12]:
# 70/30 train-test split
X_train_under, X_test_under, y_train_under, y_test_under = train_test_split(X_under, y_under, test_size=0.3, random_state=0)
# describes info about train and test set
print("Number of rows/columns in X_test_under dataset: ", X_test_under.shape)
print("Number of rows/columns in y_test_under dataset: ", y_test_under.shape)
print("Number of rows/columns in X_train_under dataset: ", X_train_under.shape)
print("Number of rows/columns in y_train_under dataset: ", y_train_under.shape)
# In[13]:
# Check the unique counts for the target classes
unique, counts = np.unique(y_train_under, return_counts=True)
dict(zip(unique, counts))
# In[14]:
# ---UNDER-SAMPLED MODEL---
RFC_under = RandomForestClassifier()
RFC_under.fit(X_train_under, y_train_under)
y_pred_under = RFC_under.predict(X_test_under) #yields predicted class 0/1
probs_under = RFC_under.predict_proba(X_test_under)
probs_under = probs_under[:,1] #yields probability of either class 0-1
# In[15]:
from sklearn.model_selection import cross_val_score
from sklearn import metrics
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.metrics import f1_score
from sklearn.metrics import auc
from sklearn.metrics import average_precision_score
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
############################################################################################################
# CLASSIFICATION REPORT ###
print('\n')
print("=== CLASSIFICATION REPORT ===")
print(classification_report(y_test_under, y_pred_under))
print("Accuracy:",metrics.accuracy_score(y_test_under, y_pred_under))
print("ROC AUC score:",metrics.roc_auc_score(y_test_under, probs_under))
confusion1 = confusion_matrix(y_test_under, y_pred_under)
# True Positives
TP_under = confusion1[1, 1]
# True Negatives
TN_under = confusion1[0, 0]
# False Positives
FP_under = confusion1[0, 1]
# False Negatives
FN_under = confusion1[1, 0]
print("Sensitivity:",TP_under / float(TP_under + FN_under))
print("Specificity:",TN_under / float(TN_under + FP_under))
print("Precision:",TP_under / float(TP_under + FP_under))
#####################################################################################################
# CONFUSION MATRIX
print('\n')
import itertools
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion Matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
Source: http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
"""
# Plot the confusion matrix
plt.figure(figsize = (8, 8))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title, size = 25)
plt.colorbar(aspect=5)
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45, size = 12)
plt.yticks(tick_marks, classes, size = 12)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
# Labeling the plot
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt), fontsize = 20,
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.grid(False)
plt.tight_layout()
plt.ylabel('Actual label', size = 15)
plt.xlabel('Predicted label', size = 15)
plt.ylim([1.5, -.5])
cm = confusion_matrix(y_test_under, y_pred_under)
plot_confusion_matrix(cm, classes = ['Good Mental Health', 'Poor Mental Health'],
title = 'Confusion Matrix')
plt.show()
############################################################################################
# ROC CURVE PLOT
roc_auc_under = roc_auc_score(y_test_under, probs_under)
fpr_under, tpr_under, thresholds = roc_curve(y_test_under, probs_under)
plt.figure(figsize=(15,10))
plt.plot(fpr, tpr, label='Baseline Model (AUC = %0.3f)' % roc_auc, color='midnightblue')
#plt.fill_between(fpr, tpr, alpha=0.2, color='midnightblue', **step_kwargs)
plt.plot(fpr_under, tpr_under, label='Under-Sampled Model (AUC = %0.3f)' % roc_auc_under, color='royalblue')
#plt.fill_between(fpr_under, tpr_under, alpha=0.2, color='royalblue', **step_kwargs)
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate', size = 15)
plt.ylabel('True Positive Rate', size = 15)
plt.title('Receiver Operating Characteristic (ROC Curve)', size = 25)
plt.legend(loc="lower right")
plt.show()
###########################################################################################
# PRECISION-RECALL CURVE
from sklearn.metrics import precision_recall_curve
import matplotlib.pyplot as plt
from inspect import signature
from sklearn.metrics import average_precision_score
average_precision_under = average_precision_score(y_test_under, probs_under)
precision_under, recall_under, _ = precision_recall_curve(y_test_under, probs_under)
step_kwargs = ({'step': 'post'}
if 'step' in signature(plt.fill_between).parameters
else {})
plt.figure(figsize=(15,10))
plt.step(recall, precision, color='navy', alpha=0.7,
where='post', label='Baseline Model (AP = %0.3f)' % average_precision)
plt.fill_between(recall, precision, alpha=0.7, color='navy', **step_kwargs)
plt.step(recall_under, precision_under, color='royalblue', alpha=0.7,
where='post', label='Under-Sampled Model (AP = %0.3f)' % average_precision_under)
plt.fill_between(recall_under, precision_under, alpha=0.7, color='royalblue', **step_kwargs)
plt.xlabel('Recall', size=15)
plt.ylabel('Precision', size=15)
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall Curve', size=25)
plt.legend()
plt.show()
# ### For this model, the average accuracy score dropped, though the classes are much more balanced. The AUC is similar. This model is better at predicting both classes, which is important. Let's tune the hyper-parameters, to see if we can improve the model further.
# # 3. Under-Sampled Model with Hyper-Parameters Tuned (FINAL)
# ### We'll use RandomizedSearchCV for tuning
# In[16]:
# 70/30 split
X_train_final, X_test_final, y_train_final, y_test_final = train_test_split(X_under, y_under, test_size=0.3, random_state=0)
# describes info about train and test set
print("Number of rows/columns in X_test_final dataset: ", X_test_final.shape)
print("Number of rows/columns in y_test_final dataset: ", y_test_final.shape)
print("Number of rows/columns in X_train_final dataset: ", X_train_final.shape)
print("Number of rows/columns in y_train_final dataset: ", y_train_final.shape)
# In[31]:
# Define parameters to be tuned
from sklearn.model_selection import RandomizedSearchCV
from pprint import pprint
# Number of trees in RFC
n_estimators = [int(x) for x in np.linspace(start = 100, stop = 1000, num = 10)]
# Number of features to consider at every split
max_features = ['sqrt',2,3,4,5,6,7,8,9,10]
# Maximum number of levels in tree
max_depth = [int(x) for x in np.linspace(10, 110, num = 11)]
max_depth.append(None)
# Minimum number of samples required to split a node
min_samples_split = [2,3,5,6,8,10]
# Minimum number of samples required at each leaf node
min_samples_leaf = [1,2,4,6,8,10,20,30,40,50,60]
# Method of selecting samples for training each tree
bootstrap = [True, False] # Create the random grid
random_grid = {'n_estimators': n_estimators,
'max_features': max_features,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'bootstrap': bootstrap}
pprint(random_grid)
# In[28]:
# Use the random grid to search for best hyperparameters
# First create the base model to tune
RFC_hyper = RandomForestClassifier()
# Random search of parameters, using 3 fold cross validation,
# Search across 50 different combinations, and use 3 available cores (n_jobs)
RFC_hyper = RandomizedSearchCV(estimator = RFC_hyper, param_distributions = random_grid, n_iter = 30, cv = 5,
verbose=1, random_state=42, n_jobs = 2)
# Fit the random search model
RFC_hyper.fit(X_train_final, y_train_final)
# In[27]:
# Print the best hyper-parameters
RFC_hyper.best_params_
# # FINAL MODEL
# ## Now, we'll adjust the hyper-parameters for the final model
# In[18]:
# ---FINAL MODEL with hyper-parameters tuned---
RFC_final = RandomForestClassifier(n_estimators=400, min_samples_split=2, min_samples_leaf=1, max_features=5,
max_depth=10,bootstrap=True, random_state=47, oob_score = True)
RFC_final.fit(X_train_final, y_train_final)
y_pred_final = RFC_final.predict(X_test_final) #yields predicted class 0/1
probs_final = RFC_final.predict_proba(X_test_final)
probs_final = probs_final[:,1] #yields probability of either class 0-1
# ## Print accuracy reports
# In[19]:
from sklearn.model_selection import cross_val_score
from sklearn import metrics
from sklearn.metrics import classification_report, confusion_matrix, recall_score, precision_score
from sklearn.metrics import f1_score
from sklearn.metrics import auc
from sklearn.metrics import average_precision_score
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
############################################################################################################
# CLASSIFICATION REPORT ###
print('\n')
print("=== CLASSIFICATION REPORT ===")
print(classification_report(y_test_final, y_pred_final))
print("Accuracy:",metrics.accuracy_score(y_test_final, y_pred_final))
print("OOB score:", RFC_final.oob_score_)
print("ROC AUC score:",metrics.roc_auc_score(y_test_final, probs_final))
confusion2 = confusion_matrix(y_test_final, y_pred_final)
# True Positives
TP_final = confusion2[1, 1]
# True Negatives
TN_final = confusion2[0, 0]
# False Positives
FP_final = confusion2[0, 1]
# False Negatives
FN_final = confusion2[1, 0]
print("Sensitivity:",TP_final / float(TP_final + FN_final))
print("Specificity:",TN_final / float(TN_final + FP_final))
print("Precision:",TP_final / float(TP_final + FP_final))
#####################################################################################################
# CONFUSION MATRIX
print('\n')
import itertools
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion Matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
Source: http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
"""
# Plot the confusion matrix
plt.figure(figsize = (8, 8))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title, size = 25)
plt.colorbar(aspect=5)
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45, size = 12)
plt.yticks(tick_marks, classes, size = 12)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
# Labeling the plot
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt), fontsize = 20,
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.grid(False)
plt.tight_layout()
plt.ylabel('Actual label', size = 15)
plt.xlabel('Predicted label', size = 15)
plt.ylim([1.5, -.5])
cm = confusion_matrix(y_test_final, y_pred_final)
plot_confusion_matrix(cm, classes = ['Good Mental Health', 'Poor Mental Health'],
title = 'Confusion Matrix')
plt.show()
############################################################################################
# ROC CURVE PLOT
roc_auc_final = roc_auc_score(y_test_final, probs_final)
fpr_final, tpr_final, thresholds = roc_curve(y_test_final, probs_final)
plt.figure(figsize=(15,10))
plt.plot(fpr, tpr, label='Baseline Model (AUC = %0.3f)' % roc_auc, color='midnightblue')
#plt.fill_between(fpr, tpr, alpha=0.2, color='orange', **step_kwargs)
plt.plot(fpr_under, tpr_under, label='Under-Sampled Model (AUC = %0.3f)' % roc_auc_under, color='royalblue')
#plt.fill_between(fpr_final, tpr_final, alpha=0.2, color='red', **step_kwargs)
plt.plot(fpr_final, tpr_final, label='Final Model (AUC = %0.3f)' % roc_auc_final, color='lightsteelblue')
#plt.fill_between(fpr_final, tpr_final, alpha=0.2, color='navy', **step_kwargs)
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate', size = 15)
plt.ylabel('True Positive Rate', size = 15)
plt.title('Receiver Operating Characteristic (ROC Curve)', size = 25)
plt.legend(loc="lower right")
plt.savefig('ROC')
plt.show()
###########################################################################################
# PRECISION-RECALL CURVE
from sklearn.metrics import precision_recall_curve
import matplotlib.pyplot as plt
from inspect import signature
from sklearn.metrics import average_precision_score
average_precision_final = average_precision_score(y_test_final, probs_final)
precision_final, recall_final, _ = precision_recall_curve(y_test_final, probs_final)
step_kwargs = ({'step': 'post'}
if 'step' in signature(plt.fill_between).parameters
else {})
plt.figure(figsize=(15,10))
plt.step(recall, precision, color='navy', alpha=1,
where='post', label='Baseline Model (AP = %0.3f)' % average_precision)
plt.fill_between(recall, precision, alpha=0.7, color='navy', **step_kwargs)
plt.step(recall_under, precision_under, color='royalblue', alpha=1,
where='post', label='Under-Sampled Model (AP = %0.3f)' % average_precision_under)
plt.fill_between(recall_under, precision_under, alpha=0.7, color='royalblue', **step_kwargs)
plt.step(recall_final, precision_final, color='lightsteelblue', alpha=1,
where='post', label='Final Model (AP = %0.3f)' % average_precision_final)
plt.fill_between(recall_final, precision_final, alpha=0.7, color='lightsteelblue', **step_kwargs)
plt.xlabel('Recall', size=15)
plt.ylabel('Precision', size=15)
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall Curve', size=25)
plt.legend()
plt.show()
# ### We can see that by tuning the hyper-parameters, we were able to increase average accuracy from 70 to 73% and increased AUC from .76 to .81.
# In[21]:
######################################################################################################
# RFC STATS
n_nodes = []
max_depths = []
for ind_tree in RFC_final.estimators_:
n_nodes.append(ind_tree.tree_.node_count)
max_depths.append(ind_tree.tree_.max_depth)
print('\n')
print("=== RFC STATS ===")
print(f'OOB score:', RFC_final.oob_score_)
print(f'Average number of nodes: {int(np.mean(n_nodes))}')
print(f'Average maximum depth: {int(np.mean(max_depths))}')
print(f'Average Sample Split: {int(np.mean(min_samples_split))}')
print(f'Average Samples Leaf: {int(np.mean(min_samples_leaf))}')
#####################################################################################################
# FEATURE IMPORTANCE GRAPH
feature_names = LLCP2.columns # to label features by name, not index
importances = RFC_final.feature_importances_
std = np.std([tree.feature_importances_ for tree in RFC_final.estimators_],
axis=0)
indices = np.argsort(importances)
# Plot the feature importances of the forest
plt.figure(figsize=(20,15))
plt.title("Feature Importances", Size=25)
plt.xlabel('Feature Importance Score', size=20)
plt.ylabel('Features', size=20)
plt.barh(range(X.shape[1]), importances[indices],
color="royalblue", xerr=std[indices], align="center")
# To define own labels
plt.yticks(range(X.shape[1]), [feature_names[i] for i in indices])
plt.ylim([-1, X.shape[1]])
plt.show()
# In[ ]: