-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
766 lines (564 loc) · 22.3 KB
/
Copy pathpreprocessing.py
File metadata and controls
766 lines (564 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
from numpy.core.multiarray import ndarray
from pymongo import MongoClient
import numpy as np
import matplotlib.pyplot as plt
import sklearn
import time
import datetime as dt
import pandas as pd
import seaborn as sns
pd.set_option('display.max_columns', None)
import warnings
warnings.filterwarnings("ignore")
# functions required
def replace_space(element, char):
if element != '':
return element
else:
return char
def check_status(mins, status):
if status == "LATE":
return -mins
else:
return mins
def get_operator(operator):
switcher = {
'23': "Arriva Trains Northern",
'71': "Transport for Wales",
'79': "c2c",
'35': "Caledonian Sleeper",
'74': "Chiltern Railway",
'27': "CrossCountry",
'34': "Devon and Cornwall Railways",
'28': "East Midlands Trains",
'06': "Eurostar",
'26': "First Capital Connect (defunct)",
'55': "First Hull Trains",
'81': "Gatwick Express",
'54': "GB Railfreight",
'88': "Govia Thameslink Railway / Southern",
'22': "Grand Central",
'25': "Great Western Railway",
'21': "Greater Anglia",
'24': "Heathrow Connect",
'86': "Heathrow Express",
'85': "Island Lines",
'29': "London Midlands",
'30': "London Overground",
'64': "Merseyrail",
'00': "Network Rail",
'56': "Nexus",
'51': "North Yorkshire Moors Railway",
'60': "ScotRail",
'84': "South Western Railway",
'19': "South Yorkshire Supertram",
'80': "Southeastern",
'33': "TFL Rail",
'20': "TransPennine Express",
'65': "Virgin Trains",
'61': "Virgin Trains East Coast",
'50': "West Coast Railway Co."
}
return switcher.get(operator, "Invalid Station")
def get_stanox(stanox):
switcher = {
'87031': "AHT",
'87021': "AON",
'86074': "ADV",
'87763': "AHS",
'86066': "BSK",
'86896': "BEU",
'86339': "BDH",
'87024': "BTY",
'86216': "BTE",
'87009': "BAW",
'86202': "BOE",
'86921': "BMH",
'86061': "BMY",
'86901': "BCU",
'86223': "BUO",
'86108': "CFR",
'86915': "CHR",
'86301': "CSA",
'86112': "DEN",
'86107': "DBG",
'86087': "ESL",
'86343': "EMS",
'86241': "FRM",
'86042': "FNB",
'87010': "FNN",
'87026': "FNH",
'86045': "FLE",
'86321': "FTN",
'86077': "GRT",
'86222': "HME",
'87062': "HSL",
'86341': "HAV",
'86201': "HDE",
'86332': "HLS",
'86913': "HNA",
'86049': "HOK",
'87064': "LIP",
'87065': "LIS",
'86908': "LYP",
'86907': "LYT",
'86081': "MIC",
'86527': "MBK",
'86219': "NTL",
'86911': "NWM",
'87012': "NCM",
'86070': "OVR",
'87066': "PTR",
'86917': "POK",
'86248': "PTC",
'86313': "PMS",
'86311': "PMH",
'86703': "RDB",
'86101': "ROM",
'87067': "RLN",
'86122': "SAL",
'86084': "SHW",
'86218': "SHO",
'86495': "SOA",
'86520': "SOU",
'86499': "SDN",
'86225': "SNW",
'86909': "SWY",
'86497': "SWG",
'86711': "TTN",
'86342': "WBL",
'86071': "WCH",
'86083': "WIN",
'86047': "WNF",
'86215': "WLS"
}
return switcher.get(stanox, "Invalid Stanox")
# Import data
client = MongoClient()
dblist = client.list_database_names()
db = client["Third_Year_Project"]
real_time_data = db["Real_Time_Data_HANTS_2_2weeks"]
# Import Data
json_data = real_time_data.find({"header.msg_type": '0003'})
json_change_orig_data = real_time_data.find({"header.msg_type": '0006'})
json_cancel_data = real_time_data.find({"header.msg_type": "0002"})
data = []
change_orig_data = []
cancel_data = []
for x in json_change_orig_data:
change_orig_data.append(list(x['body'].values()))
for x in json_cancel_data:
cancel_data.append(list(x['body'].values()))
# Change data to neg data as in characterisation
neg_data = []
label_data = []
print("created array")
for x in json_data:
if x['body']['planned_timestamp'] != "":
neg_data.append([
x['body']['event_type'], # event_type
x['body']['planned_timestamp'], # planned timestamp
# x[3], # Current train id
# x[4], # likely mins until next stanox report WONT HAVE IN REAL INPUT DATA
# x[5], # timestamp of arrival/departure WONT HAVE IN REAL INPUT DATA
# x[6], # False if not a correction of a previous report, True if it is WONT HAVE IN REAL
# x[7], # Platform number WONT HAVE IN REAL INPUT DATA
# x['body']['train_id'], # train_id
# x[9], # ONTIME/EARLY/LATE/OFFROUTE WONT HAVE IN REAL INPUT DATA
x['body']['train_service_code'], # train_service_code
# x['body']['toc_id'], # toc_id
x['body']['loc_stanox'] # station stanox (location currently)
# x['body']['next_report_stanox'], # the next station
])
label_data.append(
int(check_status(int(x['body']['timetable_variation']), x['body']['variation_status']))
# Minutes variation from timetabled time LABEL TO FIND
)
# Label/data
X = np.array(neg_data)
print(np.shape(X))
y = label_data
print(np.shape(y))
# ******** Pre processing ***********
df = pd.DataFrame(data=X, columns=["event_type", "planned_timestamp", "train_service_code", "loc_stanox"])
print(df.head(5))
# Convert timestamp
years = []
months = []
days = []
times = []
weekdays = []
for x in range(0, len(df)):
temp_time = int(int(df['planned_timestamp'][x]) / 1000)
readable = dt.datetime.fromtimestamp(temp_time).isoformat()
# years.append(int(readable.split("-")[0]))
# months.append(int(readable.split("-")[1]))
daytime = readable.split("-")[2]
days.append(daytime.split("T")[0])
hh = (daytime.split("T")[1]).split(":")[0]
mm = (daytime.split("T")[1]).split(":")[1]
ss = (daytime.split("T")[1]).split(":")[2]
times.append(int((dt.timedelta(hours=int(hh), minutes=int(mm), seconds=int(ss))).total_seconds()))
weekdays.append(dt.datetime.fromtimestamp(temp_time).weekday())
# df['year'] = pd.Series(np.array(years)).values
# df['month'] = pd.Series(np.array(months)).values
df['day'] = pd.Series(np.array(days)).values
df['time'] = pd.Series(np.array(times)).values
df['weekday'] = pd.Series(np.array(weekdays)).values
# One Hot Encoding using pandas
cat_cols = ['event_type', "train_service_code", "loc_stanox"]
df_processed = pd.get_dummies(df, prefix_sep="__", columns=cat_cols)
#print(df_p_X_train.head(1))
# Previous Station -> delay at previous station?
# Correlation between vars -> do after creating/hot encoding features
# matrix = df_processed.corr()
# sns.heatmap(matrix, xticklabels=1, yticklabels=1)
# plt.show()
# Split data into training/testing and validation 80-20, then split training/testing using k-fold cross validation
from sklearn.model_selection import train_test_split
X_train_test, X_valid, y_train_test, y_valid = train_test_split(df_processed, y, test_size=0.2)
X_train, X_test, y_train, y_test = train_test_split(X_train_test, y_train_test, test_size=0.2)
# Quick Random Forest
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import BayesianRidge
from sklearn.linear_model import PassiveAggressiveRegressor
from sklearn.linear_model import HuberRegressor
from sklearn.linear_model import RANSACRegressor
from sklearn.linear_model import ARDRegression
from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import Ridge
from sklearn.linear_model import Perceptron
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import BaggingRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import GradientBoostingRegressor
# Anything else?
# K Fold Cross Validation
from sklearn.model_selection import KFold
splits = 10
kf = KFold(n_splits=splits)
kf_X_train_test = np.array(X_train_test)
kf_y_train_test = np.array(y_train_test)
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = LinearRegression()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Linear Regression Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Linear Regression Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Random Forest Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = RandomForestRegressor()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("RF Regression Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("RF Regression Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Lasso Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = Lasso()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Lasso Regression Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Lasso Regression Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Logistic Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = LogisticRegression()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Logistic Regression Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Logistic Regression Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Elastic Net Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = ElasticNet()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Elastic Net Regression Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Elastic Net Regression Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# BayesianRidge Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = BayesianRidge()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Bayesian Ridge Regression Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Bayesian Ridge Regression Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Passive Aggressive Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = PassiveAggressiveRegressor()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Passive Aggressive Regression Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Passive Aggressive Regression Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Huber Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = HuberRegressor()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Huber Regression Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Huber Regression Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# RANSAC Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = RANSACRegressor()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("RANSAC Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("RANSAC Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# SGD Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = SGDRegressor()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("SGD Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("SGD Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Ridge Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = Ridge()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Ridge Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Ridge Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Perceptron
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = Perceptron()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Perceptron Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Perceptron Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Extra Trees Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = ExtraTreesRegressor()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("Extra Trees Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("Extra Trees Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# MLP Regression
train_rmse_agg = []
test_rmse_agg = []
for train_index, test_index in kf.split(kf_X_train_test):
kf_X_train, kf_X_test = kf_X_train_test[train_index], kf_X_train_test[test_index]
kf_y_train, kf_y_test = kf_y_train_test[train_index], kf_y_train_test[test_index]
reg = MLPRegressor()
reg.fit(kf_X_train, kf_y_train)
train_predict = reg.predict(kf_X_train)
train_rmse = np.sqrt(((kf_y_train - train_predict) ** 2).mean())
train_rmse_agg.append(train_rmse)
test_predict = reg.predict(kf_X_test)
test_rmse = np.sqrt(((kf_y_test - test_predict) ** 2).mean())
test_rmse_agg.append(test_rmse)
# Calculate Mean
train_mean = np.mean(train_rmse_agg)
test_mean = np.mean(test_rmse_agg)
# Calculate Var
train_sd = np.std(train_rmse_agg)
test_sd = np.std(test_rmse_agg)
print("MLP Training RMSE: ", train_mean, "\t sd: ", train_sd)
print("MLP Testing RMSE: ", test_mean, "\t sd: ", test_sd)
# Code Structure for fine tuning algorithm hyperparameters
# may need to improve or change metric being used to give results in respect to research and justification
# test_rmses = []
# train_rmses = []
# ns = [1, 5, 10, 50,100, 500, 1000]
# repeats = 5
# for x in ns:
# train_rmse_agg = 0
# test_rmse_agg = 0
# print(x)
# for y in range(0, repeats):