bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / interpolate / _bsplines / make_lsq_spline
function
scipy.interpolate._bsplines:make_lsq_spline
source: build-install/usr/lib/python3.14/site-packages/scipy/interpolate/_bsplines.py:2187
Signature
def make_lsq_spline(x, y, t, k = 3, w = None, axis = 0, check_finite = True, *, method = qr, clamp_values = None)Summary
Create a smoothing B-spline satisfying the Least SQuares (LSQ) criterion.
Extended Summary
The result is a linear combination
of the B-spline basis elements, , which minimizes
Parameters
x: array_like, shape (m,)Abscissas.
y: array_like, shape (m, ...)Ordinates.
t: array_like, shape (n + k + 1,)Knots. Knots and data points must satisfy Schoenberg-Whitney conditions.
k: int, optionalB-spline degree. Default is cubic,
k = 3.w: array_like, shape (m,), optionalWeights for spline fitting. Must be positive. If
None, then weights are all equal. Default isNone.axis: int, optionalInterpolation axis. Default is zero.
check_finite: bool, optionalWhether 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: str, optionalMethod 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".
clamp_values: tuple, optionalA 2-tuple
(ci, cf)where each element is either a real number, a numeric array orNone. Pins the spline's value atx[0]tociand atx[-1]tocf.Noneleaves that endpoint unclamped. For example,(5, None)clampsx[0]to5and leavesx[-1]free. Requires the knot vector to have multiplicityk + 1located exactly at the clamped endpoint(s) and be equal tox[0]andx[-1]. Default is None.
Returns
b: `BSpline` objectA BSpline object of the degree
kwith knotst.
Notes
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.
When clamp_values is supplied, the knot vector must additionally have multiplicity k + 1 at the endpoint(s) being clamped, i.e. t[0] == t[1] == ... == t[k] for the left endpoint and/or t[-1] == t[-2] == ... == t[-(k+1)] for the right endpoint. This holds for the standard clamped knot vector construction as well as other constructions with the same boundary multiplicity, such as not-a-knot boundary conditions.
Examples
Generate some noisy data:import numpy as np import matplotlib.pyplot as plt rng = np.random.default_rng() x = np.linspace(-3, 3, 50) y = np.exp(-x**2) + 0.1 * rng.standard_normal(50)✓
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)✓
from scipy.interpolate import make_interp_spline spl_i = make_interp_spline(x, y)✓
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')✗
<matplotlib.legend.Legend object at 0x7f3f60b834d0>
plt.show()
✓
y[8] = np.nan w = np.isnan(y) y[w] = 0. tck = make_lsq_spline(x, y, t, w=~w)✓
See also
- BSpline
base class representing the B-spline objects
- LSQUnivariateSpline
a FITPACK-based spline fitting routine
- make_interp_spline
a similar factory function for interpolating splines
- splrep
a FITPACK-based fitting routine
Aliases
scipy.interpolate.make_lsq_spline