minimize()
Published Jan 15, 2025
Contribute to Docs
The minimize()
function in the SciPy library is used to find the minimum of a scalar function. It provides various optimization algorithms, including both gradient-based and derivative-free methods.
Syntax
scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, constraints=(), bounds=None, tol=None, options=None)
Parameters
fun
: The objective function to be minimized.x0
: Initial guess for the variables.args
: Extra arguments passed to the objective function.method
: The optimization method to use (e.g.,'BFGS'
,'Nelder-Mead'
,'Powell'
, etc.).jac
(Optional): The gradient (Jacobian) of the objective function. If not provided, numerical differentiation is used.hess
(Optional): The Hessian matrix of the objective function. Typically used with second-order methods like ‘Newton-CG’ or ‘trust-ncg’.constraints
(Optional): Constraints definition. Can include equality or inequality constraints.bounds
(Optional): Bounds on variables.tol
(Optional): Tolerance for termination. Specifies the convergence threshold.options
(Optional): A dictionary of additional options specific to the selected optimization method (e.g., maximum number of iterations, tolerance, etc.).
It returns an OptimizeResult
object with the optimal solution, function value at the solution, success status, and other optimization details.
Example
In this example, we are using the minimize()
function to find the minimum value of a quadratic objective function:
from scipy.optimize import minimize# Define the objective functiondef objective_function(x):return x**2# Initial guessx0 = 2# Perform the minimizationresult = minimize(objective_function, x0)# Print the resultprint("Optimal value:", result.fun)print("Optimal point:", result.x)
It produces the following output:
Optimal value: 3.5662963072207506e-16Optimal point: [-1.88846401e-08]
Codebyte Example
Run the following codebyte example to understand how the minimize()
function works:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SciPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Free course
Learn Python 2
Learn the basics of the world's fastest growing and most popular programming language used by software engineers, analysts, data scientists, and machine learning engineers alike.Beginner Friendly17 hours