0
0
SciPydata~10 mins

UnivariateSpline in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - UnivariateSpline
Input data points x, y
Create UnivariateSpline object
Fit spline curve to data
Evaluate spline at new points
Get smooth curve output
We start with data points, create a spline object that fits a smooth curve, then evaluate it to get smooth values.
Execution Sample
SciPy
import numpy as np
from scipy.interpolate import UnivariateSpline

x = np.linspace(0, 10, 10)
y = np.sin(x)
spline = UnivariateSpline(x, y, s=1)
y_smooth = spline(np.linspace(0, 10, 100))
This code fits a smooth spline to sine data and evaluates it at 100 points for a smooth curve.
Execution Table
StepActionInput/ConditionResult/Output
1Generate x pointsnp.linspace(0,10,10)[0.0, 1.11, ..., 10.0] (10 points)
2Calculate y = sin(x)sin(x)[0.0, 0.9, ..., -0.54] (10 points)
3Create UnivariateSplinex, y, s=1Spline object created, smoothing applied
4Evaluate spline at 100 pointsnp.linspace(0,10,100)Array of 100 smooth y values
5Output smooth curvey_smooth arraySmooth sine curve values for plotting
💡 All steps complete, spline fitted and evaluated successfully
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
xNone[0.0, 1.11, ..., 10.0][0.0, 1.11, ..., 10.0][0.0, 1.11, ..., 10.0][0.0, 1.11, ..., 10.0][0.0, 1.11, ..., 10.0]
yNoneNone[0.0, 0.9, ..., -0.54][0.0, 0.9, ..., -0.54][0.0, 0.9, ..., -0.54][0.0, 0.9, ..., -0.54]
splineNoneNoneNoneSpline objectSpline objectSpline object
y_smoothNoneNoneNoneNone[smooth values][smooth values]
Key Moments - 3 Insights
Why do we use the parameter s=1 when creating UnivariateSpline?
The s parameter controls smoothing. Setting s=1 allows some smoothing to avoid fitting noise exactly. See execution_table step 3 where spline is created with s=1.
What happens if we evaluate the spline at points outside the original x range?
Evaluating outside original x can extrapolate but may be unreliable. Our example evaluates within range (step 4).
Why do we use more points (100) to evaluate the spline than original data points (10)?
Using more points creates a smooth curve for plotting, showing the spline's shape clearly (step 4 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of step 2?
ASpline object created
BArray of 10 sine values corresponding to x
CArray of 100 smooth y values
DInput x points
💡 Hint
Check the 'Result/Output' column for step 2 in execution_table
At which step is the smoothing parameter s=1 applied?
AStep 4
BStep 1
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Input/Condition' columns in execution_table step 3
If we change the number of evaluation points from 100 to 50, what changes in variable_tracker?
Ay_smooth array length changes to 50
Bx array length changes to 50
CSpline object changes
DOriginal y values change
💡 Hint
Check the 'y_smooth' variable in variable_tracker after step 4
Concept Snapshot
UnivariateSpline fits a smooth curve through data points.
Syntax: UnivariateSpline(x, y, s=smoothing_factor)
's' controls smoothness; s=0 fits all points exactly.
Evaluate spline by calling it with new x values.
Useful for smoothing noisy data and interpolation.
Full Transcript
We start with input data points x and y. Then we create a UnivariateSpline object with smoothing parameter s=1 to fit a smooth curve. Next, we evaluate this spline at many points to get a smooth output curve. Variables x and y hold original data, spline holds the fitted model, and y_smooth holds the smooth curve values. Key points include understanding smoothing parameter s, evaluating spline within data range, and using more points for smooth plotting.