0
0
SciPydata~20 mins

Spline interpolation in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spline Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of cubic spline interpolation at given points
Given the code below using SciPy's CubicSpline, what is the output array printed?
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)
print(cs([1.5, 2.5, 3.5]))
A[0.75 0.75 0.25]
B[1.0 0.0 1.0]
C[0.5 0.5 0.5]
D[0.6875 0.6875 0.3125]
Attempts:
2 left
💡 Hint
Think about how cubic splines smoothly connect points and evaluate at intermediate x values.
data_output
intermediate
1:30remaining
Number of points in spline evaluation output
Using the following code, how many points are in the output array from the spline evaluation?
SciPy
import numpy as np
from scipy.interpolate import CubicSpline

x = np.linspace(0, 10, 6)
y = np.sin(x)
cs = CubicSpline(x, y)
result = cs(np.linspace(0, 10, 50))
print(len(result))
A6
B60
C50
D10
Attempts:
2 left
💡 Hint
The output length matches the number of points you evaluate the spline at.
🔧 Debug
advanced
1:30remaining
Identify the error in spline interpolation code
What error does the following code raise when run?
SciPy
import numpy as np
from scipy.interpolate import CubicSpline

x = np.array([0, 1, 2, 3])
y = np.array([1, 2])
cs = CubicSpline(x, y)
AValueError: x and y must have the same length
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CIndexError: index out of range
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
Check if the input arrays x and y have matching lengths.
🧠 Conceptual
advanced
1:30remaining
Effect of boundary conditions in spline interpolation
Which boundary condition in CubicSpline ensures that the second derivative at the endpoints is zero (natural spline)?
Abc_type='not-a-knot'
Bbc_type='natural'
Cbc_type='clamped'
Dbc_type='periodic'
Attempts:
2 left
💡 Hint
Natural splines have zero curvature at the ends.
🚀 Application
expert
2:00remaining
Choosing spline type for periodic data
You have temperature data measured hourly over 24 hours and want to interpolate smoothly assuming the temperature at hour 0 equals hour 24. Which CubicSpline boundary condition should you use?
Abc_type='periodic'
Bbc_type='natural'
Cbc_type='clamped'
Dbc_type='not-a-knot'
Attempts:
2 left
💡 Hint
Periodic data means the start and end points match in value and derivatives.