scipy.optimize finds minima, maxima, and roots of scalar and vector functions—used in curve fitting, ML loss minimization, and engineering design after preparing NumPy inputs.
Core ideas
- Objective — scalar function f(x) to minimize
- Initial guess — starting point x₀ (quality matters)
- Method — algorithm (BFGS, Nelder-Mead, L-BFGS-B, etc.)
- Result object —
x,fun,success,message
Scalar vs multivariate
minimize_scalar— one variable, bracket or boundedminimize— vector x ∈ ℝⁿ- Always check
result.successand residuals
Hello minimize
import numpy as np
from scipy import optimize
def f(x):
return (x[0] - 2) ** 2 + (x[1] + 1) ** 2
res = optimize.minimize(f, x0=[0, 0], method='BFGS')
print('x:', res.x, 'f:', res.fun, 'success:', res.success)
Important interview questions and answers
- Q: Local vs global minimum?
A: Most methods find local minima—try multiple x₀ or global search for hard landscapes. - Q: Why BFGS?
A: Popular quasi-Newton method for smooth unconstrained problems; no Hessian required.
Self-check
- What four items define an optimization problem?
- What attributes does minimize return?
Tip: Always print result.success and result.message after minimize.
Interview prep
- minimize inputs?
Objective f(x), initial x0, method, optional bounds/jac.
- success flag?
Always check result.success before using parameters.