spl = LSQUnivariateSpline(x1, y1, t1[1:-1], bbox = [t1[0], t1[-1]]) spl = LSQUnivariateSpline(x1, y1, t2[1:-3], bbox = [t2[0], t2[-3]]) coeffs = np.empty(len(t)+2, dtype="f") coeffs.fill(np.nan) if np.all(m1): coeffs[:] = spl.get_coeffs() else: coeffs[1:-1][m1] = spl.get_coeffs()[1:-1] coeffs_list.append([coeffs]) print len(spl.get_knots()), len(spl.get_coeffs()) plot(x1, y1) plot(xx, spl(xx)) aa = np.concatenate(coeffs_list, axis=0) aaf = np.empty_like(aa) tt = np.arange(aa.shape[0]) for ix in range(aa.shape[-1]): y_data = aa[:,ix] my = np.isfinite(y_data) p = np.polyfit(tt[my], y_data[my], 4) p = polyfitr.polyfitr(tt[my], y_data[my], 4, 2, fev=100, w=None, diag=False, clip='both',
spl = LSQUnivariateSpline(x1, y1, t1[1:-1], bbox = [t1[0], t1[-1]]) spl = LSQUnivariateSpline(x1, y1, t2[1:-3], bbox = [t2[0], t2[-3]]) coeffs = np.empty(len(t)+2, dtype="f") coeffs.fill(np.nan) if np.all(m1): coeffs[:] = spl.get_coeffs() else: coeffs[1:-1][m1] = spl.get_coeffs()[1:-1] coeffs_list.append([coeffs]) print len(spl.get_knots()), len(spl.get_coeffs()) plot(x1, y1) plot(xx, spl(xx)) aa = np.concatenate(coeffs_list, axis=0) aaf = np.empty_like(aa) tt = np.arange(aa.shape[0]) for ix in range(aa.shape[-1]): y_data = aa[:,ix] my = np.isfinite(y_data) p = np.polyfit(tt[my], y_data[my], 4) p = polyfitr.polyfitr(tt[my], y_data[my], 4, 2, fev=100, w=None, diag=False, clip='both',
from scipy.interpolate import LSQUnivariateSpline, UnivariateSpline import matplotlib.pyplot as plt x = np.linspace(-3, 3, 50) y = np.exp(-x**2) + 0.1 * np.random.randn(50) # Fit a smoothing spline with a pre-defined internal knots: t = [-1, 0, 1] spl = LSQUnivariateSpline(x, y, t) xs = np.linspace(-3, 3, 1000) plt.plot(x, y, 'ro', ms=5) plt.plot(xs, spl(xs), 'g-', lw=3) plt.show() # Check the knot vector: spl.get_knots() # array([-3., -1., 0., 1., 3.]) # Constructing lsq spline using the knots from another spline: x = np.arange(10) s = UnivariateSpline(x, x, s=0) s.get_knots() # array([ 0., 2., 3., 4., 5., 6., 7., 9.]) knt = s.get_knots() s1 = LSQUnivariateSpline(x, x, knt[1:-1]) # Chop 1st and last knot s1.get_knots() # array([ 0., 2., 3., 4., 5., 6., 7., 9.])