Bird
0
0

Consider this code:

medium📝 Predict Output Q5 of 15
SciPy - Advanced Optimization
Consider this code:
from scipy.optimize import minimize

history = []
def cb(x):
    history.append(x.copy())

res = minimize(lambda x: (x[0]-1)**2 + (x[1]+2)**2, [0,0], callback=cb)
print(len(history))

What will be printed?
AZero, because callback is not called
BNumber of iterations performed during optimization
COne, because callback is called only once
DLength of initial guess vector
Step-by-Step Solution
Solution:
  1. Step 1: Understand callback usage

    The callback appends the current parameter vector at each iteration.
  2. Step 2: Determine what history length represents

    History length equals the number of iterations performed.
  3. Final Answer:

    Number of iterations performed during optimization -> Option B
  4. Quick Check:

    History length = Iterations count [OK]
Quick Trick: Callback called each iteration; history length = iteration count [OK]
Common Mistakes:
  • Assuming callback is never called
  • Thinking callback called only once
  • Confusing history length with vector size

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes