Bird
0
0

You want to fit a nonlinear model y = a * exp(b * x) to data points using least_squares. Which residual function is correct?

hard📝 Application Q8 of 15
SciPy - Curve Fitting and Regression
You want to fit a nonlinear model y = a * exp(b * x) to data points using least_squares. Which residual function is correct?
Adef residuals(params): a, b = params return np.sum(y_data - a * np.exp(b * x_data))
Bdef residuals(params): a, b = params return a * np.exp(b * x_data) - y_data
Cdef residuals(params): a, b = params return (y_data - a * np.exp(b * x_data))**2
Ddef residuals(params): a, b = params return np.sum((y_data - a * np.exp(b * x_data))**2)
Step-by-Step Solution
Solution:
  1. Step 1: Understand residuals definition

    Residuals are differences between model predictions and observed data (model - data per SciPy convention).
  2. Step 2: Check each option

    A returns model - data array (residuals) [check].
    B returns scalar sum(data - model), incorrect.
    C returns elementwise squared residuals, incorrect.
    D returns sum of squared residuals (scalar), incorrect.
  3. Final Answer:

    def residuals(params): a, b = params return a * np.exp(b * x_data) - y_data -> Option B
  4. Quick Check:

    Residuals = model - data [OK]
Quick Trick: Residuals = model prediction minus observed data [OK]
Common Mistakes:
  • Returning squared residuals instead of residuals
  • Returning sum of residuals instead of array
  • Confusing order of subtraction in residuals

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes