-
Notifications
You must be signed in to change notification settings - Fork 5
/
polynomial_r2.py
86 lines (71 loc) · 3.84 KB
/
polynomial_r2.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
import numpy as np
from mylibs.polynomial import Polynomial
from myutils.plot_helper import PlotHelper
from myutils.samples import *
from mylibs.validation import Validation
from mylibs.validation import ValidationResults
from mylibs.latin_hyper_cube import LatinHyperCube
from mylibs.halton import Halton
from mylibs.structured_sample import StructuredSample
if __name__ == '__main__':
polyPlot = PlotHelper(['Eingang', 'Ausgang'], fancy=True, pgf=False)
# the smooth whole function
fx = np.linspace(0, 10, 1001)
fy = list(map(f_2d, fx))
polyPlot.ax.plot(fx, fy, 'r-', label=r'$f_{original}$')
# now we pretend we only know a view points
sample = StructuredSample()
knwonParams = sample.generate_sample_plan(6, 1, [(0., 10.)])
knownParams = np.array(knwonParams).flatten()
#knownParams = np.array([0., 2., 4., 6., 8., 10.])
knownValues = np.array(list(map(f_2d, knownParams)))
# validate points
valiParams = np.array([1., 5. , 9.])
valiValues = np.array(list(map(f_2d, valiParams)))
valiParams = valiParams.reshape((len(valiParams), 1))
fx = fx.reshape((len(fx), 1))
# Polynomials
poly = Polynomial(knownParams, knownValues)
vali_res = []
poly_plot_points = [1, 3, 5, 6, 7]
degrees = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #[3, 5, 6, 7]
plot_styles = ['k', 'silver', 'k', 'darksalmon', 'k', 'cornflowerblue', 'limegreen', 'm', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k']
for i in degrees:
print('##############')
print('Order: {:d}'.format(i))
o = i
poly.update_param(o)
rbfY1 = list(map(poly.predict, fx))
if i in poly_plot_points:
polyPlot.ax.plot(fx, rbfY1, '--', color=plot_styles[i], label=r'$\widehat{f}_{polyn}$ mit $o = ' + str(o) + '$')
vali = Validation()
vali_r = vali.run_full_analysis(fx, fy,
knownParams.reshape((len(knownParams), 1)), knownValues,
valiParams, valiValues,
poly.predict, Polynomial,
update_params=[i])
vali_res.append(vali_r)
#deviation = vali.calc_deviation(fx, fy, poly.predict)
#rmse = vali.calc_rmse(valiParams, valiValues, poly.predict)
#mae = vali.calc_mae(valiParams, valiValues, poly.predict)
#rae = vali.calc_rae(valiParams, valiValues, poly.predict)
#press = vali.calc_press(knownParams.reshape((len(knownParams), 1)), knownValues, poly.predict, Polynomial, update_params=[i])
print('avg deviation: {:.3e} (-> {:.3f}%)'.format(vali_r.deviation, vali_r.deviation * 100.))
print('rmse: {:f}'.format(vali_r.rmse))
print('mae: {:f}'.format(vali_r.mae))
print('rae: {:s}'.format(str(vali_r.rae)))
print('press: {:f}'.format(vali_r.press))
polyPlot.ax.plot(knownParams, knownValues, 'ro', label=r'St\"utzstellen', markersize=10)
polyPlot.ax.scatter(valiParams, valiValues, facecolors='none', edgecolors='r', label=r'Validierungspunkte', s=100)
polyPlot.finalize(width=6, height=4, legendLoc='upper left', legendNcol=2)
polyPlot.save('../data_out/plot/polynomialR2.pdf')
# plot validation
valiPlot = PlotHelper(['Grad', 'Wert'], fancy=True, pgf=True)
valiPlot.ax.plot(degrees, [r.deviation for r in vali_res], '--', color='silver', label=r'\O Abweichung in $\%$')
valiPlot.ax.plot(degrees, [r.rmse for r in vali_res], '-', color='orange', label=r'$RMSE$')
valiPlot.ax.plot(degrees, [r.mae for r in vali_res], '-', color='green', label=r'$MAE$')
valiPlot.ax.plot(degrees, np.array([r.press for r in vali_res]) * 0.1, 'k-', label=r'$PRESS * 0.1$')
valiPlot.finalize(width=4.5, height=3, legendLoc='upper left')
valiPlot.ax.set_ylim([0, 2.5])
polyPlot.save('../data_out/plot/polynomialR2vali.pdf')
valiPlot.show()