-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestlearner.py
330 lines (292 loc) · 22.3 KB
/
testlearner.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
"""
Test a learner. (c) 2015 Tucker Balch
Copyright 2018, Georgia Institute of Technology (Georgia Tech)
Atlanta, Georgia 30332
All Rights Reserved
Template code for CS 4646/7646
Georgia Tech asserts copyright ownership of this template and all derivative
works, including solutions to the projects assigned in this course. Students
and other users of this template code are advised not to share it with others
or to make it available on publicly viewable websites including repositories
such as github and gitlab. This copyright statement should not be removed
or edited.
We do grant permission to share solutions privately with non-students such
as potential employers. However, sharing with other current or future
students of CS 7646 is prohibited and subject to being investigated as a
GT honor code violation.
-----do not edit anything above this line---
"""
import numpy as np
import math
import LinRegLearner as lrl
import DTLearner as dt
import RTLearner as rt
import BagLearner as bg
import InsaneLearner as it
import sys
import matplotlib.pyplot as plt
import time as ti
if __name__=="__main__":
if len(sys.argv) != 2:
print("Usage: python testlearner.py <filename>")
sys.exit(1)
inf = open(sys.argv[1])
data = np.array([list(map(str,s.strip().split(','))) for s in inf.readlines()])
#istanbul remove dates and header column
if(sys.argv[1] == "Data/Istanbul.csv"):
data = data[1:, 1:]
data = data.astype('float')
# compute how much of the data is training and testing
train_rows = int(0.6* data.shape[0])
test_rows = data.shape[0] - train_rows
# separate out training and testing data
trainX = data[:train_rows,0:-1]
trainY = data[:train_rows,-1]
testX = data[train_rows:,0:-1]
testY = data[train_rows:,-1]
print(f"TrainX shape: {trainX.shape}")
print(f"TrainY shape: {trainY.shape}")
print(f"TextX shape: {testX.shape}")
print(f"TextY shape: {testY.shape}")
# create a learner and train it
# learner = lrl.LinRegLearner(verbose = True) # create a LinRegLearner
# learner.addEvidence(trainX, trainY) # train it
# print(learner.author())
#-----------------------------------------------------------------------------------------------
# Create a DTlearner and train it
# -----------------------------------------------------------------------------------------------
learner = dt.DTLearner(leaf_size=1, verbose=False) # constructor
learner.addEvidence(trainX, trainY) # training step
# predY = learner.query(testX) # query
print("--------DT LEARNER--------")
# evaluate in sample (USING THE TRAINING DATA)
predY = learner.query(trainX) # get the predictions
rmse = math.sqrt(((trainY - predY) ** 2).sum()/trainY.shape[0])
print("In sample results")
print(f"RMSE: {rmse}")
c = np.corrcoef(predY, y=trainY)
print(f"corr: {c[0,1]}")
# evaluate out of sample (USING THE TEST DATA)
predY = learner.query(testX) # get the predictions
rmse = math.sqrt(((testY - predY) ** 2).sum()/testY.shape[0])
print()
print("Out of sample results")
print(f"RMSE: {rmse}")
c = np.corrcoef(predY, y=testY)
print(f"corr: {c[0,1]}")
# -----------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------
# Create a RTlearner and train it
# -----------------------------------------------------------------------------------------------
learner = rt.RTLearner(leaf_size=1, verbose=False) # constructor
learner.addEvidence(trainX, trainY) # training step
# predY = learner.query(testX) # query
print("--------RT LEARNER--------")
# evaluate in sample (USING THE TRAINING DATA)
predY = learner.query(trainX) # get the predictions
rmse = math.sqrt(((trainY - predY) ** 2).sum()/trainY.shape[0])
print("In sample results")
print(f"RMSE: {rmse}")
c = np.corrcoef(predY, y=trainY)
print(f"corr: {c[0,1]}")
# evaluate out of sample (USING THE TEST DATA)
predY = learner.query(testX) # get the predictions
rmse = math.sqrt(((testY - predY) ** 2).sum()/testY.shape[0])
print()
print("Out of sample results")
print(f"RMSE: {rmse}")
c = np.corrcoef(predY, y=testY)
print(f"corr: {c[0,1]}")
# -----------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------
# create a Bag Learner and train it
# -----------------------------------------------------------------------------------------------
learner = bg.BagLearner(learner=dt.DTLearner, kwargs={"leaf_size":1}, bags=20, boost=False, verbose=False)
learner.addEvidence(trainX, trainY)
# Y = learner.query(testX)
print("--------BAG LEARNER--------")
# evaluate in sample (USING THE TRAINING DATA)
predY = learner.query(trainX) # get the predictions
rmse = math.sqrt(((trainY - predY) ** 2).sum()/trainY.shape[0])
print("In sample results")
print(f"RMSE: {rmse}")
c = np.corrcoef(predY, y=trainY)
print(f"corr: {c[0,1]}")
# evaluate out of sample (USING THE TEST DATA)
predY = learner.query(testX) # get the predictions
rmse = math.sqrt(((testY - predY) ** 2).sum()/testY.shape[0])
print()
print("Out of sample results")
print(f"RMSE: {rmse}")
c = np.corrcoef(predY, y=testY)
print(f"corr: {c[0,1]}")
# -----------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------
# create an Insane Learner and train it
# -----------------------------------------------------------------------------------------------
learner = it.InsaneLearner(verbose=False) # constructor
learner.addEvidence(trainX, trainY) # training step
# Y = learner.query(testX)
print("--------INSANE LEARNER--------")
# evaluate in sample (USING THE TRAINING DATA)
predY = learner.query(trainX) # get the predictions
rmse = math.sqrt(((trainY - predY) ** 2).sum()/trainY.shape[0])
print("In sample results")
print(f"RMSE: {rmse}")
c = np.corrcoef(predY, y=trainY)
print(f"corr: {c[0,1]}")
# evaluate out of sample (USING THE TEST DATA)
predY = learner.query(testX) # get the predictions
rmse = math.sqrt(((testY - predY) ** 2).sum()/testY.shape[0])
print()
print("Out of sample results")
print(f"RMSE: {rmse}")
c = np.corrcoef(predY, y=testY)
print(f"corr: {c[0,1]}")
# -----------------------------------------------------------------------------------------------
# report question number 1
# -----------------------------------------------------------------------------------------------
print("--------GRAPHS FOR QUESTION 1--------")
inSampleRSME = [0] # add dummy value so that leaf size of 1 is at index 1
outSampleRSME = [0] # add dummy value so that leaf size of 1 is at index 1
for i in range(1,51): #0-50 leaf sizes
# create the learner with varying leaf size
learner = dt.DTLearner(leaf_size=i, verbose=False)
learner.addEvidence(trainX, trainY) # training step
# IN SAMPLE-------------------------------------------
predY = learner.query(trainX) # get the predictions
rmse = math.sqrt(((trainY - predY) ** 2).sum()/trainY.shape[0])
inSampleRSME.append(rmse) #build in-sample RSME data
# OUT SAMPLE-------------------------------------------
predY = learner.query(testX) # get the predictions
rmse = math.sqrt(((testY - predY) ** 2).sum()/testY.shape[0])
outSampleRSME.append(rmse)
inSampleRSME
fig, ax = plt.subplots(figsize=(11, 9))
ax.plot(inSampleRSME)
ax.plot(outSampleRSME)
ax.set_title('Overfitting assessed by RSME vs. LeafSize using DTLearners')
# ax.legend(('In_Sample","Out_Sample'))
ax.set_ylabel('RSME')
ax.set_xlabel('Leaf_Size')
ax.set_xlim(1, 50) # set the xlim
# dim = np.arange(1,50,5); # get your locations
# ax.set_xticks(dim) # set the locations of the xticks to be on the integers
# ax.grid() # turn the grid on
ax.legend(('In_Sample', 'Out_Sample'), loc='lower right')
plt.savefig('report_question1.png')
# plt.show()
# -----------------------------------------------------------------------------------------------
# report question number 1
# -----------------------------------------------------------------------------------------------
print("--------GRAPHS FOR QUESTION 2--------")
inSampleRSME = [0] # add dummy value so that leaf size of 1 is at index 1
outSampleRSME = [0] # add dummy value so that leaf size of 1 is at index 1
for i in range(1,51): #1-50 leaf sizes
# create the learner with varying leaf size
learner = bg.BagLearner(learner=dt.DTLearner, kwargs={"leaf_size":i}, bags=20, boost=False, verbose=False)
learner.addEvidence(trainX, trainY)
# IN SAMPLE-------------------------------------------
predY = learner.query(trainX) # get the predictions
rmse = math.sqrt(((trainY - predY) ** 2).sum()/trainY.shape[0])
inSampleRSME.append(rmse) #build in-sample RSME data
# OUT SAMPLE-------------------------------------------
predY = learner.query(testX) # get the predictions
rmse = math.sqrt(((testY - predY) ** 2).sum()/testY.shape[0])
outSampleRSME.append(rmse)
inSampleRSME
fig, ax = plt.subplots(figsize=(11, 9))
ax.plot(inSampleRSME)
ax.plot(outSampleRSME)
ax.set_title('Overfitting assessed by RSME vs. LeafSize using a BagLearner with bags=20 of DTLearners')
# ax.legend(('In_Sample","Out_Sample'))
ax.set_ylabel('RSME')
ax.set_xlabel('Leaf_Size')
ax.set_xlim(1, 50) # set the xlim
# dim = np.arange(1,50,5); # get your locations
# ax.set_xticks(dim) # set the locations of the xticks to be on the integers
# ax.grid() # turn the grid on
ax.legend(('In_Sample', 'Out_Sample'), loc='lower right')
plt.savefig('report_question2.png')
# plt.show()
# -----------------------------------------------------------------------------------------------
# report question number 3
# -----------------------------------------------------------------------------------------------
print("--------GRAPHS FOR QUESTION 3--------")
DT_In_Sample_MPE = [0] # add dummy value so that leaf size of 1 is at index 1
# DT_Out_Sample_MAPE = [0] # add dummy value so that leaf size of 1 is at index 1
RT_In_Sample_MPE = [0] # add dummy value so that leaf size of 1 is at index 1
# RT_Out_Sample_MAPE = [0] # add dummy value so that leaf size of 1 is at index 1
for i in range(1,51): #1-50 leaf sizes
# create the learner with varying leaf size
learner = dt.DTLearner(leaf_size=i, verbose=False)
learner.addEvidence(trainX, trainY) # training step
# IN SAMPLE-------------------------------------------
predY = learner.query(trainX) # get the predictions
MPE = np.mean((trainY - predY) / trainY) * 100 / len(trainY)
DT_In_Sample_MPE.append(MPE)
# OUT SAMPLE-------------------------------------------
# predY = learner.query(testX) # get the predictions
# MAPE = np.mean(np.abs(((testY - predY) / testY)) * (100 / len(testY)))
# DT_Out_Sample_MAPE.append(MAPE)
learner = rt.RTLearner(leaf_size=i, verbose=False)
learner.addEvidence(trainX, trainY) # training step
# IN SAMPLE-------------------------------------------
predY = learner.query(trainX) # get the predictions
MPE = np.mean((trainY - predY) / trainY) * 100 / len(trainY)
RT_In_Sample_MPE.append(MPE)
# OUT SAMPLE-------------------------------------------
# predY = learner.query(testX) # get the predictions
# MAPE = np.mean(np.abs(((testY - predY) / testY)) * (100 / len(testY)))
# RT_Out_Sample_MAPE.append(MAPE)
fig, ax = plt.subplots(figsize=(11, 9))
ax.plot(DT_In_Sample_MPE, color='r')
# ax.plot(DT_Out_Sample_MAPE)
ax.plot(RT_In_Sample_MPE, color='g')
# ax.plot(RT_Out_Sample_MAPE)
ax.set_title('MPE vs. Leaf Size for DTLearners vs. RTLearners Using Training Data')
# ax.legend(('In_Sample","Out_Sample'))
ax.set_ylabel('MPE')
ax.set_xlabel('Leaf_Size')
ax.set_xlim(1, 50) # set the xlim
# dim = np.arange(1,50,5); # get your locations
# ax.set_xticks(dim) # set the locations of the xticks to be on the integers
# ax.grid() # turn the grid on
ax.legend(('DT_In_Sample_MPE', 'RT_In_Sample_MPE'), loc='lower right')
plt.savefig('report_question3_metric1.png')
# plt.show()
# -----------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------
# second metric
# -----------------------------------------------------------------------------------------------
DT_In_Sample_TIME = [0] # add dummy value so that leaf size of 1 is at index 1
# DT_Out_Sample_MAPE = [0] # add dummy value so that leaf size of 1 is at index 1
RT_In_Sample_TIME = [0] # add dummy value so that leaf size of 1 is at index 1
# RT_Out_Sample_MAPE = [0] # add dummy value so that leaf size of 1 is at index 1
for i in range(1,51): #1-50 leaf sizes
# create the learner with varying leaf size
# IN SAMPLE-------------------------------------------
learner = dt.DTLearner(leaf_size=i, verbose=False)
start = ti.time()
learner.addEvidence(trainX, trainY) # training step
DT_In_Sample_TIME.append(ti.time() - start)
learner = rt.RTLearner(leaf_size=i, verbose=False)
start = ti.time()
learner.addEvidence(trainX, trainY) # training step
RT_In_Sample_TIME.append(ti.time() - start)
fig, ax = plt.subplots(figsize=(11, 9))
ax.plot(DT_In_Sample_TIME, color='m')
# ax.plot(DT_Out_Sample_MAPE)
ax.plot(RT_In_Sample_TIME, color='y')
# ax.plot(RT_Out_Sample_MAPE)
ax.set_title('Time vs. Leaf Size for DTLearners vs. RTLearners Using Training Data')
# ax.legend(('In_Sample","Out_Sample'))
ax.set_ylabel('Time (seconds)')
ax.set_xlabel('Leaf_Size')
ax.set_xlim(1, 50) # set the xlim
# dim = np.arange(1,50,5); # get your locations
# ax.set_xticks(dim) # set the locations of the xticks to be on the integers
# ax.grid() # turn the grid on
ax.legend(('DT_In_Sample_TIME', 'RT_In_Sample_TIME'), loc='upper right')
plt.savefig('report_question3_metric2.png')
# plt.show()