Challenge - 5 Problems
Least Squares Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of simple least squares fit
What is the output of the following code that fits a line y = mx + c to data points using scipy.optimize.least_squares?
SciPy
import numpy as np from scipy.optimize import least_squares def fun(params, x, y): m, c = params return m * x + c - y x = np.array([0, 1, 2, 3]) y = np.array([1, 3, 5, 7]) res = least_squares(fun, x0=[0, 0], args=(x, y)) print(res.x.round(2))
Attempts:
2 left
💡 Hint
Think about the line that best fits points (0,1), (1,3), (2,5), (3,7).
✗ Incorrect
The points lie perfectly on the line y = 2x + 1. The least squares finds slope m=2 and intercept c=1.
❓ data_output
intermediate2:00remaining
Number of iterations in least squares optimization
How many iterations does the least_squares optimizer perform for this problem?
SciPy
import numpy as np from scipy.optimize import least_squares def fun(params, x, y): m, c = params return m * x + c - y x = np.array([0, 1, 2, 3]) y = np.array([1, 3, 5, 7]) res = least_squares(fun, x0=[0, 0], args=(x, y)) print(res.nfev)
Attempts:
2 left
💡 Hint
Check the attribute that counts function evaluations in the result object.
✗ Incorrect
The optimizer evaluates the function 4 times before converging for this simple problem.
🔧 Debug
advanced2:00remaining
Identify the error in least squares residual function
What error does this code raise when run?
SciPy
import numpy as np from scipy.optimize import least_squares def fun(params, x, y): m, c = params return m * x + c + y x = np.array([0, 1, 2]) y = np.array([1, 2, 3]) res = least_squares(fun, x0=[0, 0], args=(x, y))
Attempts:
2 left
💡 Hint
Check if the function returns an array of residuals as expected.
✗ Incorrect
The function returns residuals but adds y instead of subtracting. This does not raise an error but leads to wrong fit.
🧠 Conceptual
advanced2:00remaining
Effect of initial guess on least squares result
Which statement about the initial guess in scipy.optimize.least_squares is true?
Attempts:
2 left
💡 Hint
Think about convex problems and local minima.
✗ Incorrect
For convex problems like linear least squares, initial guess affects speed but optimizer converges to the same solution.
🚀 Application
expert3:00remaining
Fitting a nonlinear model with least squares
Given data points x and y, which option fits the nonlinear model y = a * exp(b * x) using scipy.optimize.least_squares and returns the optimized parameters?
SciPy
import numpy as np from scipy.optimize import least_squares x = np.array([0, 1, 2, 3]) y = np.array([1, 2.7, 7.4, 20.1])
Attempts:
2 left
💡 Hint
Residuals should be model prediction minus observed data.
✗ Incorrect
Option A correctly defines residuals as model minus data. Option A reverses residuals but least_squares minimizes sum of squares so it still works but is less conventional. Option A tries log transform incorrectly. Option A is wrong model.