Spline Regression › Visualizing Spline Regression
Machine Learning from ScratchAll chapters Spline Regression › Visualizing Spline RegressionOne section · Sections in this chapter3 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's explore how spline regression works by visualizing the basis functions and comparing different spline fits to polynomial regression.




The visualization above shows several key aspects of spline regression. The first plot shows our synthetic data with a complex non-linear relationship. The second plot shows the B-spline basis functions, which are smooth, compactly supported functions that form the building blocks of our spline model. Notice how each basis function is non-zero only over a limited range, and they smoothly transition between segments.
The third plot compares different regression approaches, showing how spline regression can capture the underlying pattern more effectively than polynomial regression, especially with an appropriate number of knots. The fourth plot shows the residuals, revealing how spline regression can provide better fit quality with fewer artifacts compared to high-degree polynomials.
You can see the importance of knot selection. Too few knots (3) may underfit the data, while too many knots (7) might overfit. The optimal number of knots typically requires cross-validation to determine, balancing model complexity with generalization performance.