0% found this document useful (0 votes)
17 views7 pages

SciPy Optimizers 5+

The document provides an overview of optimizers in SciPy, which are used to find the minimum values of functions and the roots of equations. It explains how to use the `optimize.root` function for finding roots of non-linear equations and the `optimize.minimize` function for minimizing functions, detailing the required arguments and methods available. Examples are included to demonstrate the usage of these functions in Python.

Uploaded by

krishna kardam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

SciPy Optimizers 5+

The document provides an overview of optimizers in SciPy, which are used to find the minimum values of functions and the roots of equations. It explains how to use the `optimize.root` function for finding roots of non-linear equations and the `optimize.minimize` function for minimizing functions, detailing the required arguments and methods available. Examples are included to demonstrate the usage of these functions in Python.

Uploaded by

krishna kardam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

 Tutorials  Exercises  Services   Sign Up Log in

SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE

SciPy Optimizers
❮ Previous Next ❯

Optimizers in SciPy
Optimizers are a set of procedures defined in SciPy that either find the minimum value of a
function, or the root of an equation.

Optimizing Functions
Essentially, all of the algorithms in Machine Learning are nothing more than a complex equation
that needs to be minimized with the help of given data.

Roots of an Equation
NumPy is capable of finding roots for polynomials and linear equations, but it can not find roots
for non linear equations, like this one:

x + cos(x)

For that you can use SciPy's optimize.root function.

This function takes two required arguments:

fun - a function representing an equation.


x0 - an initial guess for the root.
 Tutorials  Exercises  Services   Sign Up Log in
The function returns an object with information regarding the solution.
SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
The actual solution is given under attribute x of the returned object:

Example Get your own Python Server

Find 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)

Try it Yourself »

Note: The returned object has much more information about the solution.

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

print(myroot)

Try it Yourself »

ADVERTISEMENT
 Tutorials  Exercises  Services   Sign Up Log in

SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE

Minimizing a Function
A function, in this context, represents a curve, curves have high points and low points.

High points are called maxima.

Low points are called minima.

The highest point in the whole curve is called global maxima, whereas the rest of them are
called local maxima.

The lowest point in whole curve is called global minima, whereas the rest of them are called
local minima.

Finding Minima
We can use scipy.optimize.minimize() function to minimize the function.

The minimize() function takes the following arguments:

fun - a function representing an equation.


x0 - an initial guess for the root.
method - name of the method to use. Legal values:
'CG'
'BFGS'
'Newton-CG'
 Tutorials 
'L-BFGS-B'
Exercises  Services   Sign Up Log in

'TNC'
SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
'COBYLA'
'SLSQP'

callback - function called after each iteration of optimization.


options - a dictionary defining extra params:
{
"disp": boolean - print detailed description
"gtol": number - the tolerance of the error
}

Example
Minimize the function x^2 + x + 2 with BFGS :

from scipy.optimize import minimize

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

mymin = minimize(eqn, 0, method='BFGS')

print(mymin)

Try it Yourself »

?
Exercise
How many required arguments does the root function have?

2
 3
Tutorials  Exercises  Services   Sign Up Log in

SASS VUE4 DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE

Submit Answer »

❮ Previous Next ❯

Track your progress - it's free! Sign Up Log in

ADVERTISEMENT

COLOR PICKER
 Tutorials  Exercises   
Services 
  Sign Up Log in

SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE

ADVERTISEMENT

 PLUS SPACES GET CERTIFIED

FOR TEACHERS FOR BUSINESS CONTACT US

Top Tutorials Top References


HTML Tutorial HTML Reference
CSS Tutorial CSS Reference
JavaScript Tutorial JavaScript Reference
How To Tutorial SQL Reference
SQL Tutorial Python Reference
Python Tutorial W3.CSS Reference
W3.CSS Tutorial Bootstrap Reference
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate

 How To Examples
Tutorials 
SQL Examples
Exercises  Services  Front End Certificate

SQL Certificate
Sign Up Log in
Python Examples Python Certificate
SASS VUEW3.CSSDSA
Examples GEN AI SCIPY AWS PHP Certificate
CYBERSECURITY DATA SCIENCE
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve
reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use,
cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by


W3.CSS.

You might also like