Note
Go to the end to download the full example code.
1-Dimensional Least Squares FitΒΆ

Computed coefficients close? False
7 import ndsplines
8 import matplotlib.pyplot as plt
9 import numpy as np
10 from scipy import interpolate
11
12 x = np.linspace(-3, 3, 50)
13 y = np.exp(-(x**2)) + 0.1 * np.random.randn(50)
14
15 t = [-1, 0, 1]
16 k = 3
17 t = np.r_[(x[0],) * (k + 1), t, (x[-1],) * (k + 1)]
18
19 ndspl = ndsplines.make_lsq_spline(x[:, None], y[:, None], [t], np.array([k]))
20 ispl = interpolate.make_lsq_spline(x, y, t, k)
21
22 xs = np.linspace(-3, 3, 100)
23 plt.figure()
24 plt.plot(x, y, "o", ms=5)
25 plt.plot(xs, ndspl(xs).squeeze(), label="LSQ ND spline")
26 plt.plot(xs, ispl(xs), "--", label="LSQ scipy.interpolate spline")
27 plt.legend(loc="best")
28 plt.show()
29
30 print("Computed coefficients close?", np.allclose(ndspl.coefficients, ispl.c))
Total running time of the script: (0 minutes 0.143 seconds)