Challenge - 5 Problems
Model Fitting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of linear fit coefficients
What is the output of the following code that fits a linear model to data points?
SciPy
import numpy as np from scipy.optimize import curve_fit def linear_model(x, a, b): return a * x + b x_data = np.array([1, 2, 3, 4, 5]) y_data = np.array([2.1, 4.1, 6.0, 8.1, 10.2]) params, _ = curve_fit(linear_model, x_data, y_data) print(np.round(params, 2))
Attempts:
2 left
💡 Hint
Think about how close the y_data values are to a line with slope 2 and intercept near zero.
✗ Incorrect
The data roughly follows y = 2x with a small intercept. The curve_fit finds parameters close to slope 2 and intercept 0.04.
❓ data_output
intermediate2:00remaining
Number of points within 0.2 of fitted quadratic
After fitting a quadratic model to data, how many points lie within 0.2 units of the fitted curve?
SciPy
import numpy as np from scipy.optimize import curve_fit def quad_model(x, a, b, c): return a * x**2 + b * x + c x = np.linspace(-2, 2, 10) y = 3 * x**2 + 2 * x + 1 + np.random.normal(0, 0.1, x.size) params, _ = curve_fit(quad_model, x, y) y_fit = quad_model(x, *params) close_points = np.sum(np.abs(y - y_fit) < 0.2) print(close_points)
Attempts:
2 left
💡 Hint
The noise is small, so most points should be close to the fitted curve.
✗ Incorrect
Because noise is low, all 10 points are within 0.2 of the fitted quadratic curve.
❓ visualization
advanced2:00remaining
Identify the plot showing best fit line
Which plot correctly shows the data points and the best fit line from a linear regression?
SciPy
import matplotlib.pyplot as plt import numpy as np from scipy.optimize import curve_fit def linear(x, m, c): return m * x + c x = np.array([0, 1, 2, 3, 4]) y = np.array([1, 3, 5, 7, 9]) params, _ = curve_fit(linear, x, y) y_fit = linear(x, *params) plt.figure(figsize=(8, 5)) plt.scatter(x, y, label='Data points') plt.plot(x, y_fit, label='Fitted line', color='red') plt.legend() plt.title('Linear Fit Example') plt.xlabel('x') plt.ylabel('y') plt.show()
Attempts:
2 left
💡 Hint
Best fit lines minimize error but rarely pass exactly through all points.
✗ Incorrect
The data lies perfectly on the line y=2x+1, so the fitted line passes exactly through all points.
🧠 Conceptual
advanced1:30remaining
Why does fitting a model reveal relationships?
Why does fitting a mathematical model to data help reveal relationships between variables?
Attempts:
2 left
💡 Hint
Think about what fitting tries to optimize in the model.
✗ Incorrect
Fitting finds parameters that minimize differences between model predictions and data, showing how variables relate.
🔧 Debug
expert2:30remaining
Identify the error in model fitting code
What error will this code raise when trying to fit a model to data?
SciPy
import numpy as np from scipy.optimize import curve_fit def model(x, a, b): return a * np.sin(b * x) x = np.linspace(0, 2 * np.pi, 50) y = np.sin(x) + np.random.normal(0, 0.1, x.size) params, _ = curve_fit(model, x, y, p0=[1, 1])
Attempts:
2 left
💡 Hint
Check the number of parameters in the model and the initial guess length.
✗ Incorrect
The model has two parameters (a, b), but p0 provides only one initial guess, causing ValueError.