Challenge - 5 Problems
Polynomial Fitting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of polynomial fit coefficients
What is the output of the following code that fits a polynomial of degree 2 to the data points?
SciPy
import numpy as np from numpy import polyfit x = np.array([0, 1, 2, 3]) y = np.array([1, 3, 7, 13]) coeffs = polyfit(x, y, 2) print(coeffs)
Attempts:
2 left
💡 Hint
Remember that polyfit returns coefficients starting from the highest degree term.
✗ Incorrect
The points fit the polynomial y = 1*x^2 + 1*x + 1 exactly, so the coefficients are [1, 1, 1].
❓ data_output
intermediate1:30remaining
Number of coefficients from polynomial fit
How many coefficients does polyfit return when fitting a polynomial of degree 4 to 6 data points?
SciPy
import numpy as np from numpy import polyfit x = np.arange(6) y = np.array([1, 4, 9, 16, 25, 36]) coeffs = polyfit(x, y, 4) print(len(coeffs))
Attempts:
2 left
💡 Hint
The number of coefficients is degree + 1.
✗ Incorrect
A polynomial of degree 4 has 5 coefficients, from x^4 down to the constant term.
❓ visualization
advanced2:30remaining
Plotting polynomial fit and data points
Which option produces a plot showing the original data points as red dots and the polynomial fit curve as a blue line?
SciPy
import numpy as np import matplotlib.pyplot as plt from numpy import polyfit, polyval x = np.array([0, 1, 2, 3, 4]) y = np.array([1, 3, 7, 13, 21]) coeffs = polyfit(x, y, 2) x_fit = np.linspace(0, 4, 100) y_fit = polyval(coeffs, x_fit) plt.scatter(x, y, color='red') plt.plot(x_fit, y_fit, color='blue') plt.show()
Attempts:
2 left
💡 Hint
Check the colors and plot types used for data and fit.
✗ Incorrect
The code uses plt.scatter with red color for data points and plt.plot with blue color for the polynomial curve.
🧠 Conceptual
advanced2:00remaining
Effect of polynomial degree on overfitting
What happens if you fit a polynomial of very high degree to a small noisy dataset?
Attempts:
2 left
💡 Hint
Think about what happens when a model is too complex for the data.
✗ Incorrect
High-degree polynomials can wiggle to fit noise, causing overfitting and poor predictions on new data.
🔧 Debug
expert1:30remaining
Identify error in polynomial fitting code
What error does the following code raise?
SciPy
import numpy as np from numpy import polyfit x = np.array([1, 2, 3]) y = np.array([2, 4]) coeffs = polyfit(x, y, 2)
Attempts:
2 left
💡 Hint
Check the lengths of x and y arrays.
✗ Incorrect
polyfit requires x and y to have the same length; here y has fewer elements, causing a ValueError.