SciPy Optimizer
The optimize module in SciPy provides commonly used optimization algorithm functions that can be directly called to solve optimization problems, such as finding the minimum value of a function or the roots of an equation.
NumPy can find the roots of polynomial and linear equations, but it cannot find the roots of nonlinear equations, as shown below:
x + cos(x)
Therefore, we can use SciPy's optimize.root function, which requires two parameters:
- fun - the function representing the equation.
- x0 - the initial guess for the root.
The function returns an object containing information about the solution.
The actual solution is in the attribute x of the returned object, as shown in the following example:
Example
Finding the root of the equation x + cos(x):
from scipy.optimize import root
from math import cos
def eqn(x):
return x + cos(x)
myroot = root(eqn, 0)
print(myroot.x)
# View more information
#print(myroot)
Executing the above code, the output is as follows:
[-0.73908513]
To view more information:
Example
from scipy.optimize import root
from math import cos
def eqn(x):
return x + cos(x)
myroot = root(eqn, 0)
print(myroot)
Executing the above code, the output is as follows:
fjac: array([[-1.]])
fun: array([0.])
message: 'The solution converged.'
nfev: 9
qtf: array([-2.66786593e-13])
r: array([-1.67361202])
status: 1
success: True
x: array([-0.73908513])
Minimizing Functions
A function represents a curve, which has high and low points.
High points are called maxima.
Low points are called minima.
The highest point on the entire curve is the global maximum, and the rest are local maxima.
The lowest point on the entire curve is the global minimum, and the rest are local minima.
The scipy.optimize.minimize()
function can be used to minimize a function.
minimize() accepts the following parameters:
- fun - the function to optimize
- x0 - the initial guess
- method - the name of the method to use, which can be: 'CG', 'BFGS', 'Newton-CG', 'L-BFGS-B', 'TNC', 'COBYLA', 'SLSQP'.
- callback - a function called after each iteration of optimization.
- options - a dictionary defining additional parameters:
{ "disp": boolean - print detailed description "gtol": number - the tolerance of the error }
Example
Minimizing the function x^2 + x + 2 using BFGS:
from scipy.optimize import minimize
def eqn(x):
return x**2 + x + 2
mymin = minimize(eqn, 0, method='BFGS')
print(mymin)
Executing the above code, the output is as follows:
fun: 1.75
hess_inv: array([[0.50000001]])
jac: array([0.])
message: 'Optimization terminated successfully.'
nfev: 8
nit: 2
njev: 4
status: 0
success: True
x: array([-0.50000001])