Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮   
     ❯   

SciPyOptimizers


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 fornon linear equations, like this one:

x + cos(x)

For that you can use SciPy'soptimize.root function.

This function takes two required arguments:

fun - a function representing an equation.

x0 - an initial guess for the root.

The function returns an object with information regarding the solution.

The actual solution is given under attributex of the returned object:

Example

Find root of the equationx + 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 justx which is the root)

print(myroot)
Try it Yourself »


Minimizing a Function

A function, in this context, represents a curve, curves havehigh points andlow points.

High points are calledmaxima.

Low points are calledminima.

The highest point in the whole curve is calledglobal maxima, whereas the rest of them are calledlocal maxima.

The lowest point in whole curve is calledglobal minima, whereas the rest of them are calledlocal minima.


Finding Minima

We can usescipy.optimize.minimize() function to minimize the function.

Theminimize() 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'
   'L-BFGS-B'
   'TNC'
   '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 functionx^2 + x + 2 withBFGS:

from scipy.optimize import minimize

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

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

print(mymin)
Try it Yourself »



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

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 ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp