-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel_selector_temps.py
139 lines (110 loc) · 3.39 KB
/
model_selector_temps.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
#! /usr/bin/python3
import numpy as np
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt # noqa: E402
import data_generator as gen # noqa: E402
# import models as mod # noqa: E402
import model_selector as sel # noqa: E402
def prepare_data():
y = gen.simulate_annual()
x = np.cumsum(np.ones(y.size)) - 1
return x, y
def plot_data(x, y):
plt.figure(num=7893, figsize=(8, 4.5))
plt.clf()
plt.style.use('dark_background')
plt.plot(x, y, linestyle="none", marker='.', markersize=6)
plt.xlabel("Year")
plt.ylabel("Typical high temp")
plt.gca().set_xlim(-3, 123)
plt.gca().set_ylim(8, 16)
return
def finalize_plot(plotname):
plt.title(plotname)
filename = "_".join(plotname.split()) + ".png"
plt.savefig(filename, dpi=300)
return
def show_just_data(x, y):
plot_data(x, y)
finalize_plot("Typical daily highs")
return
def show_data_connected(x, y):
plot_data(x, y)
plt.plot(x, y)
finalize_plot("Interpolation model")
return
def show_data_connected_test(x_train, y_train, x_test, y_test):
plot_data(x_test, y_test)
plt.plot(x_train, y_train)
finalize_plot("Interpolation model test")
return
def show_model(model, x, y):
plot_data(x, y)
res = sel.train(model, x, y, n_iter=model.n_iter_default)
p_final = res.x
y_predicted = model.evaluate(p_final, x)
plt.plot(x, y_predicted, linewidth=2)
finalize_plot(model.name)
return
def show_model_test(model, x_train, y_train, x_test, y_test):
plot_data(x_test, y_test)
res = sel.train(model, x_train, y_train, n_iter=model.n_iter_default)
p_final = res.x
y_predicted = model.evaluate(p_final, x_test)
plt.plot(x_test, y_predicted, linewidth=2)
finalize_plot(model.name + " test")
return
def show_errors(models, training_errors, testing_errors):
plt.figure(num=9437, figsize=(8, 4.5))
plt.clf()
plt.style.use('dark_background')
for i_model, model in enumerate(models):
try:
order = model.order
except Exception:
continue
if order > 8:
continue
plt.plot(
order,
testing_errors[i_model],
linestyle="none",
color="white",
marker='.',
markersize=12,
)
plt.plot(
order,
training_errors[i_model],
linestyle="none",
marker='o',
markersize=6,
markerfacecolor="none",
markeredgecolor="white",
)
plt.xlabel("Polynomial model order")
plt.ylabel("Error (hollow=training, solid=testing)")
plt.gca().set_xlim(0, 9)
plt.gca().set_ylim(.5, 1)
finalize_plot("Fit errors")
return
def main():
"""
Check whether the code is doing what it should.
"""
x, y = prepare_data()
# show_just_data(x, y)
# for model in mod.all_models:
# show_model(model, x, y)
# show_data_connected(x, y)
x_train, y_train, x_test, y_test = sel.split_data(x, y)
# for model in mod.all_models:
# show_model_test(model, x_train, y_train, x_test, y_test)
# show_data_connected_test(x_train, y_train, x_test, y_test)
models, training_errors, testing_errors = sel.compare_models(
x_train, y_train, x_test, y_test)
show_errors(models, training_errors, testing_errors)
return
if __name__ == "__main__":
main()