Challenge - 5 Problems
Polyfit Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.polyfit() coefficients
What is the output of the following code?
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 6, 8, 10]) coeffs = np.polyfit(x, y, 1) print(coeffs)
NumPy
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 6, 8, 10]) coeffs = np.polyfit(x, y, 1) print(coeffs)
Attempts:
2 left
💡 Hint
Remember np.polyfit returns coefficients starting with the highest degree term.
✗ Incorrect
np.polyfit(x, y, 1) fits a line y = mx + b. The output is [m, b]. Here, slope m=2.0 and intercept b=0.0.
❓ data_output
intermediate2:00remaining
Number of points predicted by polyfit model
Given the code below, how many predicted y-values are generated?
import numpy as np x = np.array([0, 1, 2, 3]) y = np.array([1, 3, 5, 7]) coeffs = np.polyfit(x, y, 1) predicted_y = np.polyval(coeffs, x) print(len(predicted_y))
NumPy
import numpy as np x = np.array([0, 1, 2, 3]) y = np.array([1, 3, 5, 7]) coeffs = np.polyfit(x, y, 1) predicted_y = np.polyval(coeffs, x) print(len(predicted_y))
Attempts:
2 left
💡 Hint
The predicted values are calculated for each x input point.
✗ Incorrect
np.polyval evaluates the polynomial at each x value, so the output length matches input x length.
🔧 Debug
advanced2:00remaining
Error raised by incorrect np.polyfit usage
What error does this code raise?
import numpy as np x = np.array([1, 2, 3]) y = np.array([2, 4]) coeffs = np.polyfit(x, y, 1)
NumPy
import numpy as np x = np.array([1, 2, 3]) y = np.array([2, 4]) coeffs = np.polyfit(x, y, 1)
Attempts:
2 left
💡 Hint
Check if x and y arrays have the same size.
✗ Incorrect
np.polyfit requires x and y arrays to be the same length. Here, x has 3 elements, y has 2, so it raises ValueError.
🧠 Conceptual
advanced2:00remaining
Interpretation of np.polyfit output array
If np.polyfit(x, y, 2) returns [3, -2, 5], what is the polynomial equation it represents?
Attempts:
2 left
💡 Hint
np.polyfit returns coefficients from highest degree to constant term.
✗ Incorrect
The array [3, -2, 5] means 3*x^2 - 2*x + 5.
🚀 Application
expert3:00remaining
Predicting new values using np.polyfit and np.polyval
You fit a line to data x=[1,2,3,4], y=[2,3,5,7] using np.polyfit with degree 1. What is the predicted y value for x=5?
import numpy as np x = np.array([1, 2, 3, 4]) y = np.array([2, 3, 5, 7]) coeffs = np.polyfit(x, y, 1) predicted_y = np.polyval(coeffs, 5) print(round(predicted_y, 2))
NumPy
import numpy as np x = np.array([1, 2, 3, 4]) y = np.array([2, 3, 5, 7]) coeffs = np.polyfit(x, y, 1) predicted_y = np.polyval(coeffs, 5) print(round(predicted_y, 2))
Attempts:
2 left
💡 Hint
Calculate slope and intercept from polyfit, then evaluate at x=5.
✗ Incorrect
The fitted line is approximately y = 1.7x + 0.1, so at x=5, y ≈ 8.5.