VOOZH about

URL: https://www.geeksforgeeks.org/data-analysis/b-splines-using-scipy/

⇱ B-Splines using Scipy - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

B-Splines using Scipy

Last Updated : 4 Jul, 2025

B-splines, or basis splines, are an important tool in numerical analysis and computer graphics for curve fitting and data smoothing. They offer a flexible way to represent curves and surfaces through piecewise polynomial functions.

What are B-Splines?

A B-spline is a type of spline function that provides minimal support with respect to a given degree, smoothness, and domain partition. In simpler terms, they are piecewise polynomial functions defined over a sequence of intervals known as knots.

Mathematical Foundation of B-Splines

B-splines are defined by their degree n, a set of control points, and a knot vector. The degree of the spline determines the degree of the polynomial pieces that make up the spline. The knot vector is a sequence of parameter values that determine where and how the control points affect the B-spline curve.

The general form of a B-spline can be expressed as:

where

  • are the B-spline basis functions of degree
  • is the knot vector
  • are the coefficients.

Characteristics of B-Splines

  • Local Control: Changes to one part of a B-spline curve do not affect the entire curve. This property is due to the local support nature of B-spline basis functions.
  • Smoothness: The smoothness of a B-spline is determined by its degree and the multiplicity of its knots. For instance, cubic B-splines (k=3) provide continuous first and second derivatives.
  • Flexibility: B-splines can represent complex shapes with fewer control points compared to other types of splines.

Implementing B-Splines with SciPy

Python's SciPy library provides robust tools for working with B-splines. Here, we explore how to create and manipulate B-splines using SciPy's interpolate module.

To create a B-spline in SciPy, you need to define your knot vector, coefficients, and spline degree. Here's an example:

Output:

👁 b-spline
B-Splines with SciPy

This code snippet demonstrates how to define a simple quadratic B-spline using SciPy's BSpline class.

Evaluating and Visualizing B-Splines

To evaluate a spline at given points or visualize it:

  • Use splev for evaluating splines at specific points.
  • Use splrep to find the spline representation of data.

Here's an example using splrep and splev:

Output:

👁 b-spline
B-Splines with SciPy

This example shows how to interpolate data using cubic splines.

Advanced Topics in B-Splines

1. Parametric Representation

In some cases, it's beneficial to represent curves parametrically using arc-length parameterization. This approach ensures uniform sampling along the curve's length:

Output:

👁 b-spline
B-Splines with SciPy

This example demonstrates how to create parametric splines using equal arc-length intervals.

2. Smoothing Splines

Smoothing splines are used when you want to fit a curve that balances between fitting the data closely and maintaining smoothness:

Output:

👁 b-spline
B-Splines with SciPy

This code illustrates how smoothing splines can be used for noise reduction while fitting data smoothly.

Applications of B-Splines

B-splines have numerous applications across various domains:

  • Data Smoothing: They can smooth noisy data while preserving essential trends.
  • Curve Fitting: Used in computer graphics for modeling complex shapes with precision.
  • Feature Selection: In machine learning for dimensionality reduction by capturing essential data patterns
Comment