scipy.optimize

dakshdeepHERE's avatar
Published Dec 21, 2024
Contribute to Docs

The scipy.optimize module is part of the SciPy library for scientific computing in Python. It provides a variety of optimization and root-finding routines designed to solve mathematical problems, such as finding minima or maxima of functions, solving systems of equations, and performing linear or nonlinear optimizations. Whether tuning model parameters, allocating resources, or fitting complex curves, scipy.optimize offers a rich toolbox for improving decision-making and model performance.

Functions in scipy.optimize

Minimization

Minimizes a scalar function (i.e., finds the values that minimize the objective function). It has the following syntax:

optimize.minimize(fun, x0, method=...)
  • fun: The objective function to minimize.
  • x0: Initial guess.
  • method: Algorithm to use (e.g., 'BFGS', 'Nelder-Mead', etc.).

Root-Finding

Finds the roots (or solutions) of a function, i.e., the points where the function equals zero. It has a syntax:

optimize.root(fun, x0, method=...)
  • fun: The function for which the root is sought.
  • x0: Initial guess.
  • method: Algorithm to use (e.g., 'hybr', 'broyden1').

Linear Programming

Solves linear optimization problems, such as maximizing or minimizing a linear objective function subject to linear constraints:

optimize.linprog(c, A_ub=..., b_ub=..., A_eq=..., b_eq=..., bounds=..., method='highs')
  • c: Coefficients of the linear objective function.
  • A_ub, b_ub: Inequality constraints.
  • A_eq, b_eq: Equality constraints.
  • bounds: Variable bounds.

Curve Fitting

Fits a model to observed data by performing nonlinear least squares fitting, finding the parameters that minimize the difference between the observed data and the model. The syntax is:

optimize.curve_fit(f, xdata, ydata, p0=...)
  • f: The model function, f(x, …).
  • xdata, ydata: The observed data.
  • p0: Initial guess for the parameters.

scipy.optimize

minimize()
Returns the minimum of a scalar function of one or more variables using optimization methods from SciPy.

All contributors

Contribute to Docs

Learn SciPy on Codecademy