Challenge - 5 Problems
Least Squares Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of scipy.optimize.least_squares with linear residuals
What is the output of the following code snippet using
scipy.optimize.least_squares to fit a line y = mx + b to data points?SciPy
import numpy as np from scipy.optimize import least_squares def residuals(params, x, y): m, b = params return m * x + b - y x_data = np.array([0, 1, 2, 3]) y_data = np.array([1, 3, 5, 7]) result = least_squares(residuals, x0=[0, 0], args=(x_data, y_data)) print(result.x)
Attempts:
2 left
💡 Hint
Think about the line that fits points (0,1), (1,3), (2,5), (3,7) exactly.
✗ Incorrect
The data points lie perfectly on the line y = 2x + 1. The least squares method finds parameters m=2 and b=1.
❓ data_output
intermediate2:00remaining
Number of iterations in least_squares optimization
After running the following code, what is the value of
result.nfev (number of function evaluations)?SciPy
import numpy as np from scipy.optimize import least_squares def residuals(p, x, y): return p[0] * x + p[1] - y x = np.linspace(0, 10, 5) y = 3 * x + 4 + np.random.normal(0, 0.1, size=x.size) result = least_squares(residuals, x0=[0, 0], args=(x, y)) print(result.nfev)
Attempts:
2 left
💡 Hint
The solver usually needs multiple evaluations to converge, but not too many for a simple linear fit.
✗ Incorrect
The solver typically evaluates the residual function about 6 times for this simple problem before converging.
🔧 Debug
advanced2:00remaining
Identify the error in least_squares residual function
What error will this code raise when run?
SciPy
import numpy as np from scipy.optimize import least_squares def residuals(params, x, y): m, b = params return m * x + b + y # Incorrect sign here x = np.array([1, 2, 3]) y = np.array([2, 4, 6]) result = least_squares(residuals, x0=[0, 0], args=(x, y))
Attempts:
2 left
💡 Hint
Check the residuals formula carefully for correctness.
✗ Incorrect
The code runs without error but the residuals are incorrect because it adds y instead of subtracting, leading to a wrong fit.
🧠 Conceptual
advanced2:00remaining
Effect of jac='3-point' in least_squares
What does setting
jac='3-point' do in scipy.optimize.least_squares?Attempts:
2 left
💡 Hint
Think about numerical differentiation methods.
✗ Incorrect
The '3-point' option uses a finite difference method with three points to approximate the Jacobian numerically.
🚀 Application
expert2:00remaining
Predict parameter values from least_squares output
Given this output from
least_squares: result.x = [1.5, -0.5], and residuals defined as m * x + b - y, what is the predicted y value for x=4?Attempts:
2 left
💡 Hint
Use the formula y = m*x + b with given parameters.
✗ Incorrect
With m=1.5 and b=-0.5, y = 1.5*4 - 0.5 = 6 - 0.5 = 5.5.