Bird
0
0

Identify the error in this code snippet using least_squares:

medium📝 Debug Q14 of 15
SciPy - Advanced Optimization
Identify the error in this code snippet using least_squares:
from scipy.optimize import least_squares

def fun(x):
    return x**2 - 4

result = least_squares(fun)
print(result.x)
AMissing initial guess argument in least_squares call
BResidual function returns scalar instead of array
CFunction fun should return x**2 + 4
DPrint statement syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check least_squares function call

    The call lacks the required initial guess argument x0.
  2. Step 2: Confirm residual function and print are correct

    The residual function returns an array-like (scalar is acceptable as 1D array), and print syntax is valid.
  3. Final Answer:

    Missing initial guess argument in least_squares call -> Option A
  4. Quick Check:

    least_squares needs initial guess [OK]
Quick Trick: Always provide initial guess to least_squares [OK]
Common Mistakes:
  • Forgetting initial guess
  • Thinking scalar residuals cause error
  • Misreading print syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes