0
0
SciPydata~10 mins

Spline interpolation in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Spline interpolation
Input data points (x, y)
Choose spline type (e.g., cubic)
Fit spline to data
Evaluate spline at new points
Output smooth interpolated values
Spline interpolation fits a smooth curve through given points by choosing a spline type, fitting it, then evaluating at new points.
Execution Sample
SciPy
import numpy as np
from scipy.interpolate import CubicSpline

x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 1, 0, 1, 0])
cs = CubicSpline(x, y)
x_new = np.linspace(0, 4, 9)
y_new = cs(x_new)
This code fits a cubic spline to 5 points and evaluates it at 9 new points between 0 and 4.
Execution Table
StepActionInput/VariableResult/Output
1Define original x pointsx = [0,1,2,3,4]Array x created
2Define original y pointsy = [0,1,0,1,0]Array y created
3Create CubicSpline objectcs = CubicSpline(x, y)Spline fitted to points
4Create new x points for evaluationx_new = np.linspace(0,4,9)x_new = [0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0]
5Evaluate spline at x_newy_new = cs(x_new)y_new = [0.0,0.6875,1.0,0.6875,0.0,0.3125,1.0,0.6875,0.0]
💡 All new points evaluated, spline interpolation complete
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
xundefined[0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4]
yundefinedundefined[0,1,0,1,0][0,1,0,1,0][0,1,0,1,0][0,1,0,1,0]
csundefinedundefinedundefinedCubicSpline objectCubicSpline objectCubicSpline object
x_newundefinedundefinedundefinedundefined[0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0][0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0]
y_newundefinedundefinedundefinedundefinedundefined[0.0,0.6875,1.0,0.6875,0.0,0.3125,1.0,0.6875,0.0]
Key Moments - 3 Insights
Why does the spline output y_new have values between the original y points?
Because spline interpolation fits a smooth curve, it estimates values between points, not just the original y values. See execution_table step 5 where y_new has intermediate values.
What happens if we evaluate the spline outside the original x range?
By default, CubicSpline extrapolates outside the original x range, which can give unexpected values. This is not shown here but important to know.
Why do we use np.linspace for x_new instead of just original x?
np.linspace creates evenly spaced points to see the smooth curve between original points, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 5, what is the interpolated y value at x_new = 2.0?
A0.6875
B1.0
C0.0
D0.3125
💡 Hint
Check the y_new array in step 5 of execution_table; the value at index corresponding to x_new=2.0 is 0.0
At which step is the spline function created and fitted to the data?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the Action column in execution_table; step 3 shows creation of CubicSpline object
If we change x_new to have fewer points, how does y_new change?
Ay_new will have more values
By_new will have fewer values
Cy_new values will be unchanged
Dy_new will be empty
💡 Hint
Refer to variable_tracker for x_new and y_new sizes; y_new matches length of x_new
Concept Snapshot
Spline interpolation fits a smooth curve through data points.
Use scipy.interpolate.CubicSpline(x, y) to create spline.
Evaluate spline at new points with cs(x_new).
Output is smooth y values between original points.
Useful for smooth data estimation and plotting.
Full Transcript
Spline interpolation is a way to draw a smooth curve through given points. We start with original x and y points. Then we create a cubic spline object using scipy's CubicSpline. This fits a smooth curve through the points. Next, we create new x points where we want to estimate y values. Finally, we evaluate the spline at these new points to get smooth y values. This helps us see smooth trends between data points instead of just dots.