1-Dimensional Least Squares FitΒΆ

1d lsq
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),
18           t,
19           (x[-1],)*(k+1)]
20
21 ndspl = ndsplines.make_lsq_spline(x[:, None], y[:, None], [t], np.array([k]))
22 ispl = interpolate.make_lsq_spline(x, y, t, k)
23
24 xs = np.linspace(-3, 3, 100)
25 plt.figure()
26 plt.plot(x, y, 'o', ms=5)
27 plt.plot(xs, ndspl(xs).squeeze(), label='LSQ ND spline')
28 plt.plot(xs, ispl(xs), '--', label='LSQ scipy.interpolate spline')
29 plt.legend(loc='best')
30 plt.show()
31
32 print("Computed coefficients close?", np.allclose(ndspl.coefficients, ispl.c))

Total running time of the script: (0 minutes 0.152 seconds)

Gallery generated by Sphinx-Gallery