Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

interpolate.UnivariateSpline

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.

How it works

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.

Syntax

scipy.interpolate.UnivariateSpline(x, y, w=None, bbox=[None, None], k=3, s=None, ext=0, check_finite=False)

Parameters

NameTypeDescription
x(N,) array_like1-D array of independent input data; it must be increasing and strictly increasing when s is 0.
y(N,) array_like1-D dependent input data with the same length as x.
w(N,) array_like, optionalPositive fitting weights; None assigns equal weights.
bbox(2,) array_like, optionalTwo-element boundary of the approximation interval; None uses [x[0], x[-1]].
kint, optionalDegree of the smoothing spline, from 1 through 5; default 3.
sfloat or None, optionalPositive smoothing factor controlling the number of knots and fit smoothness; default None.
extint or str, optionalControls extrapolation behavior outside the interval: extrapolate, return boundary value, return NaN, or raise an error.
check_finitebool, optionalWhether to check input arrays for finite values; default False.

Returns

A callable UnivariateSpline object representing the fitted spline.

Key points

  • Use this class to fit a one-dimensional smoothing spline to sampled data.
  • The fitted curve has degree k and is determined from the supplied x and y arrays.
  • The algorithm adds knots until its residual-based smoothing condition is met.
  • The weighted squared residual is constrained to be no greater than s.
  • Omitting w gives every observation equal weight.
  • The default cubic degree is k=3.

Common mistakes

  • The x values must be increasing, and duplicates are not allowed when using interpolation mode with s=0.
  • Every supplied weight must be positive.
  • Keep the spline degree between 1 and 5; the default k=3 is cubic.
  • Increasing s smooths more aggressively, while decreasing it follows the samples more closely.

Example: Smooth and evaluate sampled data

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.

When to use it

Smooth noisy observations

You have noisy one-dimensional measurements and need a continuous curve that suppresses measurement noise.

Evaluate a continuous interpolant

You need values between sampled coordinates on a one-dimensional domain.

Weight observations during fitting

Different observations have different reliability or measurement uncertainty.

Interview questions

QWhat does UnivariateSpline construct, and what are k and s for?

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.

QWhat input constraints must be checked before fitting?

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.

QHow is the fitted object used after construction?

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.

  • BSpline
  • LSQUnivariateSpline
  • InterpolatedUnivariateSpline
  • make_splrep
  • splev

Source: scipy.interpolate.UnivariateSpline. Checked 2026-10-15.