SciPy Optimizers

scipy.optimize provides functions for finding the minima of functions and the roots of equations, using a variety of numerical algorithms.

What scipy.optimize Does

scipy.optimize is SciPy's subpackage for finding the input that minimizes a function, or the input where a function crosses zero. Those two problems - minimization and root-finding - cover an enormous range of practical tasks, from fitting a curve to data to solving an equation that has no neat algebraic solution.

  • minimize() - find the input that minimizes a scalar function of one or more variables
  • minimize_scalar() - the same idea, specialized for a single-variable function
  • root() - find where a function of one or more variables equals zero
  • root_scalar() - the same idea, specialized for a single-variable function
  • curve_fit() - fit a function with adjustable parameters to a set of data points
  • linprog() - solve linear programming problems with linear constraints

Minimizing a Function

Minimizing x**2 + x + 2

from scipy.optimize import minimize

def f(x):
    return x**2 + x + 2

result = minimize(f, x0=0)

print(result.x)    # array([-0.5])
print(result.fun)  # 1.75

Calculus tells us the minimum of x**2 + x + 2 sits where its derivative, 2x + 1, equals zero - at x = -0.5, giving a value of 1.75. minimize() finds this numerically without you ever writing the derivative yourself, starting from the initial guess x0 and refining it step by step. The returned result object carries the location in .x, the function value in .fun, and a .success flag telling you whether the algorithm converged.

Finding a Root

Solving x + cos(x) = 0

from scipy.optimize import root
import numpy as np

def equation(x):
    return x + np.cos(x)

solution = root(equation, x0=0)

print(solution.x)            # array([-0.73908513])
print(equation(solution.x))  # very close to 0

There is no simple formula for solving x + cos(x) = 0 by hand, but root() locates it numerically: starting from x0=0, it iterates until the function's value is close enough to zero. Plugging the result back into equation() confirms the residual is essentially zero.

FunctionPurpose
minimizeMinimize a function of one or more variables
minimize_scalarMinimize a function of a single variable
rootFind a zero of a function of one or more variables
curve_fitFit parameters of a function to data
linprogSolve a linear program with constraints
Note: Numerical optimizers only guarantee a local minimum or root near the starting guess. minimize(f, x0=10) on a function with several dips can land on a different, worse minimum than minimize(f, x0=0). Try more than one starting point when you are not sure the function is well-behaved.
Note: Both minimize() and root() accept a method argument (for example 'Nelder-Mead', 'BFGS', or 'CG' for minimize) so you can switch algorithms without rewriting your objective function.

Exercise: SciPy Optimizers

What is scipy.optimize primarily used for?