make_lsq_spline
- scipy.interpolate.make_lsq_spline(x, y, t, k=3, w=None, axis=0, check_finite=True, *, method='qr')
Create a smoothing B-spline satisfying the Least SQuares (LSQ) criterion.
The result is a linear combination
of the B-spline basis elements, , which minimizes
Abscissas.
Ordinates.
Knots. Knots and data points must satisfy Schoenberg-Whitney conditions.
B-spline degree. Default is cubic, k = 3.
Weights for spline fitting. Must be positive. If ,
then weights are all equal.
Default is .
The number of data points must be larger than the spline degree k.
Knots t must satisfy the Schoenberg-Whitney conditions,
i.e., there must be a subset of data points x[j] such that
t[j] < x[j] < t[j+k+1], for j=0, 1,...,n-k-2.
Array API Standard Support
make_lsq_spline has experimental support for Python Array API Standard compatible
backends in addition to NumPy. Please consider testing these features
by setting an environment variable SCIPY_ARRAY_API=1 and providing
CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following
combinations of backend and device (or other capability) are supported.
Library | CPU | GPU |
|---|---|---|
NumPy | ✅ | n/a |
CuPy | n/a | ⛔ |
PyTorch |
See Support for the array API standard for more information.
Generate some noisy data:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> rng = np.random.default_rng()
>>> x = np.linspace(-3
NoneInterpolation axis. Default is zero.
Whether to check that the input arrays contain only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs. Default is True.
Method for solving the linear LSQ problem. Allowed values are “norm-eq” (Explicitly construct and solve the normal system of equations), and “qr” (Use the QR factorization of the design matrix). Default is “qr”.
splrepa FITPACK-based fitting routine
✅ |
⛔ |
JAX | ⚠️ no JIT | ⛔ |
Dask | ⚠️ computes graph | n/a |
Now fit a smoothing cubic spline with a pre-defined internal knots. Here we make the knot vector (k+1)-regular by adding boundary knots:
>>> from scipy.interpolate import make_lsq_spline, BSpline
>>> t = [-1, 0, 1]
>>> k = 3
>>> t = np.r_[(x[0],)*(k+1),
... t,
... (x[-1],)*(k+1)]
>>> spl = make_lsq_spline(x, y, t, k)
For comparison, we also construct an interpolating spline for the same set of data:
>>> from scipy.interpolate import make_interp_spline
>>> spl_i = make_interp_spline(x, y)
Plot both:
>>> xs = np.linspace(-3, 3, 100)
>>> plt.plot(x, y, 'ro', ms=5)
>>> plt.plot(xs, spl(xs), 'g-', lw=3, label='LSQ spline')
>>> plt.plot(xs, spl_i(xs), 'b-', lw=3, alpha=0.7, label='interp spline')
>>> plt.legend(loc='best')
>>> plt.show()
NaN handling: If the input arrays contain nan values, the result is
not useful since the underlying spline fitting routines cannot deal with
nan. A workaround is to use zero weights for not-a-number data points:
>>> y[8] = np.nan
>>> w = np.isnan(y)
>>> y[w] = 0.
>>> tck = make_lsq_spline(x, y, t, w=~w)
Notice the need to replace a nan by a numerical value (precise value
does not matter as long as the corresponding weight is zero.)