Challenge - 5 Problems
UnivariateSpline Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
Think about the shape of y = x^2 and how spline fits exactly with s=0.
✗ Incorrect
The spline fits the points exactly because smoothing factor s=0. At x=2.5, y = 2.5^2 = 6.25, so the spline returns 6.25.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Smoothing reduces the number of knots compared to the number of data points.
✗ Incorrect
With smoothing s=1, the spline reduces knots from 11 data points to fewer knots, here 9 knots are returned.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the x array for duplicates or ordering.
✗ Incorrect
UnivariateSpline requires x values to be strictly increasing. Duplicate 2's cause ValueError.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
More smoothing means less sensitivity to noise.
✗ Incorrect
A larger positive s smooths the spline, reducing noise effects while preserving the overall trend.
🧠 Conceptual
expert2:00remaining
Effect of Spline Degree on Fit
How does increasing the degree k of a UnivariateSpline from 1 to 3 affect the fitted curve?
Attempts:
2 left
💡 Hint
Higher degree splines can model curves better than linear splines.
✗ Incorrect
Increasing degree k allows the spline to fit curves with more flexibility and smoothness.