Latticework

Command Palette

Search for a command to run...

SciPy

Optimization with SciPy

18 min

Explanation

Optimization's course built gradient descent from scratch — genuinely useful for understanding HOW iterative optimization works. In practice, though, real code reaches for scipy.optimize: a well-tested library with several optimization algorithms, automatic step-size selection, and robust convergence handling that a hand-rolled implementation usually doesn't get right on the first try.

from scipy.optimize import minimize

def find_minimum(a, b, c, x0):
    f = lambda x: a * x[0] ** 2 + b * x[0] + c
    result = minimize(f, x0=[x0])
    return round(result.x[0], 4)
Try it

minimize() found the exact same answer -b/(2a) gives algebraically for a quadratic -- for functions where there's no clean closed-form solution (which is most real objective functions), this same call works identically, iterating numerically instead.

Loading editor…
Exercise

Write `find_minimum(a, b, c, x0)`: use `scipy.optimize.minimize` to find the `x` that minimizes `f(x) = a*x^2 + b*x + c`, starting the search from `x0`. Return `x` rounded to 4 decimal places.

Quiz

Optimization's own course implements gradient descent from scratch. Why reach for scipy.optimize instead, in practice?

Checkpoint

You can use scipy.optimize.minimize to find a function's minimum numerically, and understand why production code relies on a tested library rather than a hand-rolled optimizer.