Introduction to Python SciPy Optimizers

In This Article, You Will Learn About Python SciPy Optimizers.

Python SciPy Optimizers – Before moving ahead, let’s know a bit about Constants in SciPy

Table of Contents

Optimizers in SciPy

Optimizers are procedures defined in SciPy that either finds the function’s minimum value or the root of an equation.

Optimizing Functions

In essence, all algorithms used in Machine Learning are nothing more than a complicated equation which has to be reduced by using information.

Roots of an Equation

NumPy is able to find roots for linear and polynomial equations. However, it does not locate roots for non- linear equations. For that we can use SciPy’s optimze.root function.

This function takes two arguments –

fun – A function that represents an equation
x0 – An initial guess for the root.

This function will return an object that contains information about the solution.

The solution actually is provided in the attribute of x of the object returned.

				
					from scipy.optimize import root
from math import cos

def equation(x):
  return x + cos(x)

first_root = root(equation, 0)

print(first_root.x)

				
			
python-scipy-optimizers

As shown clearly, it returned a solution in attribute of x. 

Example – Print all information about the solution (not just x which is the root).

				
					from scipy.optimize import root
from math import cos

def equation(x):
  return x + cos(x)

first_root = root(equation, 0)

print(first_root)

				
			
python-scipy-optimizers

As shown above, it returned information for all information not only for x.

Minimizing a Function

A function, in this instance is a type of curve. which has both high and low points.

Points that are high called maxima.

Points that are low called minima.

The top point of the whole curve is known as the global maximum while the rest of them are known as the local maxima.

The top point of the whole curve is known as the global minima while the rest of them are known as the local minima.

Find Minima

To minimize function, use scipy.optimize.minimize() function.

This function takes two arguments –

fun – A function that represents an equation

x0 – An initial guess for the root.

method – Legal values. Name of the method to use.

‘CG’
    ‘BFGS’
    ‘Newton-CG’
    ‘L-BFGS-B’
    ‘TNC’
    ‘COBYLA’
    ‘SLSQP’

callback – A function that is called upon every repetition of optimization.

options – A dictionary defines extra parameters.

{

     “disp”: boolean – print detailed description

     “gtol”: number – the tolerance of the error

  }

Example –  Minimize the function x^2 + x + 2 with TNC.

				
					from scipy.optimize import minimize

def equation(x):
  return x**2 + x + 2

first_minima = minimize(equation, 0, method='TNC')

print(first_minima)

				
			
python-scipy-optimizers

As a result, it returned a minimize function with method “TNC”.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

1 thought on “Introduction to Python SciPy Optimizers”

Leave a Comment

Stay in the loop

codingstreets