scipy.interpolate.

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 in ndim dimensions. If grid=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.lsqr is 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 values 0, 1, and 2 indicate successful termination.

**solver_args

Additional keyword arguments passed to solver.

Returns:
splNdBSpline

Tensor-product B-spline with coefficients fitted by least squares.

See also

NdBSpline

Tensor-product B-spline representation and evaluation.

make_lsq_spline

Construct a least-squares B-spline in one dimension.

RegularGridInterpolator

Interpolate data on a rectilinear grid.

Notes

Added in version 2.0.0.

Let A[i, j] be the value of the j-th tensor-product basis function evaluated at the i-th data point x[i], and let W = 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 d must lie in the base interval. With grid=False, this means t[d][k[d]] <= x[:, d] <= t[d][-k[d] - 1]; with grid=True, it means t[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])