-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
516 lines (345 loc) · 15.9 KB
/
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
SENG 474 Assignment 1
Benjamin Frizzell (V00932255)
"""
import sklearn as sk
import numpy as np
import pandas as pd
import re
import matplotlib.pyplot as plt
# function to compute k-fold cross validation
def k_cross_validation(df,model,k=5):
# array to hold validation error
valid_err = []
# separate data into k partitions
partitions = np.array_split(df,k)
# compute validation error
for i,df_valid in enumerate(partitions):
# training set = compliment of validation set
train_partitions = [x for j,x in enumerate(partitions) if j != i]
df_train = pd.concat(train_partitions)
# get features and labels for training,validation sets
X_train,Y_train = df_train.iloc[:, :-1],df_train.iloc[:,-1]
X_valid,Y_valid = df_valid.iloc[:, :-1],df_valid.iloc[:,-1]
# training model
trained_model = model.fit(X_train,Y_train)
# testing model on validation set
valid_err.append(1-trained_model.score(X_valid,Y_valid))
return np.mean(valid_err)
# %%
# set seed for reproducability
SEED = 10
# %%
# read in data
df_all = pd.read_csv('spambase/spambase.data',header = None)
names = open('spambase/spambase.names', 'r')
# getting feature names using regex (not necessary, purely for helping with visualization)
features = []
pat = re.compile('(.*):\s*continuous\.')
for line in names:
m = re.match(pat,line)
if m:
features.append(m.group(1))
# compiling into dataframe
features.append("label")
df_all.columns = features
# shuffle dataframe and split into training & testing set
from sklearn.model_selection import train_test_split
df_train,df_test = train_test_split(df_all,test_size = 0.2,random_state=SEED)
X_train,Y_train = df_train.iloc[:, :-1],df_train.iloc[:,-1]
X_test,Y_test = df_test.iloc[:, :-1],df_test.iloc[:,-1]
# %% DECISION TREE TESTING
from sklearn.tree import DecisionTreeClassifier
# Testing hyperparameters on three criterion
criterion = ["Gini","Entropy","Log Loss"]
print("Decision Tree Testing:")
# %% 1. Testing pre-pruning hyperparameter (minimum samples required at leaf)
# range of parameters
B = np.arange(1,100)
# storing training/testinig error in a dictionary organized by split criterion names
error = {ki:np.zeros([len(B),2]) for ki in criterion}
for i,Bi in enumerate(B):
# training three models of differing split criteria, varying min_samples_leaf
DT_gini = DecisionTreeClassifier(criterion='gini',min_samples_leaf = Bi,random_state = SEED).fit(X_train,Y_train)
DT_entropy = DecisionTreeClassifier(criterion='entropy',min_samples_leaf = Bi,random_state=SEED).fit(X_train,Y_train)
DT_logloss = DecisionTreeClassifier(criterion='log_loss',min_samples_leaf = Bi,random_state=SEED).fit(X_train,Y_train)
models = [DT_gini,DT_entropy,DT_logloss]
# evaluate training and testing accuracy for each model
for j,model in enumerate(models):
error[criterion[j]][i] = 1-model.score(X_train,Y_train),1-model.score(X_test,Y_test)
# %% Plotting Results
fig, axs = plt.subplots(ncols=3,figsize = (20,8))
plt.subplots_adjust(wspace=0.4, hspace=0.4)
opt_B = {} # storing optimal value of min_samples_leaf for each model
for ax,key in zip(axs,criterion):
err_train = error[key].T[0]
err_test = error[key].T[1]
ax.plot(B,err_train,'b',label="Training")
ax.plot(B,err_test,'r',label="Test")
ax.set_title(key)
ax.set_xlabel(r'Minimum Samples Per leaf ($\beta$)')
ax.set_ylabel("Error")
ax.set_xticks(np.arange(0,120,20))
ax.set_ylim(-0.01,0.12)
ax.grid()
opt_B[key] = B[err_test == min(err_test)][0]
plt.legend(fontsize = 'large')
plt.savefig("figures/DT_preprune.jpeg",dpi=200)
print("Optimal pre-pruning parameters:",opt_B)
# %% 2. Testing sample size
# Increasing sample size from 1/8 original size to originial training set in increments of 10
num_samples = np.arange(len(X_train)/8,len(X_train)+10,10,dtype=int)
# reset error dictionary
error = {ki:np.zeros([len(num_samples),2]) for ki in criterion}
for i,ni in enumerate(num_samples):
# train only on subset of specified size (no pruning)
DT_gini = DecisionTreeClassifier(criterion='gini',random_state=SEED).fit(X_train[:ni],Y_train[:ni])
DT_entropy = DecisionTreeClassifier(criterion='entropy',random_state=SEED).fit(X_train[:ni],Y_train[:ni])
DT_logloss = DecisionTreeClassifier(criterion='log_loss',random_state=SEED).fit(X_train[:ni],Y_train[:ni])
models = [DT_gini,DT_entropy,DT_logloss]
# evaluate training and testing accuracy
for j,model in enumerate(models):
error[criterion[j]][i] = 1-model.score(X_train[:ni],Y_train[:ni]),1-model.score(X_test,Y_test)
# %% Plotting results
fig, axs = plt.subplots(ncols=3,figsize = (20,8))
plt.subplots_adjust(wspace=0.4, hspace=0.4)
for ax,key in zip(axs,criterion):
err_train = error[key].T[0]
err_test = error[key].T[1]
ax.plot(num_samples,err_train,'b',label="Training")
ax.plot(num_samples,err_test,'r',label="Test")
ax.set_title(key)
ax.set_xlabel('Training Sample Size')
ax.set_ylabel("Error")
ax.set_ylim(-0.01,0.16)
ax.grid()
plt.legend(fontsize = 'large')
plt.savefig("figures/DT_samplesize.jpeg",dpi=200)
# %% Repeating above analysis with pre-pruning
# Idea: Want to show how pre-pruning effects overfitting the training data and the test error
# reset error
error = {ki:np.zeros([len(num_samples),2]) for ki in criterion}
for i,ni in enumerate(num_samples):
DT_gini = DecisionTreeClassifier(criterion='gini',min_samples_leaf=opt_B['Gini'],random_state=SEED).fit(X_train[:ni],Y_train[:ni])
DT_entropy = DecisionTreeClassifier(criterion='entropy',min_samples_leaf=opt_B['Entropy'],random_state=SEED).fit(X_train[:ni],Y_train[:ni])
DT_logloss = DecisionTreeClassifier(criterion='log_loss',min_samples_leaf=opt_B['Log Loss'],random_state=SEED).fit(X_train[:ni],Y_train[:ni])
models = [DT_gini,DT_entropy,DT_logloss]
# evaluate training and testing accuracy
for j,model in enumerate(models):
error[criterion[j]][i] = 1-model.score(X_train[:ni],Y_train[:ni]),1-model.score(X_test,Y_test)
# %% Plotting Results
fig, axs = plt.subplots(ncols=3,figsize = (20,8))
plt.subplots_adjust(wspace=0.4, hspace=0.4)
for ax,key in zip(axs,criterion):
err_train = error[key].T[0]
err_test = error[key].T[1]
ax.plot(num_samples,err_train,'b',label="Training")
ax.plot(num_samples,err_test,'r',label="Test")
ax.set_title(key)
ax.set_xlabel('Training Sample Size')
ax.set_ylabel("Error")
ax.set_ylim(-0.01,0.2)
ax.grid()
plt.legend(fontsize = 'large')
plt.savefig("figures/DT_samplesize_pruning.jpeg",dpi=200)
# %% 3. Testing cost-complexity pruning (effective alpha)
# Range of effective alphas
alphas = np.linspace(0,0.035,300)
error = {ki:np.zeros([len(alphas),2]) for ki in criterion}
for i,ai in enumerate(alphas):
DT_gini = DecisionTreeClassifier(criterion='gini',ccp_alpha=ai,random_state=SEED).fit(X_train,Y_train)
DT_entropy = DecisionTreeClassifier(criterion='entropy',ccp_alpha=ai,random_state=SEED).fit(X_train,Y_train)
DT_logloss = DecisionTreeClassifier(criterion='log_loss',ccp_alpha=ai,random_state=SEED).fit(X_train,Y_train)
models = [DT_gini,DT_entropy,DT_logloss]
# evaluate training and testing accuracy
for j,model in enumerate(models):
error[criterion[j]][i] = 1-model.score(X_train,Y_train),1-model.score(X_test,Y_test)
# %% Plotting Results
fig, axs = plt.subplots(ncols=3,figsize = (20,8))
plt.subplots_adjust(wspace=0.4, hspace=0.4)
#find optimal effective alpha for each criterion
opt_alpha = {}
for ax,key in zip(axs,criterion):
err_train = error[key].T[0]
err_test = error[key].T[1]
ax.plot(alphas,err_train,'b',label="Training")
ax.plot(alphas,err_test,'r',label="Test")
ax.set_title(key)
ax.set_xlabel(r'effective $\alpha$')
ax.set_ylabel("Error")
ax.set_ylim(-0.01,0.19)
ax.grid()
opt_alpha[key] = round(alphas[err_test == min(err_test)][0],7)
plt.legend(fontsize = 'large')
plt.savefig("figures/DT_ccpruning.jpeg",dpi=200)
print("Optimal Cost-Complexity Pruning Parameters:",opt_alpha)
# %% RANDOM FOREST TESTING
from sklearn.ensemble import RandomForestClassifier
print('\nRandom Forest Testing:')
# %% 1) Testing number of trees in forest
num_trees = np.arange(1,150)
# testing only with Gini split criterion as it appears to yield overall lowest error
error = np.zeros([len(num_trees),2])
for i,ni in enumerate(num_trees):
RF = RandomForestClassifier(n_estimators = ni,random_state = SEED).fit(X_train,Y_train)
error[i] = 1-RF.score(X_train,Y_train),1-RF.score(X_test,Y_test)
# %% Plotting Results
err_train = error.T[0]
err_test = error.T[1]
fig, ax = plt.subplots(figsize = (20,8))
ax.plot(num_trees,err_train,color = 'blue',label = "Training",zorder = -1)
ax.plot(num_trees,err_test,color = 'red',label = "Testing",zorder = -1)
ax.set_xlabel("Number of Trees")
ax.set_ylabel("Error")
ax.legend(fontsize='large')
ax.grid()
plt.title("Random Forest - Ensemble Size",fontsize = 'x-large')
plt.savefig("figures/RF_numtrees.jpeg",dpi=200)
# Finding smallest optimal number of trees
opt_ntrees = num_trees[err_test == min(err_test)][0]
print("Optimal Number of Trees:", opt_ntrees) # will compare this to value obtained from cross validation
# %% 2) Testing number of features
d = len(X_train.columns) # total number of features
dp = round(np.sqrt(d)) # expected optimal (rule of thumb) number of features
# test error over a range of feature sizes, and compare to rule of thumb
num_features = np.arange(1,d)
error = np.zeros([len(num_features),2])
for i,di in enumerate(num_features):
RF = RandomForestClassifier(max_features = di,random_state = SEED).fit(X_train,Y_train)
error[i] = 1-RF.score(X_train,Y_train),1-RF.score(X_test,Y_test)
# %% Plotting
err_train = error.T[0]
err_test = error.T[1]
fig, ax = plt.subplots(figsize = (20,8))
ax.plot(num_features,err_train,color = 'blue',label = "Training")
ax.plot(num_features,err_test,color = 'red',label = "Testing")
# Plotting special point for rule of thumb, d' = sqrt(d)
ax.scatter([dp,dp],[err_train[dp-1],err_test[dp-1]],s = 90,marker = '.',c='yellow',edgecolors = 'black',zorder = 3)
ax.set_xlabel("Number of Features $d\'$")
ax.set_ylabel("Error")
ax.legend(fontsize='large')
ax.grid()
plt.title("Random Forest - Number of Features",fontsize = 'x-large')
plt.savefig('figures/RF_numfeatures.jpeg',dpi = 200)
# Obtain optimal number of features
opt_features = num_features[err_test==min(err_test)][0]
print("Optimal Number of Features:",opt_features)
print("Expected:",dp)
# %% 3) Testing sample size
error = np.zeros([len(num_samples),2])
for i,ni in enumerate(num_samples):
RF = RandomForestClassifier(n_estimators=opt_ntrees,max_features=opt_features,random_state = SEED).fit(X_train[:ni],Y_train[:ni])
# using optimal number of estimators and features
error[i] = 1-RF.score(X_train[:ni],Y_train[:ni]),1-RF.score(X_test,Y_test)
# %%
err_train = error.T[0]
err_test = error.T[1]
fig, ax = plt.subplots(figsize = (20,8))
ax.plot(num_samples,err_train,color = 'blue',label = "Training")
ax.plot(num_samples,err_test,color = 'red',label = "Testing")
ax.set_xlabel("Training Sample Size $n\'$")
ax.set_ylabel("Error")
ax.legend(fontsize = 'large')
ax.grid()
plt.title("Random Forest - Sample Size",fontsize = 'x-large')
plt.savefig('figures/RF_samplesize.jpeg',dpi = 200)
# %% ADABOOST TESTING
from sklearn.ensemble import AdaBoostClassifier
print("\nAdaBoost Testing")
# %% 1) Testing ensemble size (number of stumps)
error = np.zeros([len(num_trees),2])
for i,ni in enumerate(num_trees):
AB = AdaBoostClassifier(n_estimators = ni,random_state = SEED).fit(X_train,Y_train)
error[i] = 1-AB.score(X_train,Y_train),1-AB.score(X_test,Y_test)
# %% Plotting
err_train = error.T[0]
err_test = error.T[1]
fig, ax = plt.subplots(figsize = (20,8))
ax.plot(num_trees,err_train,color = 'blue',label = "Training")
ax.plot(num_trees,err_test,color = 'red',label = "Testing")
ax.set_xlabel("Number of Estimators")
ax.set_ylabel("Error")
ax.legend(fontsize = 'large')
ax.grid()
plt.title("AdaBoost - Ensemble Size",fontsize = 'x-large')
plt.savefig("figures/AB_numestimators.jpeg",dpi=200)
opt_nstumps = num_trees[err_test == min(err_test)][0]
print("Optimal Ensemble Size (depth = 1):",opt_nstumps)
# %% 2) Testing max depth
depths = np.arange(1,31)
error = np.zeros([len(depths),2])
for i,di in enumerate(depths):
DT = DecisionTreeClassifier(max_depth=di)
AB = AdaBoostClassifier(estimator = DT,random_state = SEED).fit(X_train,Y_train)
error[i] = 1-AB.score(X_train,Y_train),1-AB.score(X_test,Y_test)
# %% Plotting
err_train = error.T[0]
err_test = error.T[1]
fig, ax = plt.subplots(figsize = (20,8))
ax.plot(depths,err_train,color = 'blue',label = "Training")
ax.plot(depths,err_test,color = 'red',label = "Testing")
ax.set_xlabel("Maximum Depth")
ax.set_ylabel("Error")
ax.set_xticks(depths)
ax.legend(fontsize = 'large')
ax.grid()
plt.title("AdaBoost - Max Depth")
plt.savefig("figures/AB_maxdepth.jpeg",dpi=200)
opt_maxdepth = depths[err_test == min(err_test)][0]
print("Optimal Maximum Depth:",opt_maxdepth)
# %% 3) Testing sample size
error = np.zeros([len(num_samples),2])
for i,ni in enumerate(num_samples):
# since we are using trees of depth 1, selecting optimal number of estimators from previous experiment
AB = AdaBoostClassifier(n_estimators = opt_nstumps,random_state = SEED).fit(X_train[:ni],Y_train[:ni])
error[i] = 1-AB.score(X_train[:ni],Y_train[:ni]),1-AB.score(X_test,Y_test)
# %% Plotting
err_train = error.T[0]
err_test = error.T[1]
fig, ax = plt.subplots(figsize = (20,8))
ax.plot(num_samples,err_train,color = 'blue',label = "Training")
ax.plot(num_samples,err_test,color = 'red',label = "Testing")
ax.set_xlabel("Sample Size")
ax.set_ylabel("Error")
ax.legend(fontsize = 'large')
ax.grid()
plt.title("AdaBoost - Sample Size")
plt.savefig("figures/AB_samplesize.jpeg",dpi=200)
# %% VALIDATION ERROR MODEL SELECTION
# Calculating validation error across varying ensemble size for Random Forest and AdaBoost
num_trees = np.arange(50,260,10) # range selected empirically over repeated trials
# storing validation error for each model
RF_err = []
AB_err = []
# Using optimal number of features rom previous analysis for Random Forest
# using stumps for AdaBoost
for ni in num_trees:
RF = RandomForestClassifier(n_estimators = ni,max_features=opt_features,random_state = SEED)
RF_err.append(k_cross_validation(df_train,RF,5)) # k = 5 for quicker computation
AB = AdaBoostClassifier(n_estimators = ni,random_state=SEED)
AB_err.append(k_cross_validation(df_train,AB,5))
# %% Plotting Test error & Validation Error
fig, ax = plt.subplots(figsize = (20,8))
ax.plot(num_trees,RF_err,color = 'purple',label = 'Random Forest')
ax.plot(num_trees,AB_err,color = 'green',label = 'AdaBoost')
ax.set_xlabel("Ensemble Size")
ax.set_ylabel("Validation Error")
ax.grid()
ax.legend(fontsize = 'large')
plt.title("Ensemble Model Selection - Cross Validation",fontsize = 'x-large')
plt.savefig("figures/crossvalidation.jpeg",dpi=200)
# optimal number of trees in random forest from cross validation
print("\nOptimal Ensemble Size with Cross Validation:")
opt_ntrees_xval = num_trees[RF_err == min(RF_err)][0]
print("Random Forest:",opt_ntrees_xval)
# optimal number of stumps
opt_nstumps_xval = num_trees[AB_err == min(AB_err)][0]
print("AdaBoost:",opt_nstumps_xval)
# %% Final model selection with test error
RF_opt = RandomForestClassifier(n_estimators=opt_ntrees_xval,max_features=opt_features,random_state=SEED).fit(X_train,Y_train)
AB_opt = AdaBoostClassifier(n_estimators=opt_nstumps_xval,random_state=SEED).fit(X_train,Y_train)
print("\nFinal Test Error of Optimized Ensemble Models:")
print("Random Forest: %.3f" % (1-RF_opt.score(X_test,Y_test)))
print("AdaBoost: %.3f" % (1-AB_opt.score(X_test,Y_test)))