📖 Scenario: You are working with a set of 2D points representing a path on a map. You want to create a smooth curve that passes through these points to better visualize the route.
🎯 Goal: Build a parametric interpolation using SciPy to create smooth x and y coordinates from given points, then plot the smooth curve.
📋 What You'll Learn
Create two lists called x_points and y_points with given coordinates
Create a parameter array t for the points
Use scipy.interpolate.interp1d to create interpolation functions for x and y
Generate new parameter values t_new for smooth interpolation
Calculate interpolated x and y values using the interpolation functions
Print the interpolated x and y arrays
💡 Why This Matters
🌍 Real World
Parametric interpolation is used in mapping, animation, and robotics to create smooth paths from discrete points.
💼 Career
Data scientists and engineers use interpolation to fill missing data, smooth signals, and model continuous phenomena from samples.
Progress0 / 4 steps
1
Create the original points
Create two lists called x_points and y_points with these exact values: x_points = [0, 1, 2, 3, 4] and y_points = [0, 1, 0, 1, 0].
SciPy
Hint
Use square brackets to create lists with the exact numbers given.
2
Create the parameter array
Import numpy as np and create a numpy array called t with values from 0 to 4 (inclusive) matching the length of x_points.
SciPy
Hint
Use np.arange(5) to create an array from 0 to 4.
3
Create interpolation functions
Import interp1d from scipy.interpolate. Then create two interpolation functions called interp_x and interp_y using interp1d with t and x_points, and t and y_points respectively. Use kind='cubic' for smooth curves.
SciPy
Hint
Use interp1d with kind='cubic' to create smooth interpolation functions.
4
Generate smooth points and print
Create a numpy array t_new with 50 evenly spaced values from 0 to 4 using np.linspace. Then calculate x_smooth and y_smooth by passing t_new to interp_x and interp_y. Finally, print x_smooth and y_smooth.
SciPy
Hint
Use np.linspace(0, 4, 50) to create t_new. Then call interp_x(t_new) and interp_y(t_new). Finally, print both results.
Practice
(1/5)
1. What is the main purpose of parametric interpolation in data science?
easy
A. To create smooth curves where both x and y depend on a parameter
Parametric interpolation creates smooth curves by defining both x and y as functions of a parameter, usually denoted t.
Step 2: Compare with other options
Sorting, averaging, and finding maximum values are unrelated to creating smooth curves from points.
Final Answer:
To create smooth curves where both x and y depend on a parameter -> Option A
Quick Check:
Parametric interpolation = smooth curves with parameter [OK]
Hint: Parametric means both x and y depend on a parameter [OK]
Common Mistakes:
Confusing interpolation with sorting or averaging
Thinking only y depends on the parameter
Assuming parametric interpolation finds max or min values
2. Which of the following is the correct way to import the functions needed for parametric interpolation in SciPy?
easy
A. from scipy.interpolate import splprep, splev
B. import scipy.interpolate as spint
C. from scipy import interpolate
D. import splprep, splev from scipy.interpolate
Solution
Step 1: Recall correct import syntax
The correct syntax to import specific functions is 'from module import function1, function2'.
Step 2: Check each option
from scipy.interpolate import splprep, splev uses correct syntax. import scipy.interpolate as spint imports the module but not functions directly. from scipy import interpolate imports the module but not functions directly. import splprep, splev from scipy.interpolate has incorrect syntax.
Final Answer:
from scipy.interpolate import splprep, splev -> Option A
Quick Check:
Correct import uses 'from ... import ...' [OK]
Hint: Use 'from scipy.interpolate import splprep, splev' [OK]
Common Mistakes:
Using incorrect import syntax
Importing the whole module without functions
Trying to import functions with wrong order
3. What will be the output of the following code snippet?
import numpy as np
from scipy.interpolate import splprep, splev
x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 0, 1])
tck, u = splprep([x, y], s=0)
new_points = splev(np.linspace(0, 1, 3), tck)
print(np.round(new_points, 2))
medium
A. Error due to incorrect input arrays
B. [[0. 1. 2. ]
[0. 1. 0. ]]
C. [[0. 1.5 3. ]
[0. 0.5 1. ]]
D. [[0. 1.25 2.5 ]
[0. 0.75 0.5 ]]
Solution
Step 1: Understand splprep and splev usage
splprep creates a parametric spline for points (x, y). splev evaluates the spline at given parameter values.
Step 2: Evaluate splev at 3 points between 0 and 1
Using np.linspace(0,1,3) gives [0, 0.5, 1]. Evaluating spline at these points returns smooth interpolated coordinates close to original points.
Hint: Remember splev outputs arrays for x and y at given parameters [OK]
Common Mistakes:
Confusing input arrays with output
Expecting original points instead of interpolated
Misunderstanding parameter range for splev
4. Identify the error in the following code for parametric interpolation:
import numpy as np
from scipy.interpolate import splprep, splev
x = [0, 1, 2]
y = [0, 1]
tck, u = splprep([x, y], s=0)
new_points = splev(np.linspace(0, 1, 5), tck)
print(new_points)
medium
A. Missing import statement for numpy
B. x and y arrays have different lengths causing an error
C. Incorrect use of splev function parameters
D. No error, code runs fine
Solution
Step 1: Check input arrays length
x has length 3, y has length 2. splprep requires x and y to have the same length.
Step 2: Understand consequence of length mismatch
Different lengths cause splprep to raise an error because points must be paired.
Final Answer:
x and y arrays have different lengths causing an error -> Option B
Quick Check:
Input arrays must be same length [OK]
Hint: Check that x and y arrays have equal length before splprep [OK]
Common Mistakes:
Ignoring array length mismatch
Assuming splev parameters are wrong instead
Forgetting to import numpy (though it is imported here)
5. You have scattered points representing a closed shape. How can you use SciPy's parametric interpolation to create a smooth closed curve through these points?
hard
A. Use splprep with s>0 to ignore the closed shape
B. Use splev with s=0 to smooth the curve
C. Use splprep with parameter per=1 to indicate a periodic (closed) curve
D. Use numpy.linspace to close the curve manually
Solution
Step 1: Understand how to create closed curves with splprep
Setting per=1 in splprep tells it to create a periodic spline, which closes the curve smoothly.
Step 2: Evaluate options
splev evaluates the spline but does not control closure. s>0 smooths but does not close. numpy.linspace generates parameter values but does not close the curve.
Final Answer:
Use splprep with parameter per=1 to indicate a periodic (closed) curve -> Option C
Quick Check:
per=1 in splprep closes the curve [OK]
Hint: Set per=1 in splprep for closed curves [OK]
Common Mistakes:
Using splev or s parameter to close curve incorrectly
Not setting per=1 for closed shapes
Trying to close curve manually with linspace
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.