0
0
SciPydata~20 mins

UnivariateSpline in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UnivariateSpline Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Spline Evaluation
What is the output of the following code snippet that fits a UnivariateSpline and evaluates it at x=2.5?
SciPy
import numpy as np
from scipy.interpolate import UnivariateSpline

x = np.array([1, 2, 3, 4, 5])
y = np.array([1, 4, 9, 16, 25])
spline = UnivariateSpline(x, y, s=0)
result = spline(2.5)
print(round(result, 2))
A9.0
B7.0
C6.25
D8.5
Attempts:
2 left
💡 Hint
Think about the shape of y = x^2 and how spline fits exactly with s=0.
data_output
intermediate
2:00remaining
Number of Knots in Spline
After fitting a UnivariateSpline with smoothing factor s=1 on the data below, how many knots does the spline have?
SciPy
import numpy as np
from scipy.interpolate import UnivariateSpline

x = np.linspace(0, 10, 11)
y = np.sin(x)
spline = UnivariateSpline(x, y, s=1)
knots_count = len(spline.get_knots())
print(knots_count)
A7
B11
C5
D9
Attempts:
2 left
💡 Hint
Smoothing reduces the number of knots compared to the number of data points.
🔧 Debug
advanced
2:00remaining
Error Raised by Incorrect Input
What error does the following code raise when trying to fit a UnivariateSpline?
SciPy
import numpy as np
from scipy.interpolate import UnivariateSpline

x = np.array([1, 2, 2, 4, 5])
y = np.array([1, 4, 9, 16, 25])
spline = UnivariateSpline(x, y, s=0)
AValueError: x must be strictly increasing
BTypeError: unsupported operand type(s) for -: 'list' and 'int'
CIndexError: index out of range
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
Check the x array for duplicates or ordering.
🚀 Application
advanced
2:00remaining
Choosing Smoothing Factor for Noisy Data
You have noisy data points and want to fit a smooth curve using UnivariateSpline. Which smoothing factor s is most appropriate to reduce noise but keep the trend?
As = 0 (no smoothing, exact fit)
Bs = large positive value (more smoothing)
Cs = negative value
Ds = None (default)
Attempts:
2 left
💡 Hint
More smoothing means less sensitivity to noise.
🧠 Conceptual
expert
2:00remaining
Effect of Spline Degree on Fit
How does increasing the degree k of a UnivariateSpline from 1 to 3 affect the fitted curve?
AThe spline becomes smoother and can model curvature better
BThe spline becomes less smooth and more linear
CThe spline ignores data points and fits a constant line
DThe spline raises an error because degree must be 1
Attempts:
2 left
💡 Hint
Higher degree splines can model curves better than linear splines.