Unconstrained numerical optimization
Find parameters that minimize a differentiable loss without constraints.
SciPy · Function
Minimizes a scalar function of one or more variables using a selectable optimization method. It supports derivatives, bounds, constraints, tolerances, callbacks, and method-specific options.
minimize searches for an argument vector that produces a small value of a scalar objective. It accepts an initial point, optionally selects a solver and derivatives, and returns an OptimizeResult containing the solution, objective value, and solver status information.
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: fun(x, *args) -> float. |
x0 | ndarray | Initial guess; a real one-dimensional array of shape (n,). |
args | tuple, optional | Extra arguments passed to the objective and derivative functions. |
method | str or callable, optional | Solver name or custom minimization callable. |
jac | callable, bool, or str, optional | Jacobian specification for the objective. |
hess | callable or str, optional | Hessian specification or Hessian-update strategy. |
hessp | callable, optional | Hessian-vector product callable. |
bounds | sequence or Bounds, optional | Bounds on variables. |
constraints | Constraint or sequence, optional | Constraints definition. |
tol | float, optional | Solver termination tolerance. |
options | dict, optional | Method-specific options. |
callback | callable, optional | Progress callback. |
OptimizeResult containing the solution, objective value, success flag, termination message, and method-dependent diagnostic fields.
import numpy as np
from scipy.optimize import minimize
result = minimize(lambda x: (x[0] - 3)**2 + (x[1] + 1)**2,
x0=np.array([0.0, 0.0]), method="BFGS")
print(result.x)
print(result.fun)Minimizes a two-variable quadratic from an initial guess and prints the estimated minimizer and objective value.
Find parameters that minimize a differentiable loss without constraints.
Optimize parameters subject to variable limits or feasibility conditions.
Use an analytic gradient or Hessian to improve solver performance.
fun receives a one-dimensional parameter array and any values supplied through args, and it must return a scalar objective value. x0 supplies the initial guess for the independent variables.
Follow-up: How would you provide a gradient? Pass jac as a callable returning the gradient, or use the documented boolean shortcut when fun returns both the objective and gradient.
method selects the optimization algorithm and can be a method name or a callable. If it is omitted, SciPy selects an appropriate method from the presence of bounds and constraints.
Follow-up: Which solver is used for an unconstrained problem when method is omitted? BFGS is the default in that case.
The return value is an OptimizeResult, with fields such as x for the solution, fun for the objective value, success for the termination status, and message for a human-readable explanation. Solver-specific fields may also be present.
Follow-up: How should a caller detect failure? Check success and inspect message rather than assuming that a returned x indicates successful convergence.
scipy.optimize.OptimizeResultscipy.optimize.minimize_scalarscipy.optimize.least_squaresscipy.optimize.Boundsscipy.optimize.LinearConstraintscipy.optimize.NonlinearConstraint