-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
609 lines (448 loc) · 17.5 KB
/
main.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
"""
Calibration of sensors in uncontrolled environments in
Air Pollution Sensor Monitoring Networks
TOML - Project 2
Marcel Cases
June 2021
"""
#%%
# General dependencies
import pandas as pd # for data handling
import matplotlib.pyplot as plt # for linear plot
import seaborn as sns # for scatter plot
from sklearn.model_selection import train_test_split
import datetime
#%%
# Read sensor data
sensor = pd.read_csv("data.csv", sep = ';', index_col = 0, parse_dates = False)
# Build main dataset
df = pd.DataFrame({'RefSt': sensor["RefSt"], 'Sensor_O3': sensor["Sensor_O3"], 'Temp': sensor["Temp"], 'RelHum': sensor["RelHum"]})
# Split main dataset and build train and test datasets
X = df[['Sensor_O3', 'Temp', 'RelHum']]
Y = df['RefSt']
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 1, shuffle = False)
df_train = pd.DataFrame({'RefSt': Y_train, 'Sensor_O3': X_train["Sensor_O3"], 'Temp': X_train["Temp"], 'RelHum': X_train["RelHum"]})
df_test = pd.DataFrame({'RefSt': Y_test, 'Sensor_O3': X_test["Sensor_O3"], 'Temp': X_test["Temp"], 'RelHum': X_test["RelHum"]})
#%%
# Loss functions definition
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
def loss_functions(y_true, y_pred):
print("Loss functions:")
print("* R-squared =", r2_score(y_true, y_pred))
print("* RMSE =", mean_squared_error(y_true, y_pred))
print("* MAE =", mean_absolute_error(y_true, y_pred))
# %%
# Normalise sensor data
def normalize(col):
μ = col.mean()
σ = col.std()
return (col - μ)/σ
df["normRefSt"] = normalize(df["RefSt"])
df["normSensor_O3"] = normalize(df["Sensor_O3"])
df["normTemp"] = normalize(df["Temp"])
df["normRelHum"] = normalize(df["RelHum"])
#%%
# Intro to Pandas
# Print first top lines from data
print(sensor.head(5))
#%%
# Print all data types
print(sensor.dtypes)
#%%
# Show data info summary
print(sensor.info())
#%%
# Select and print specific columns
Temp_Sensor_O3 = sensor[["Temp", "Sensor_O3"]]
print(Temp_Sensor_O3.head(5))
#%%
# Simple plot
df.plot()
plt.xticks(rotation = 20)
#%%
# Data observation
# Plot the ozone (KOhms) and ozone reference data (μgr/m^3) as function of time
df[["RefSt", "Sensor_O3"]].plot()
plt.xticks(rotation = 20)
# %%
# Plot the ozone (KOhms) and ozone reference data (μgr/m^3) as function of time - factor
Sensor_O3_RefSt_factor = df[["Sensor_O3", "RefSt"]]
Sensor_O3_RefSt_factor["RefSt"] = 4*Sensor_O3_RefSt_factor["RefSt"]
Sensor_O3_RefSt_factor.plot()
plt.xticks(rotation = 20)
# %%
# Raw scatter plot
sns.lmplot(x = 'Sensor_O3', y = 'RefSt', data = df, fit_reg = False, line_kws = {'color': 'orange'})
# %%
# Normalised scatter plot
sns.lmplot(x = 'normSensor_O3', y = 'normRefSt', data = df, fit_reg = False, line_kws = {'color': 'orange'})
# %%
# Temp with respect to Sensor_O3
sns.lmplot(x = 'Sensor_O3', y = 'Temp', data = df, fit_reg = False, line_kws = {'color': 'orange'})
# %%
# Temp with respect to RefSt
sns.lmplot(x = 'RefSt', y = 'Temp', data = df, fit_reg = False, line_kws = {'color': 'orange'})
# %%
# RelHum with respect to Sensor_O3
sns.lmplot(x = 'Sensor_O3', y = 'RelHum', data = df, fit_reg = False, line_kws = {'color': 'orange'})
# %%
# RelHum with respect to RefSt
sns.lmplot(x = 'RefSt', y = 'RelHum', data = df, fit_reg = False, line_kws = {'color': 'orange'})
# %%
# Data calibration
# Multiple Linear Regression
from sklearn.linear_model import LinearRegression
# Model
lr = LinearRegression()
# Fit
lr.fit(X_train, Y_train)
# Get MLR coefficients
print('Intercept: \n', lr.intercept_)
print('Coefficients: \n', lr.coef_)
# Predict
df_test["MLR_Pred"] = lr.intercept_ + lr.coef_[0]*df_test["Sensor_O3"] + lr.coef_[1]*df_test["Temp"] + lr.coef_[2]*df_test["RelHum"]
# Plot linear
df_test[["RefSt", "MLR_Pred"]].plot()
plt.xticks(rotation = 20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'MLR_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["MLR_Pred"])
# %%
# Multiple Linear Regression with Batch Gradient Descent
# %%
# Multiple Linear Regression with Stochastic Gradient Descent
from sklearn.linear_model import SGDRegressor
from sklearn.preprocessing import StandardScaler
# Model
# sgdr = SGDRegressor(loss='squared_loss', alpha=.001, tol=1e-5)
sgdr = SGDRegressor(loss = 'squared_loss', max_iter = 5)
# Normalize
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Fit
sgdr.fit(X_train, Y_train)
# Get MLR coefficients
print('Intercept: \n', sgdr.intercept_)
print('Coefficients: \n', sgdr.coef_)
print('Iters: \n', sgdr.n_iter_)
print(sgdr.get_params())
# Predict
# df_test["MLR_SGDR_Pred"] = sgdr.intercept_ + sgdr.coef_[0]*X_test[0] + sgdr.coef_[1]*X_test[1] - sgdr.coef_[2]*X_test[2]
df_test["MLR_SGD_Pred"] = sgdr.predict(X_test)
# Plot linear
df_test[["RefSt", "MLR_SGD_Pred"]].plot()
plt.xticks(rotation = 20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'MLR_SGD_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["MLR_SGD_Pred"])
# %%
# K-Nearest Neighbor
from sklearn.neighbors import KNeighborsRegressor
# Model
knn = KNeighborsRegressor(n_neighbors = 19)
# Fit
knn.fit(X_train, Y_train)
# Predict
df_test["KNN_Pred"] = knn.predict(X_test)
print(df_test)
# Plot linear
df_test[["RefSt", "KNN_Pred"]].plot()
plt.xticks(rotation=20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'KNN_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["KNN_Pred"])
# %%
# K-Nearest Neighbor stats vs. hyperparameters
def knn_stats():
knn_aux = pd.DataFrame({'RefSt': Y_test})
n_neighbors = [*range(1, 151, 1)]
r_squared = []
rmse = []
mae = []
time_ms = []
for i in n_neighbors:
# Model
knn = KNeighborsRegressor(n_neighbors=i)
# Fit
start_time = float(datetime.datetime.now().strftime('%S.%f'))
knn.fit(X_train, Y_train)
end_time = float(datetime.datetime.now().strftime('%S.%f'))
execution_time = (end_time - start_time) * 1000
# Predict
knn_aux["KNN_Pred"] = knn.predict(X_test)
# Loss
r_squared.append(r2_score(knn_aux["RefSt"], knn_aux["KNN_Pred"]))
rmse.append(mean_squared_error(knn_aux["RefSt"], knn_aux["KNN_Pred"]))
mae.append(mean_absolute_error(knn_aux["RefSt"], knn_aux["KNN_Pred"]))
time_ms.append(execution_time)
knn_stats = pd.DataFrame({'k': n_neighbors, 'r_squared': r_squared, 'rmse': rmse, 'mae': mae, 'time_ms': time_ms})
knn_stats = knn_stats.set_index('k') # index column (X axis for the plots)
print(knn_stats)
# plot
knn_stats[["r_squared"]].plot()
knn_stats[["rmse"]].plot()
knn_stats[["mae"]].plot()
knn_stats[["time_ms"]].plot()
knn_stats()
# %%
# Random Forest
from sklearn.ensemble import RandomForestRegressor
# Model
rf = RandomForestRegressor(n_estimators = 20 ,random_state = 0)
# Fit
rf.fit(X_train, Y_train)
# Predict
df_test["RF_Pred"] = rf.predict(X_test)
print(df_test)
# Plot linear
df_test[["RefSt", "RF_Pred"]].plot()
plt.xticks(rotation = 20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'RF_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["RF_Pred"])
# RF feature importances
print('Feature importances:\n', list(zip(X.columns, rf.feature_importances_)))
# %%
# Random Forest stats vs. hyperparameters
def rf_stats():
rf_aux = pd.DataFrame({'RefSt': Y_test})
n_estimators = [*range(1, 101, 1)]
r_squared = []
rmse = []
mae = []
time_ms = []
for i in n_estimators:
rf=RandomForestRegressor(n_estimators=i,random_state=0)
# fit
start_time = float(datetime.datetime.now().strftime('%S.%f'))
rf.fit(X_train, Y_train)
end_time = float(datetime.datetime.now().strftime('%S.%f'))
execution_time = (end_time - start_time) * 1000
# predict
rf_aux["RF_Pred"] = rf.predict(X_test)
# Loss
r_squared.append(r2_score(rf_aux["RefSt"], rf_aux["RF_Pred"]))
rmse.append(mean_squared_error(rf_aux["RefSt"], rf_aux["RF_Pred"]))
mae.append(mean_absolute_error(rf_aux["RefSt"], rf_aux["RF_Pred"]))
time_ms.append(execution_time)
rf_stats = pd.DataFrame({'n_estimators': n_estimators, 'r_squared': r_squared, 'rmse': rmse, 'mae': mae, 'time_ms': time_ms})
rf_stats = rf_stats.set_index('n_estimators') # index column (X axis for the plots)
print(rf_stats)
# plot
rf_stats[["r_squared"]].plot()
rf_stats[["rmse"]].plot()
rf_stats[["mae"]].plot()
rf_stats[["time_ms"]].plot()
rf_stats()
# %%
# Kernel Regression
# from sklearn_extensions.kernel_regression import KernelRegression
from sklearn.kernel_ridge import KernelRidge
# Models
kr_rbf = KernelRidge(kernel = "rbf")
kr_poly = KernelRidge(kernel = "poly", degree = 4)
# Fit
kr_rbf.fit(X_train, Y_train)
kr_poly.fit(X_train, Y_train)
# Predict
df_test["KR_RBF_Pred"] = kr_rbf.predict(X_test)
df_test["KR_Poly_Pred"] = kr_poly.predict(X_test)
# Plot linear
df_test[["RefSt", "KR_RBF_Pred", "KR_Poly_Pred"]].plot()
plt.xticks(rotation=20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'KR_RBF_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
sns.lmplot(x = 'RefSt', y = 'KR_Poly_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["KR_RBF_Pred"])
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["KR_Poly_Pred"])
# %%
# Polynomial Kernel Regression stats vs. hyperparameters
def kr_stats():
kr_aux = pd.DataFrame({'RefSt': Y_test})
degree = [*range(1, 26, 1)]
r_squared = []
rmse = []
mae = []
time_ms = []
for i in degree:
kr = KernelRidge(kernel = "poly", degree = i)
# Fit
start_time = float(datetime.datetime.now().strftime('%S.%f'))
kr.fit(X_train, Y_train)
end_time = float(datetime.datetime.now().strftime('%S.%f'))
execution_time = (end_time - start_time) * 1000
# Predict
kr_aux["KR_Pred"] = kr.predict(X_test)
# Loss
r_squared.append(r2_score(kr_aux["RefSt"], kr_aux["KR_Pred"]))
rmse.append(mean_squared_error(kr_aux["RefSt"], kr_aux["KR_Pred"]))
mae.append(mean_absolute_error(kr_aux["RefSt"], kr_aux["KR_Pred"]))
time_ms.append(execution_time)
kr_stats = pd.DataFrame({'degree': degree, 'r_squared': r_squared, 'rmse': rmse, 'mae': mae, 'time_ms': time_ms})
kr_stats = kr_stats.set_index('degree') # index column (X axis for the plots)
print(kr_stats)
# plot
kr_stats[["r_squared"]].plot()
kr_stats[["rmse"]].plot()
kr_stats[["mae"]].plot()
kr_stats[["time_ms"]].plot()
kr_stats()
# %%
# Gaussian Process
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import ConstantKernel, RBF, DotProduct, WhiteKernel
# Kernels definition
# rbf = ConstantKernel(constant_value=1.0, constant_value_bounds=(1e-10, 1e10)) * RBF(length_scale=1.0, length_scale_bounds=(1e-10, 1e10))
rbf = ConstantKernel() * RBF()
dpwh = DotProduct() + WhiteKernel()
# Models
gp_rbf = GaussianProcessRegressor(kernel = rbf, alpha = 150, random_state = 0)
gp_dpwh = GaussianProcessRegressor(kernel = dpwh, alpha = 150, random_state = 0)
# Fit
gp_rbf.fit(X_train, Y_train)
gp_dpwh.fit(X_train, Y_train)
# Predict
df_test["GP_RBF_Pred"] = gp_rbf.predict(X_test)
df_test["GP_DPWK_Pred"] = gp_dpwh.predict(X_test)
# Obtain optimized kernel parameters
# l = gp.kernel_.k2.get_params()['length_scale']
# sigma_f = np.sqrt(gp.kernel_.k1.get_params()['constant_value'])
# Print parameters
print("RBF params", gp_rbf.get_params())
print("Dot params", gp_dpwh.get_params())
# Plot linear
df_test[["RefSt", "GP_RBF_Pred", "GP_DPWK_Pred"]].plot()
plt.xticks(rotation = 20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'GP_RBF_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
sns.lmplot(x = 'RefSt', y = 'GP_DPWK_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["GP_RBF_Pred"])
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["GP_DPWK_Pred"])
# %%
# Gaussian Process stats vs. hyperparameters
def gp_stats():
gp_aux = pd.DataFrame({'RefSt': Y_test})
alpha = [*range(20, 202, 2)]
# alpha = [1e-5,1e-4,1e-3,1e-2,1e-1,1,10,50,100,150,200]
r_squared = []
rmse = []
mae = []
time_ms = []
rbf = ConstantKernel() * RBF()
for i in alpha:
gp_rbf = GaussianProcessRegressor(kernel = rbf, alpha = i, random_state = 0)
# gp = GaussianProcessRegressor(kernel=rbf, alpha=i, random_state=0)
# fit
start_time = float(datetime.datetime.now().strftime('%S.%f'))
gp_rbf.fit(X_train, Y_train)
end_time = float(datetime.datetime.now().strftime('%S.%f'))
execution_time = (end_time - start_time) * 1000
# predict
gp_aux["GP_RBF_Pred"] = gp_rbf.predict(X_test)
# Loss
r_squared.append(r2_score(gp_aux["RefSt"], gp_aux["GP_RBF_Pred"]))
rmse.append(mean_squared_error(gp_aux["RefSt"], gp_aux["GP_RBF_Pred"]))
mae.append(mean_absolute_error(gp_aux["RefSt"], gp_aux["GP_RBF_Pred"]))
time_ms.append(execution_time)
gp_stats = pd.DataFrame({'alpha': alpha, 'r_squared': r_squared, 'rmse': rmse, 'mae': mae, 'time_ms': time_ms})
gp_stats = gp_stats.set_index('alpha') # index column (X axis for the plots)
print(gp_stats)
# plot
gp_stats[["r_squared"]].plot()
gp_stats[["rmse"]].plot()
gp_stats[["mae"]].plot()
gp_stats[["time_ms"]].plot()
gp_stats()
# %%
# Support Vector Regression
from sklearn.svm import SVR
# Models
svr_rbf = SVR(kernel = 'rbf', C = 1e3)#, gamma=0.1)
svr_lin = SVR(kernel = 'linear', C = 1e3)
svr_poly = SVR(kernel = 'poly', C = 1e3, degree = 3)
# Fit
svr_rbf.fit(X_train, Y_train)
svr_lin.fit(X_train, Y_train)
svr_poly.fit(X_train, Y_train)
# Predict
df_test["SVR_RBF_Pred"] = svr_rbf.predict(X_test)
df_test["SVR_Line_Pred"] = svr_lin.predict(X_test)
df_test["SVR_Poly_Pred"] = svr_poly.predict(X_test)
# Plot linear
df_test[["RefSt", "SVR_RBF_Pred", "SVR_Line_Pred", "SVR_Poly_Pred"]].plot()
plt.xticks(rotation=20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'SVR_RBF_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
sns.lmplot(x = 'RefSt', y = 'SVR_Line_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
sns.lmplot(x = 'RefSt', y = 'SVR_Poly_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["SVR_RBF_Pred"])
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["SVR_Line_Pred"])
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["SVR_Poly_Pred"])
# %%
# Neural Network - SKL
from sklearn.neural_network import MLPRegressor
# Model
mlp = MLPRegressor(hidden_layer_sizes=(16,16), activation='relu', solver='adam', max_iter=1000)
# Fit
mlp.fit(X_train,Y_train)
# Predict
# predict_train = mlp.predict(X_train)
df_test["NN_Pred"] = mlp.predict(X_test)
print(df_test)
# Plot linear
df_test[["RefSt", "NN_Pred"]].plot()
plt.xticks(rotation=20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'NN_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["NN_Pred"])
# %%
# Neural Network - TF
import tensorflow as tf
from tensorflow.keras.layers import Dense, Activation, InputLayer
from tensorflow.keras.models import Sequential
from sklearn.preprocessing import StandardScaler
print(tf.__version__)
# Normalise data
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Model
nn = Sequential()
# Model - Layers
nn.add(InputLayer(input_shape = (3))) # Input layer
nn.add(Dense(units = 64, activation = 'relu')) # 1st hidden layer
nn.add(Dense(units = 64, activation = 'relu')) # 2nd hidden layer
nn.add(Dense(units = 64, activation = 'relu')) # 3rd hidden layer
nn.add(Dense(units = 64, activation = 'relu')) # 4th hidden layer
nn.add(Dense(units = 64, activation = 'relu')) # 5th hidden layer
nn.add(Dense(units = 1)) # Output layer
nn.compile(optimizer = 'adam', loss = 'mean_squared_error')
# Fit
history = nn.fit(X_train, Y_train, batch_size = 10, epochs = 750)
# Plot loss
plt.plot(history.history['loss'][5:])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
# plt.legend(['train', 'val'], loc='upper left')
plt.show()
# Predict
df_test["NN_Pred"] = nn.predict(X_test)
print(df_test)
# Plot linear
df_test[["RefSt", "NN_Pred"]].plot()
plt.xticks(rotation=20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'NN_Pred', data = df_test, fit_reg = True, line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["NN_Pred"])
# %%