scipy.optimize
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.
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