Fit model parameters by minimizing a scalar loss function.
Parameter estimation
SciPy · Function
scipy.optimize.minimize minimizes a scalar function of one or more variables. Choose a solver and optionally provide derivatives, bounds, constraints, tolerances, callbacks, and solver-specific options.
minimize selects a numerical solver to reduce a scalar objective from an initial vector. The objective receives the current vector and optional fixed arguments; derivatives, bounds, constraints, tolerances, callbacks, and solver-specific options can be supplied to control the optimization.
scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)| Name | Type | Description |
|---|---|---|
fun | callable | Objective callable with signature fun(x, *args) -> float. |
x0 | ndarray, shape (n,) | Initial guess: a one-dimensional array of length n. |
args | tuple, optional | Extra arguments passed to fun and derivative functions. |
method | str or callable, optional | Solver name or callable. |
jac | callable, bool or str, optional | Gradient of the objective; callable, Boolean, or finite-difference specification depending on method. |
hess | callable, str or HessianUpdateStrategy, optional | Hessian of the objective, supplied as a callable, finite-difference scheme, or HessianUpdateStrategy where supported. |
hessp | callable, optional | Hessian-vector product callable. |
bounds | sequence or Bounds, optional | Bounds for variables. |
constraints | {Constraint, dict} or List of {Constraint, dict}, optional | Equality and inequality constraints. |
tol | float, optional | Tolerance for termination. |
callback | callable, optional | Callback invoked during optimization. |
options | dict, optional | Solver-specific options. |
OptimizeResult containing the solution, objective value, success status, termination message, and method-dependent diagnostic fields.
import numpy as np
from scipy.optimize import minimize
def objective(x):
return (x[0] - 2.0) ** 2 + (x[1] + 1.0) ** 2
result = minimize(objective, x0=np.array([0.0, 0.0]), method="BFGS")
print(result.x)
print(result.fun)Defines a two-variable quadratic objective and minimizes it from an initial guess using BFGS.
Parameter estimation
Constrained optimization
Numerical design
minimize solves scalar minimization problems with one or more variables. It takes an objective callable and an initial vector, then returns an OptimizeResult containing the solution and diagnostic information.
Follow-up: How would you pass fixed data to the objective? Supply it through args, whose tuple is forwarded to the objective and derivative callables.
method chooses the optimization algorithm and can be a solver name or a callable. If omitted, SciPy selects a method based on the supplied bounds and constraints.
Follow-up: What must a custom method accept? It is called with fun, x0, args, keyword parameters, and the contents of options.
jac supplies the objective gradient, while hess supplies a Hessian and hessp supplies Hessian-vector products for methods that use second-order information. These derivatives can improve efficiency or are required by particular methods.
Follow-up: When are bounds and constraints appropriate? Use bounds for per-variable intervals and constraints for equality or inequality relationships among variables.
scipy.optimize.OptimizeResultscipy.optimize.minimize_scalarscipy.optimize.Boundsscipy.optimize.LinearConstraintscipy.optimize.NonlinearConstraintscipy.optimize.show_options