Real optimizers enforce box bounds (each variable in [low, high]) and sometimes linear/nonlinear constraints—essential for physically plausible parameters.
Bounds format
For minimize with method='L-BFGS-B', pass bounds=[(0, None), (-5, 5)] per variable. Use None for open ends.
Constraints dict
type='ineq'— c(x) ≥ 0type='eq'— c(x) = 0- Methods: SLSQP, trust-constr
Bounded example
import numpy as np
from scipy import optimize
def f(x):
return (x[0] - 1) ** 2 + (x[1] - 2) ** 2
bounds = [(0, 1), (0, 1)] # box [0,1] x [0,1]
res = optimize.minimize(f, [0.5, 0.5], method='L-BFGS-B', bounds=bounds)
print(res.x)
Important interview questions and answers
- Q: Why bounds in ML?
A: Keep weights or probabilities in valid ranges—e.g. non-negative concentrations. - Q: L-BFGS-B?
A: Limited-memory BFGS with box constraints—workhorse for bounded ML-style problems.
Self-check
- How do you pass bounds to minimize?
- What does an inequality constraint mean in SciPy's convention?
Tip: Use L-BFGS-B when parameters must stay in a box (probabilities, physical lengths).
Interview prep
- L-BFGS-B?
Limited-memory BFGS with box bounds per variable.
- ineq constraint?
c(x) >= 0 in SciPy constraint dict.