From 1de9034954a3fd8c815e3d9f578576e58b6765a0 Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Mon, 29 Jul 2019 14:00:42 +0200 Subject: [PATCH] add simple visualisation of RBF for the bachelor thesis --- rbf-vis.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 rbf-vis.py diff --git a/rbf-vis.py b/rbf-vis.py new file mode 100644 index 0000000..b3a6046 --- /dev/null +++ b/rbf-vis.py @@ -0,0 +1,59 @@ +import numpy as np + +from matplotlib import pyplot as plt + +np.random.seed(15) + + +def theta(r): + return np.exp(-r ** 2) + + +points = [0, 3, 5] +values = [0.2, 0.8, 0.1] + +# points = np.random.rand(15) * 5 +# values = np.sin(points) + +plt.scatter(points, values, label="data points") + +subtract = np.zeros((len(points), len(points))) + +for i in range(len(points)): + for j in range(len(points)): + subtract[i][j] = abs(points[i] - points[j]) + +print(subtract) + +left_side = theta(subtract) +print(left_side) + +lambdas = np.linalg.solve(left_side, values) + +print(lambdas) + + +def s(x, i=None): + running_sum = 0 + for index, lam in enumerate(lambdas): + if i is None or i == index: + running_sum += lam * theta(x - points[index]) + return running_sum + + +x = np.linspace(0, 5, 100) +y = s(x) + +plt.plot(x, y, label="$s(x)$", zorder=-1) + +for i in range(len(points)): + plt.plot(x, s(x, i), label=f"RBF {i}",zorder=-2) +plt.legend() + +# x_interpol = 2.3 +# y_interpol = s(x_interpol) +# plt.scatter(x_interpol,y_interpol,color="green") + +plt.savefig("../arbeit/images/rbf1.pdf") +# plt.savefig("../arbeit/images/rbf2.pdf") +plt.show()