-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmilestone_code.py
1096 lines (849 loc) · 35.6 KB
/
milestone_code.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 13 06:44:36 2018
@author: jackieff, vkozlow, margalan
CS 229 Final Project code
This is for the final version, everything done previously for the milestone is
included in the "milestone_code.py" file.
We ran the code bit by bit, so many variable names are reused.
"""
#importing libraries
import os
import conda
import sys
conda_file_dir = conda.__file__
conda_dir = conda_file_dir.split('lib')[0]
proj_lib = os.path.join(os.path.join(conda_dir, 'share'), 'proj')
os.environ["PROJ_LIB"] = proj_lib
from mpl_toolkits.basemap import Basemap
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn import metrics
import itertools
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from matplotlib import rcParams
from imblearn.over_sampling import SMOTE
import sklearn.svm as svm
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import label_binarize
from sklearn.metrics import f1_score
from sklearn.neural_network import MLPClassifier
#from keras.models import Sequential
#from keras.layers import Dense
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import BaggingClassifier
from sklearn import model_selection
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.model_selection import KFold
#changing default parameters for plotting with matplotlib
rcParams['mathtext.fontset'] = 'stix'
rcParams['font.family'] = 'STIXGeneral'
rcParams.update({'font.size': 14})
def plot_confusion_matrix(cm, classes, title,
normalize=False,
cmap=plt.cm.Blues):
"""
Printing and plotting confusion matrix (cm) with optional
normalization
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.figure(figsize = (8,8))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
#plotting routine adapted from matplotlib example
df = pd.read_csv('training_set_values.csv') #reading training set features
"""
Training set features in df dataframe contain two out of the three outputs we
are predicting as output with our algorithms (water quality and water quantity).
For each of the three output predictions, we are using the other two as input
features (i.e., water quality and functionality of pump are both predictors for
water quantity, etc.)
"""
labels = pd.read_csv('training_set_labels.csv') #reading training set labels for functionality of pumps
df['labels']=labels['status_group'] #adding labels to the dataframe
#dropping variables that were deemed repetitive/not useful
df =df.drop(columns=['id','wpt_name', 'num_private', 'subvillage', 'region', 'district_code', 'lga', 'ward', 'recorded_by', 'scheme_name', 'extraction_type_group', 'payment', 'water_quality', 'quantity', 'source_type', 'waterpoint_type_group'])
#creating a new variable of "age" from the construction_year and date_recorded raw features
df['date_recorded'] = df['date_recorded'].astype(str).str[:-6] #getting the year of the date recorded
df['age']=df['date_recorded'].astype(int)-df['construction_year']
ind = df['age']>2000
df['age'][ind]=np.nan #if there is no construction date set the age to NaN
df = df.drop(columns = ['date_recorded', 'construction_year'])
#editing variable from hundreds of categories to funder = 1 if village/villagers, 0 otherwise
df['funder']=df['funder'].fillna('0')
df['funder'] = df['funder'].str.contains('|'.join(['Village','village'])).astype(int) #binary
#places with 0 longitude and 2e-08 latitude had missing data for those 2 features
ind = df['longitude']==0.0
df['longitude'][ind]=np.nan
ind = df['latitude']==-2e-08
df['latitude'][ind]=np.nan
#similar to funder, reducint from hundreds of categories to installer = 1 if village/villagers, 0 otherwise
df['installer']=df['installer'].fillna('0')
df['installer'] = df['installer'].str.contains('|'.join(['Village','village'])).astype(int) #binary
#from T/F to 0/1
ind = df.notna()['public_meeting']
df['public_meeting'][ind]=df['public_meeting'][ind].astype(int)
ind = df.notna()['permit']
df['permit'][ind]=df['permit'][ind].astype(int)
#Here begins One Hot Encoding (OHE) for all remaining features
new_basin = np.asarray(df['basin']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False, categories='auto')
a = enc.fit_transform(new_basin)
for num,i in enumerate(np.unique(df['basin'])):
df['basin_'+str(i)] = a[:,num]
df = df.drop(columns = 'basin')
new_region = np.asarray(df['region_code']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False, categories='auto')
a = enc.fit_transform(new_region)
for num,i in enumerate(np.unique(df['region_code'])):
df['region_'+str(i)] = a[:,num]
df = df.drop(columns = 'region_code')
df['scheme_management'] = df['scheme_management'].fillna('znan')
new_scheme = np.asarray(df['scheme_management']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False, categories='auto')
a = enc.fit_transform(new_scheme)
for num,i in enumerate(np.unique(df['scheme_management'])):
df['scheme_'+str(i)] = a[:,num]
df = df.drop(columns = ['scheme_znan', 'scheme_management'])
new_extype = np.asarray(df['extraction_type']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False, categories='auto')
a = enc.fit_transform(new_extype)
for num,i in enumerate(np.unique(df['extraction_type'])):
df['exttype_'+str(i)] = a[:,num]
df = df.drop(columns = 'extraction_type')
new_extypeclass = np.asarray(df['extraction_type_class']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False, categories='auto')
a = enc.fit_transform(new_extypeclass)
for num,i in enumerate(np.unique(df['extraction_type_class'])):
df['exttypeclass_'+str(i)] = a[:,num]
df = df.drop(columns = 'extraction_type_class')
new_mgmt = np.asarray(df['management']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False, categories='auto')
a = enc.fit_transform(new_mgmt)
for num,i in enumerate(np.unique(df['management'])):
df['mgmt_'+str(i)] = a[:,num]
df = df.drop(columns = 'management')
new_mgmtgp = np.asarray(df['management_group']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False,categories='auto')
a = enc.fit_transform(new_mgmtgp)
for num,i in enumerate(np.unique(df['management_group'])):
df['mgmtgp_'+str(i)] = a[:,num]
df = df.drop(columns = 'management_group')
new_paytype = np.asarray(df['payment_type']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False,categories='auto')
a = enc.fit_transform(new_paytype)
for num,i in enumerate(np.unique(df['payment_type'])):
df['paytype_'+str(i)] = a[:,num]
df = df.drop(columns = 'payment_type')
new_source = np.asarray(df['source']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False,categories='auto')
a = enc.fit_transform(new_source)
for num,i in enumerate(np.unique(df['source'])):
df['source_'+str(i)] = a[:,num]
df = df.drop(columns = 'source')
new_sourceclass = np.asarray(df['source_class']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False,categories='auto')
a = enc.fit_transform(new_sourceclass)
for num,i in enumerate(np.unique(df['source_class'])):
df['sourceclass_'+str(i)] = a[:,num]
df = df.drop(columns = 'source_class')
new_wpt = np.asarray(df['waterpoint_type']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False,categories='auto')
a = enc.fit_transform(new_wpt)
for num,i in enumerate(np.unique(df['waterpoint_type'])):
df['wpttype_'+str(i)] = a[:,num]
df = df.drop(columns = 'waterpoint_type')
## predicting functionality
df_functional = df.copy()
new_qual = np.asarray(df_functional['quality_group']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False,categories='auto')
a = enc.fit_transform(new_qual)
for num,i in enumerate(np.unique(df_functional['quality_group'])):
df_functional['quality_'+str(i)] = a[:,num]
df_functional = df_functional.drop(columns = 'quality_group')
new_quant = np.asarray(df_functional['quantity_group']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False,categories='auto')
a = enc.fit_transform(new_quant)
for num,i in enumerate(np.unique(df_functional['quantity_group'])):
df_functional['quantity_'+str(i)] = a[:,num]
df_functional = df_functional.drop(columns = 'quantity_group')
###### Functionality Prediction #######
#filling NANs with mean of variable
df_func = df_functional.fillna(df_functional.mean())
#separating into test (25%) and train (75%)
x_train, x_test, y_train, y_test = train_test_split(df_func.drop(columns='labels'), df_func['labels'], test_size=0.25, random_state=0)
#three classes of our output variable
classes=['Functional', 'Needs Repair', 'Non-Functional']
#Encoding the three output classes into integers
enc = preprocessing.LabelEncoder()
enc.fit(y_test)
test_labels = enc.transform(y_test)
# SMOTE resampling to deal with class imbalance
sm = SMOTE()
x_res,y_res = sm.fit_resample(x_train,y_train)
enc = preprocessing.LabelEncoder()
enc.fit(y_res)
train_labels = enc.transform(y_res)
## LR
logreg = LogisticRegression()
logreg.fit(x_res,train_labels)
y_pred=logreg.predict(x_test)
lr_acc = metrics.confusion_matrix(test_labels,y_pred)
plot_confusion_matrix(lr_acc, classes,title = 'Logistic Regression', normalize=True)
#optimizing hyperparameters
"""
We used GridSearchCV which performs k-fold cross validation (k=5 for us) and searches
a grid of specified parameters to find the best parameters.
"""
param_dist = {
'penalty': ['l1','l2'],
'C': [0.001,0.01,0.5,1]
}
lr_search= GridSearchCV(LogisticRegression(solver='liblinear', multi_class='auto'),param_dist,cv=5)
lr_search.fit(x_res,y_res)
lr_best = lr_search.best_estimator_ #best classifier found with GridSearchCV
lr_preds = lr_best.predict(x_test)
train_preds = lr_best.predict(x_res)
a = metrics.confusion_matrix(y_test,lr_preds)
plot_confusion_matrix(a, classes,title = 'Logistic Regression with L2 Penalty, C = 1'), normalize=True)
#calculating microaveraged F1 scores for train and test
f1_LR_train = metrics.f1_score(y_res,train_preds,average = 'micro')
f1_LR_test = metrics.f1_score(y_test,lr_preds,average = 'micro')
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
#getting the indices for the 5-fold cross validation of the test set (25% of original data)
kf = KFold(n_splits=5)
k_indices = []
for _, test_index in kf.split(x_test):
k_indices.append(test_index)
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = lr_best.predict(x_res)
test_preds = lr_best.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### RF optimizing hyperparameters
parameters = {'n_estimators':(100,70,50),'max_depth':(30,25,20,5)}
rf = RandomForestClassifier(criterion = 'entropy',min_samples_leaf = 5, min_samples_split = 10)
rf_cv = GridSearchCV(rf,parameters,cv=5,verbose=3)
rf_cv.fit(x_res,y_res)
rf_best = rf_cv.best_estimator_
#train and test predictions
train_rf_pred = rf_best.predict(x_res)
rf_pred = rf_best.predict(x_test)
#calculating microaveraged F1 scores for train and test
f1_rf_train = metrics.f1_score(y_res,train_rf_pred,average = 'micro')
f1_rf_test = metrics.f1_score(y_test,rf_pred,average = 'micro')
a =metrics.confusion_matrix(y_test,rf_pred)
plot_confusion_matrix(a, classes,title = 'Random Forest, 70 Estimators', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = rf_best.predict(x_res)
test_preds = rf_best.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### Bagging optimizing hyperparameters
bag = BaggingClassifier()
bag.fit(x_res,y_res)
bag_pred = bag.predict(x_test)
acc = metrics.confusion_matrix(y_test,bag_pred)
plot_confusion_matrix(acc, classes,
title='Bagging', normalize=True)
parameters = {'n_estimators':(100,70,50),'max_samples':(20,10,5),'max_features':(138,70,25)}
bag_cv = GridSearchCV(bag,parameters,cv=5,verbose=3)
bag_cv.fit(x_res,y_res)
bag_best = bag_cv.best_estimator_
#train and test predictions
train_bag_pred = bag_best.predict(x_res)
bag_pred = bag_best.predict(x_test)
#calculating microaveraged F1 scores for train and test
f1_bag_train = metrics.f1_score(y_res,train_bag_pred,average = 'micro')
f1_bag_test = metrics.f1_score(y_test,bag_pred,average = 'micro')
a =metrics.confusion_matrix(y_test,bag_pred)
plot_confusion_matrix(a, classes,title = 'Bagging', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = bag_best.predict(x_res)
test_preds = bag_best.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### Adaboost
ab = AdaBoostClassifier(n_estimators=35)
ab.fit(x_res,y_res)
ab_pred = ab.predict(x_test)
acc = metrics.confusion_matrix(y_test,ab_pred)
plot_confusion_matrix(acc, classes,
title='AdaBoost', normalize=True)
## Neural net optimizing hyperparameters
nnet = MLPClassifier(alpha=1e-5)
parameters ={
'learning_rate': ["constant", "invscaling", "adaptive"],
'hidden_layer_sizes': [(138,60,2), (100,10), (60,5,1), (75,30,5), (138)],
'activation': ["logistic", "tanh"]
}
nn_cv = GridSearchCV(nnet,parameters,cv=5,verbose=3)
nn_cv.fit(x_res,y_res)
nn_best = nn_cv.best_estimator_
#train and test predictions
train_nn_pred = nn_best.predict(x_res)
nn_pred = nn_best.predict(x_test)
a =metrics.confusion_matrix(y_test,nn_pred)
plot_confusion_matrix(a, classes,title = 'Neural Network', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = nn_best.predict(x_res)
test_preds = nn_best.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### SVM hyperparameters
svm = SVC(gamma='scale')
parameters ={
'C': (0.01,0.1,1),
'kernel': ("rbf", "sigmoid","poly")
}
svm_cv = GridSearchCV(svm,parameters,cv=5,verbose=3)
svm_cv.fit(x_res,y_res)
svm_best = svm_cv.best_estimator_
#train and test predictions
train_svm_pred = svm_best.predict(x_res)
svm_pred = svm_best.predict(x_test)
a =metrics.confusion_matrix(y_test,svm_pred)
plot_confusion_matrix(a, classes,title = 'SVM', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = svm_best.predict(x_res)
test_preds = svm_best.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### combining LR, NN, RF with Ensemble - Voting Classifier
vote_func = VotingClassifier(estimators=[('lr', lr_best), ('rf', rf_best), ('nn', nn_best)])
vote_func.fit(x_res,y_res)
vote_preds_f = vote_func.predict(x_test) #test predictions
vote_preds_t = vote_func.predict(x_res) #train predictions
a = metrics.confusion_matrix(y_test,vote_preds_f)
plot_confusion_matrix(a, classes,title = 'Voting Classifier', normalize=True)
############# Water Quantity #############
df_quant = df.copy()
#OHE process for water quality and functionality - used as predictors for water
# quantity
new_qual = np.asarray(df_quant['quality_group']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False,categories='auto')
a = enc.fit_transform(new_qual)
for num,i in enumerate(np.unique(df_quant['quality_group'])):
df_quant['quality_'+str(i)] = a[:,num]
df_quant = df_quant.drop(columns = 'quality_group')
new_func = np.asarray(df_quant['labels']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False,categories='auto')
a = enc.fit_transform(new_func)
for num,i in enumerate(np.unique(df_quant['labels'])):
df_quant[str(i)] = a[:,num]
df_quant = df_quant.drop(columns = 'labels')
#filling NANs with mean of variable
df_quant = df_quant.fillna(df_quant.mean())
#separating into test (25%) and train (75%)
x_train, x_test, y_train, y_test = train_test_split(df_quant.drop(columns='quantity_group'), df_quant['quantity_group'], test_size=0.25)
#five classes of our output variable
classes_quant = ['Dry', 'Enough', 'Insufficient', 'Seasonal','Unknown']
#Encoding the five output classes into integers
enc = preprocessing.LabelEncoder()
enc.fit(y_train)
train_labels = enc.transform(y_train)
# SMOTE resampling to deal with class imbalance
sm = SMOTE()
x_res,y_res = sm.fit_resample(x_train,y_train)
enc = preprocessing.LabelEncoder()
enc.fit(y_res)
train_labels = enc.transform(y_res)
## LR
logreg = LogisticRegression()
logreg.fit(x_res,train_labels)
y_pred=logreg.predict(x_test)
lr_acc = metrics.confusion_matrix(test_labels,y_pred)
plot_confusion_matrix(lr_acc, classes_quant,title = 'Logistic Regression', normalize=True)
#optimizing hyperparameters
"""
We used GridSearchCV which performs k-fold cross validation (k=5 for us) and searches
a grid of specified parameters to find the best parameters.
"""
param_dist = {
'penalty': ['l1','l2'],
'C': [0.001,0.01,0.5,1]
}
lr_search= GridSearchCV(LogisticRegression(solver='liblinear', multi_class='auto'),param_dist,cv=5)
lr_search.fit(x_res,y_res)
lr_best_qt = lr_search.best_estimator_ #best classifier found with GridSearchCV
lr_preds = lr_best_qt.predict(x_test)
train_preds = lr_best_qt.predict(x_res)
a = metrics.confusion_matrix(y_test,lr_preds)
plot_confusion_matrix(a, classes_quant,title = 'Logistic Regression with L2 Penalty, C = 1'), normalize=True)
#calculating microaveraged F1 scores for train and test
f1_LR_train = metrics.f1_score(y_res,train_preds,average = 'micro')
f1_LR_test = metrics.f1_score(y_test,lr_preds,average = 'micro')
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
#getting the indices for the 5-fold cross validation of the test set (25% of original data)
kf = KFold(n_splits=5)
k_indices = []
for _, test_index in kf.split(x_test):
k_indices.append(test_index)
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = lr_best_qt.predict(x_res)
test_preds = lr_best_qt.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### RF optimizing hyperparameters
parameters = {'n_estimators':(100,75,45,20),'max_depth':(45,30,25,20)}
rf = RandomForestClassifier(criterion = 'entropy',min_samples_leaf = 5, min_samples_split = 10)
rf_cv = GridSearchCV(rf,parameters,cv=5,verbose=3)
rf_cv.fit(x_res,y_res)
rf_best_qt = rf_cv.best_estimator_
#train and test predictions
train_rf_pred = rf_best_qt.predict(x_res)
rf_pred = rf_best_qt.predict(x_test)
#calculating microaveraged F1 scores for train and test
f1_rf_train = metrics.f1_score(y_res,train_rf_pred,average = 'micro')
f1_rf_test = metrics.f1_score(y_test,rf_pred,average = 'micro')
a =metrics.confusion_matrix(y_test,rf_pred)
plot_confusion_matrix(a, classes_quant,title = 'Random Forest, 45 Estimators', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = rf_best_qt.predict(x_res)
test_preds = rf_best_qt.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### Bagging optimizing hyperparameters
bag = BaggingClassifier()
bag.fit(x_res,y_res)
bag_pred = bag.predict(x_test)
acc = metrics.confusion_matrix(y_test,bag_pred)
plot_confusion_matrix(acc, classes_quant,
title='Bagging', normalize=True)
parameters = {'n_estimators':(100,75,45,20),'max_samples':(45,20,10,5),'max_features':(138,70,25)}
bag_cv = GridSearchCV(bag,parameters,cv=5,verbose=3)
bag_cv.fit(x_res,y_res)
bag_best_qt = bag_cv.best_estimator_
#train and test predictions
train_bag_pred = bag_best_qt.predict(x_res)
bag_pred = bag_best_qt.predict(x_test)
#calculating microaveraged F1 scores for train and test
f1_bag_train = metrics.f1_score(y_res,train_bag_pred,average = 'micro')
f1_bag_test = metrics.f1_score(y_test,bag_pred,average = 'micro')
a =metrics.confusion_matrix(y_test,bag_pred)
plot_confusion_matrix(a, classes_quant,title = 'Bagging', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = bag_best_qt.predict(x_res)
test_preds = bag_best_qt.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### Adaboost
ab = AdaBoostClassifier(n_estimators=35)
ab.fit(x_res,y_res)
ab_pred = ab.predict(x_test)
acc = metrics.confusion_matrix(y_test,ab_pred)
plot_confusion_matrix(acc, classes,
title='AdaBoost', normalize=True)
## Neural net optimizing hyperparameters
nnet = MLPClassifier(alpha=1e-5)
parameters ={
'learning_rate': ["constant", "invscaling", "adaptive"],
'hidden_layer_sizes': [(137,50,2), (80,10), (60,15), (75,30,5), (137)],
'activation': ["logistic", "tanh"]
}
nn_cv = GridSearchCV(nnet,parameters,cv=5,verbose=3)
nn_cv.fit(x_res,y_res)
nn_best_qt = nn_cv.best_estimator_
#train and test predictions
train_nn_pred = nn_best_qt.predict(x_res)
nn_pred = nn_best_qt.predict(x_test)
a =metrics.confusion_matrix(y_test,nn_pred)
plot_confusion_matrix(a, classes,title = 'Neural Network', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = nn_best_qt.predict(x_res)
test_preds = nn_best_qt.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### SVM hyperparameters
svm = SVC(gamma='scale')
parameters ={
'C': (0.01,0.1,1),
'kernel': ("rbf", "sigmoid","poly")
}
svm_cv = GridSearchCV(svm,parameters,cv=5,verbose=3)
svm_cv.fit(x_res,y_res)
svm_best_qt = svm_cv.best_estimator_
#train and test predictions
train_svm_pred = svm_best_qt.predict(x_res)
svm_pred = svm_best_qt.predict(x_test)
a =metrics.confusion_matrix(y_test,svm_pred)
plot_confusion_matrix(a, classes,title = 'SVM', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = svm_best_qt.predict(x_res)
test_preds = svm_best_qt.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### combining LR, NN, RF with Ensemble - Voting Classifier
vote_quant = VotingClassifier(estimators=[('lr', lr_best_qt), ('rf', rf_best_qt), ('nn', nn_best_qt)])
vote_quant.fit(x_res,y_res)
vote_preds_f = vote_quant.predict(x_test) #test predictions
vote_preds_t = vote_quant.predict(x_res) #train predictions
a = metrics.confusion_matrix(y_test,vote_preds_f)
plot_confusion_matrix(a, classes_qual,title = 'Voting Classifier', normalize=True)
############# Water Quality #############
df_qual = df.copy()
#OHE process for quantity and functionality - used as predictors for water quality
new_quant = np.asarray(df_qual['quantity_group']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False)
a = enc.fit_transform(new_quant)
for num,i in enumerate(np.unique(df_qual['quantity_group'])):
df_qual['quantity_'+str(i)] = a[:,num]
df_qual = df_qual.drop(columns = 'quantity_group')
new_func = np.asarray(df_qual['labels']).reshape(-1,1)
enc = preprocessing.OneHotEncoder(sparse= False)
a = enc.fit_transform(new_func)
for num,i in enumerate(np.unique(df_qual['labels'])):
df_qual[str(i)] = a[:,num]
df_qual = df_qual.drop(columns = 'labels')
#filling NANs with mean of variable
df_qual = df_qual.fillna(df_qual.mean())
#separating into test (25%) and train (75%)
x_train, x_test, y_train, y_test = train_test_split(df_qual.drop(columns='quality_group'), df_qual['quality_group'], test_size=0.25)
#six classes of our output variable
classes_qual = ['Colored', 'Fluoride', 'Good', 'Milky','Salty', 'Unknown']
#Encoding the output classes into integers
enc = preprocessing.LabelEncoder()
enc.fit(y_train)
train_labels = enc.transform(y_train)
# SMOTE resampling to deal with class imbalance
sm = SMOTE()
x_res,y_res = sm.fit_resample(x_train,y_train)
enc = preprocessing.LabelEncoder()
enc.fit(y_res)
train_labels = enc.transform(y_res)
## LR
logreg = LogisticRegression()
logreg.fit(x_res,train_labels)
y_pred=logreg.predict(x_test)
lr_acc = metrics.confusion_matrix(test_labels,y_pred)
plot_confusion_matrix(lr_acc, classes,title = 'Logistic Regression', normalize=True)
#optimizing hyperparameters
"""
We used GridSearchCV which performs k-fold cross validation (k=5 for us) and searches
a grid of specified parameters to find the best parameters.
"""
param_dist = {
'penalty': ['l1','l2'],
'C': [0.001,0.01,0.5,1]
}
lr_search= GridSearchCV(LogisticRegression(solver='liblinear', multi_class='auto'),param_dist,cv=5)
lr_search.fit(x_res,y_res)
lr_best_qt = lr_search.best_estimator_ #best classifier found with GridSearchCV
lr_preds = lr_best_qt.predict(x_test)
train_preds = lr_best_qt.predict(x_res)
a = metrics.confusion_matrix(y_test,lr_preds)
plot_confusion_matrix(a, classes,title = 'Logistic Regression with L2 Penalty, C = 1'), normalize=True)
#calculating microaveraged F1 scores for train and test
f1_LR_train = metrics.f1_score(y_res,train_preds,average = 'micro')
f1_LR_test = metrics.f1_score(y_test,lr_preds,average = 'micro')
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
#getting the indices for the 5-fold cross validation of the test set (25% of original data)
kf = KFold(n_splits=5)
k_indices = []
for _, test_index in kf.split(x_test):
k_indices.append(test_index)
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = lr_best_qt.predict(x_res)
test_preds = lr_best_qt.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### RF optimizing hyperparameters
parameters = {'n_estimators':(100,75,45,20),'max_depth':(45,30,25,20)}
rf = RandomForestClassifier(criterion = 'entropy',min_samples_leaf = 5, min_samples_split = 10)
rf_cv = GridSearchCV(rf,parameters,cv=5,verbose=3)
rf_cv.fit(x_res,y_res)
rf_best_qt = rf_cv.best_estimator_
#train and test predictions
train_rf_pred = rf_best_qt.predict(x_res)
rf_pred = rf_best_qt.predict(x_test)
#calculating microaveraged F1 scores for train and test
f1_rf_train = metrics.f1_score(y_res,train_rf_pred,average = 'micro')
f1_rf_test = metrics.f1_score(y_test,rf_pred,average = 'micro')
a =metrics.confusion_matrix(y_test,rf_pred)
plot_confusion_matrix(a, classes_qual,title = 'Random Forest, 45 Estimators', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = rf_best_qt.predict(x_res)
test_preds = rf_best_qt.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### Bagging optimizing hyperparameters
bag = BaggingClassifier()
bag.fit(x_res,y_res)
bag_pred = bag.predict(x_test)
acc = metrics.confusion_matrix(y_test,bag_pred)
plot_confusion_matrix(acc, classes,
title='Bagging', normalize=True)
parameters = {'n_estimators':(100,75,45,20),'max_samples':(45,20,10,5),'max_features':(138,70,25)}
bag_cv = GridSearchCV(bag,parameters,cv=5,verbose=3)
bag_cv.fit(x_res,y_res)
bag_best_qt = bag_cv.best_estimator_
#train and test predictions
train_bag_pred = bag_best_qt.predict(x_res)
bag_pred = bag_best_qt.predict(x_test)
#calculating microaveraged F1 scores for train and test
f1_bag_train = metrics.f1_score(y_res,train_bag_pred,average = 'micro')
f1_bag_test = metrics.f1_score(y_test,bag_pred,average = 'micro')
a =metrics.confusion_matrix(y_test,bag_pred)
plot_confusion_matrix(a, classes,title = 'Bagging', normalize=True)
#Performing 5-fold CV on test set:
f1_train = []
f1_test = []
mcc_train = []
mcc_test = []
for i in range(5):
inds = k_indices[i]
x_t,y_t = np.asarray(x_test)[inds],np.asarray(y_test)[inds]
train_preds = bag_best_qt.predict(x_res)
test_preds = bag_best_qt.predict(x_t)
f1_train.append(metrics.f1_score(y_res,train_preds,average = "micro"))
f1_test.append(metrics.f1_score(y_t,test_preds,average = "micro"))
mcc_train.append(metrics.matthews_corrcoef(y_res,train_preds)
mcc_test.append(metrics.matthews_corrcoef(y_t,test_preds)
print(i)
print(np.nanmean(f1_test))
print(np.nanmean(mcc_test))
### Adaboost
ab = AdaBoostClassifier(n_estimators=35)
ab.fit(x_res,y_res)
ab_pred = ab.predict(x_test)
acc = metrics.confusion_matrix(y_test,ab_pred)
plot_confusion_matrix(acc, classes,