Curriculum
Course: SCIPY
Login
Text lesson

SciPy Optimizers

Optimizers in SciPy

Optimizers in SciPy are a set of methods designed to find the minimum value of a function or the root of an equation.

Optimizing Functions

In essence, all machine learning algorithms are complex equations that need to be minimized using the provided data.

Roots of an Equation

NumPy can find roots for polynomials and linear equations, but it cannot solve non-linear equations like:

x + cos(x)

For such cases, you can use SciPy’s optimize.root function.

This function requires two arguments:

fun: a function representing the equation.

x0: an initial guess for the root.

The function returns an object containing details about the solution, with the actual solution provided under the x attribute of the returned object.

Example

Find the root of the equation x+cosā”(x)x + \cos(x)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)

Example

Display all details about the solution, not just the root (x).

print(myroot)