bundles / scipy latest / scipy / interpolate / _fitpack_repro / make_splrep
function
scipy.interpolate._fitpack_repro:make_splrep
Signature
def make_splrep ( x , y , * , w = None , xb = None , xe = None , k = 3 , s = 0 , t = None , nest = None , bc_type = None ) Summary
Create a smoothing B-spline function with bounded error, minimizing derivative jumps.
Extended Summary
Given the set of data points (x[i], y[i]), determine a smooth spline approximation of degree k on the interval xb <= x <= xe.
Parameters
x, y: array_like, shape (m,)The data points defining a curve
y = f(x).w: array_like, shape (m,), optionalStrictly positive 1D array of weights, of the same length as
xandy. The weights are used in computing the weighted least-squares spline fit. If the errors in the y values have standard-deviation given by the vectord, thenwshould be1/d. Default isnp.ones(m).xb, xe: float, optionalThe interval to fit. If None, these default to
x[0]andx[-1], respectively.k: int, optionalThe degree of the spline fit. It is recommended to use cubic splines,
k=3, which is the default. Even values ofkshould be avoided, especially with smallsvalues.s: float, optionalThe smoothing condition. The amount of smoothness is determined by satisfying the LSQ (least-squares) constraint
sum((w * (g(x) - y))**2 ) <= swhere
g(x)is the smoothed fit to(x, y). The user can usesto control the tradeoff between closeness to data and smoothness of fit. Largersmeans more smoothing while smaller values ofsindicate less smoothing. Recommended values ofsdepend on the weights,w. If the weights represent the inverse of the standard deviation ofy, then a goodsvalue should be found in the range(m-sqrt(2*m), m+sqrt(2*m))wheremis the number of datapoints inx,y, andw. Default iss = 0.0, i.e. interpolation.t: array_like, optionalThe spline knots. If None (default), the knots will be constructed automatically. There must be at least
2*k + 2and at mostm + k + 1knots.nest: int, optionalThe target length of the knot vector. Should be between
2*(k + 1)(the minimum number of knots for a degree-kspline), andm + k + 1(the number of knots of the interpolating spline). The actual number of knots returned by this routine may be slightly larger thannest. Default is None (no limit, add up tom + k + 1knots).periodic: bool, optionalIf True, data points are considered periodic with period
x[m-1]-x[0]and a smooth periodic spline approximation is returned. Values ofy[m-1]andw[m-1]are not used. The default is False, corresponding to boundary condition 'not-a-knot'.bc_type: str, optionalBoundary conditions. Default is
"not-a-knot". The following boundary conditions are recognized:"not-a-knot"(default): The first and second segments are the same polynomial. This is equivalent to havingbc_type=None."periodic": The values and the firstk-1derivatives at the ends are equivalent.
Returns
spl: a `BSpline` instanceFor
s=0,spl(x) == y. For non-zero values ofsthe spl represents the smoothed approximation to(x, y), generally with fewer knots.
Notes
This routine constructs the smoothing spline function, , to minimize the sum of jumps, , of the k-th derivative at the internal knots (), where
Specifically, the routine constructs the spline function which minimizes
provided that
where is the input parameter.
In other words, we balance maximizing the smoothness (measured as the jumps of the derivative, the first criterion), and the deviation of from the data (the second criterion).
Note that the summation in the second criterion is over all data points, and in the first criterion it is over the internal spline knots (i.e. those with xb < t[i] < xe). The spline knots are in general a subset of data, see generate_knots for details.
Also note the difference of this routine to make_lsq_spline: the latter routine does not consider smoothness and simply solves a least-squares problem
for a spline function with a _fixed_ knot vector t.
Array API Standard Support
make_splrep 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 ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
See also
- generate_knots
is used under the hood for generating the knots
- make_interp_spline
construct an interpolating spline (
s = 0)- make_lsq_spline
construct the least-squares spline given the knot vector
- make_splprep
the analog of this routine for parametric curves
- splrep
a FITPACK analog of this routine
Aliases
-
scipy.interpolate.make_splrep