Smooth noisy observations
You have noisy one-dimensional measurements and need a continuous curve that suppresses measurement noise.
SciPy · Function
Fits a one-dimensional smoothing spline to ordered x-y samples. The smoothing factor s balances fidelity against smoothness, while k selects the spline degree.
The constructor builds a degree-k spline through ordered samples, selecting knots so the weighted squared residual meets the smoothing condition controlled by s. The resulting object is callable, so passing scalar or array-like inputs evaluates the fitted spline.
scipy.interpolate.UnivariateSpline(x, y, w=None, bbox=[None, None], k=3, s=None, ext=0, check_finite=False)| Name | Type | Description |
|---|---|---|
x | (N,) array_like | 1-D array of independent input data; it must be increasing and strictly increasing when s is 0. |
y | (N,) array_like | 1-D dependent input data with the same length as x. |
w | (N,) array_like, optional | Positive fitting weights; None assigns equal weights. |
bbox | (2,) array_like, optional | Two-element boundary of the approximation interval; None uses [x[0], x[-1]]. |
k | int, optional | Degree of the smoothing spline, from 1 through 5; default 3. |
s | float or None, optional | Positive smoothing factor controlling the number of knots and fit smoothness; default None. |
ext | int or str, optional | Controls extrapolation behavior outside the interval: extrapolate, return boundary value, return NaN, or raise an error. |
check_finite | bool, optional | Whether to check input arrays for finite values; default False. |
A callable UnivariateSpline object representing the fitted spline.
import numpy as np
from scipy.interpolate import UnivariateSpline
x = np.linspace(0, 2 * np.pi, 12)
y = np.sin(x) + 0.08 * np.cos(7 * x)
spline = UnivariateSpline(x, y, s=0.2)
x_new = np.linspace(x[0], x[-1], 100)
y_new = spline(x_new)
print(y_new[:3])Fits a smoothing spline to noisy samples and evaluates it on a denser grid.
You have noisy one-dimensional measurements and need a continuous curve that suppresses measurement noise.
You need values between sampled coordinates on a one-dimensional domain.
Different observations have different reliability or measurement uncertainty.
UnivariateSpline fits a one-dimensional smoothing spline to x and y data. The degree is controlled by k, and s controls the residual-based smoothing condition used to choose knots.
Follow-up: How does s affect the result? A larger s permits a larger weighted squared residual and therefore generally produces a smoother fit; a smaller s produces a closer fit.
x must be increasing, while s=0 additionally requires strictly increasing x. y must have the same length as x, and optional weights must be positive.
Follow-up: Why is the strict requirement relevant? With s=0, repeated x coordinates would make exact interpolation ambiguous.
The returned object represents the fitted spline and can be called with evaluation points to obtain spline values. Its default degree is k=3, and the allowed degree range is 1 through 5.
Follow-up: How could the fit be evaluated densely? Create a new grid over the approximation interval and pass that array to the spline object.
BSplineLSQUnivariateSplineInterpolatedUnivariateSplinemake_splrepsplev