make_lsq_ndbspline#
- scipy.interpolate.make_lsq_ndbspline(x, y, t, k=3, *, w=None, grid=False, solver=None, **solver_args)[source]#
Construct a least-squares tensor-product B-spline.
- Parameters:
- xarray_like, shape (npts, ndim), or tuple of array_like
Coordinates of the data points. If
grid=False, each row contains one point inndimdimensions. Ifgrid=True, x is a tuple of one-dimensional coordinate arrays with shapes(m1,), ..., (mN,)defining a rectilinear grid.- yarray_like, shape (npts, …) or (m1, …, mN, …)
Data values at x. If
grid=True, the leading dimensions must match the lengths of the grid axes. Trailing dimensions are treated as separate outputs and are fitted using the same design matrix.- ttuple of ndim array_like
Full knot vectors for each dimension, including boundary knots.
- kint or array_like, shape (ndim,), optional
Spline degree in each dimension. A scalar applies the same degree to every dimension. Default is
3.- warray_like, shape (npts,) or (m1, …, mN), optional
Non-negative residual weights. If
grid=True, the shape must match the grid. Zero-weighted data points do not contribute to the fit. By default, all data points have equal weight.- gridbool, optional
If False (default), x contains scattered coordinate rows. If True, x contains the one-dimensional axes of a complete rectilinear grid.
- solvercallable, optional
Sparse least-squares solver. By default,
scipy.sparse.linalg.lsqris currently used. The solver must accept a sparse design matrix and one-dimensional right-hand side and return a result with the coefficients and termination status in its first two entries. Status values0,1, and2indicate successful termination.- **solver_args
Additional keyword arguments passed to solver.
- Returns:
- splNdBSpline
Tensor-product B-spline with coefficients fitted by least squares.
See also
NdBSplineTensor-product B-spline representation and evaluation.
make_lsq_splineConstruct a least-squares B-spline in one dimension.
RegularGridInterpolatorInterpolate data on a rectilinear grid.
Notes
Added in version 2.0.0.
Let
A[i, j]be the value of thej-th tensor-product basis function evaluated at thei-th data pointx[i], and letW = diag(w). This function solves\[\min_c \lVert W (A c - y) \rVert_2.\]Thus, w multiplies the residual directly; the corresponding squared objective contains
w[i]**2. The design matrix is sparse because each B-spline basis function has local support.Every tensor-product basis function must be supported by at least one data point with positive weight, and there must be at least as many data points as spline coefficients. Violating either condition makes the design matrix rank-deficient, so the coefficients cannot be determined uniquely and a ValueError is raised. These conditions are necessary, but do not by themselves guarantee that the design matrix has full rank. Coordinates in dimension
dmust lie in the base interval. Withgrid=False, this meanst[d][k[d]] <= x[:, d] <= t[d][-k[d] - 1]; withgrid=True, it meanst[d][k[d]] <= x[d] <= t[d][-k[d] - 1].With
grid=True, the grid is converted to scattered coordinate rows internally. Both input forms therefore have the same least-squares and solver semantics.Examples
Fit a linear function to scattered two-dimensional samples. For a linear spline, each boundary is repeated twice in the full knot vectors.
>>> import numpy as np >>> from scipy.interpolate import make_lsq_ndbspline >>> x = np.array([[0., 0.], [0., 1.], [1., 0.], ... [1., 1.], [0.5, 0.5]]) >>> y = x[:, 0] + 2*x[:, 1] >>> t = (np.array([0., 0., 1., 1.]), ... np.array([0., 0., 1., 1.])) >>> spl = make_lsq_ndbspline(x, y, t, k=1) >>> spl([[0.25, 0.75]]) array([1.75])
Fit data sampled on a complete rectilinear grid by passing its axes and setting
grid=True.>>> points = (np.linspace(0., 1., 4), np.linspace(-1., 1., 5)) >>> x0, x1 = np.meshgrid(*points, indexing="ij") >>> values = x0 + 2*x1 >>> t_grid = (np.array([0., 0., 1., 1.]), ... np.array([-1., -1., 1., 1.])) >>> spl = make_lsq_ndbspline( ... points, values, t_grid, k=1, grid=True ... ) >>> spl([[0.25, 0.5]]) array([1.25])