Function Optimization With SciPy

Optimization involves finding the inputs to an objective function that result in the minimum or maximum output of the function.

The open-source Python library for scientific computing called SciPy provides a suite of optimization algorithms. Many of the algorithms are used as a building block in other algorithms, most notably machine learning algorithms in the scikit-learn library.

These optimization algorithms can be used directly in a standalone manner to optimize a function. Most notably, algorithms for local search and algorithms for global search, the two main types of optimization you may encounter on a machine learning project.

In this tutorial, you will discover optimization algorithms provided by the SciPy library.

After completing this tutorial, you will know:

  • The SciPy library provides a suite of different optimization algorithms for different purposes.
  • The local search optimization algorithms available in SciPy.
  • The global search optimization algorithms available in SciPy.

Kick-start your project with my new book Optimization for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

Function Optimization With SciPy

Function Optimization With SciPy
Photo by Manoel Lemos, some rights reserved.

Tutorial Overview

This tutorial is divided into three parts; they are:

  1. Optimization With SciPy
  2. Local Search With SciPy
  3. Global Search With SciPy

Optimization With SciPy

The Python SciPy open-source library for scientific computing provides a suite of optimization techniques.

Many of the algorithms are used as building blocks for other algorithms within the SciPy library, as well as machine learning libraries such as scikit-learn.

Before we review specific techniques, let’s look at the types of algorithms provided by the library.

They are:

  • Scalar Optimization: Optimization of a convex single variable function.
  • Local Search: Optimization of a unimodal multiple variable function.
  • Global Search: Optimization of a multimodal multiple variable function.
  • Least Squares: Solve linear and non-linear least squares problems.
  • Curve Fitting: Fit a curve to a data sample.
  • Root Finding: Find the root (input that gives an output of zero) of a function.
  • Linear Programming: Linear optimization subject to constraints.

All algorithms assume the objective function that is being optimized is a minimization function. If your function is maximizing, it can be converted to minimizing by adding a negative sign to values returned from your objective function.

In addition to the above list, the library also provides utility functions used by some of the algorithms, as well as the Rosenbrock test problem.

For a good overview of the capabilities of the SciPy library for optimization, see:

Now that we have a high-level idea of the types of optimization techniques supported by the library, let’s take a closer look at two groups of algorithms we are more likely to use in applied machine learning. They are local search and global search.

Want to Get Started With Optimization Algorithms?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Local Search With SciPy

Local search, or local function optimization, refers to algorithms that seek the input to a function that results in the minimum or maximum output where the function or constrained region being searched is assumed to have a single optima, e.g. unimodal.

The function that is being optimized may or may not be convex, and may have one or more than one input variable.

A local search optimization may be applied directly to optimize a function if the function is believed or known to be unimodal; otherwise, the local search algorithm may be applied to fine-tune the result of a global search algorithm.

The SciPy library provides local search via the minimize() function.

The minimize() function takes as input the name of the objective function that is being minimized and the initial point from which to start the search and returns an OptimizeResult that summarizes the success or failure of the search and the details of the solution if found.

Additional information about the objective function can be provided if known, such as the bounds on the input variables, a function for computing the first derivative of the function (gradient or Jacobian matrix), a function for computing the second derivative of the function (Hessian matrix), and any constraints on the inputs.

Importantly, the function provides the “method” argument that allows the specific optimization used in the local search to be specified.

A suite of popular local search algorithms are available, such as:

The example below demonstrates how to solve a two-dimensional convex function using the L-BFGS-B local search algorithm.

Running the example performs the optimization and reports the success or failure of the search, the number of function evaluations performed, and the input that resulted in the optima of the function.

Now that we are familiar with using a local search algorithm with SciPy, let’s look at global search.

Global Search With SciPy

Global search or global function optimization refers to algorithms that seek the input to a function that results in the minimum or maximum output where the function or constrained region being searched is assumed to have multiple local optima, e.g. multimodal.

The function that is being optimized is typically nonlinear, nonconvex, and may have one or more than one input variable.

Global search algorithms are typically stochastic, meaning that they make use of randomness in the search process and may or may not manage a population of candidate solutions as part of the search.

The SciPy library provides a number of stochastic global optimization algorithms, each via different functions. They are:

The library also provides the shgo() function for sequence optimization and the brute() for grid search optimization.

Each algorithm returns an OptimizeResult object that summarizes the success or failure of the search and the details of the solution if found.

The example below demonstrates how to solve a two-dimensional multimodal function using simulated annealing.

Running the example performs the optimization and reports the success or failure of the search, the number of function evaluations performed, and the input that resulted in the optima of the function.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.

APIs

Articles

Summary

In this tutorial, you discovered optimization algorithms provided by the SciPy library.

Specifically, you learned:

  • The SciPy library provides a suite of different optimization algorithms for different purposes.
  • The local search optimization algorithms available in SciPy.
  • The global search optimization algorithms available in SciPy.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Get a Handle on Modern Optimization Algorithms!

Optimization for Maching Learning

Develop Your Understanding of Optimization

...with just a few lines of python code

Discover how in my new Ebook:
Optimization for Machine Learning

It provides self-study tutorials with full working code on:
Gradient Descent, Genetic Algorithms, Hill Climbing, Curve Fitting, RMSProp, Adam, and much more...

Bring Modern Optimization Algorithms to
Your Machine Learning Projects


See What's Inside

16 Responses to Function Optimization With SciPy

  1. pawel February 8, 2021 at 7:38 am #

    You don’t even know how happy I am you are making this optimization module.
    Thank you for your impact!

  2. Kala C P February 9, 2021 at 2:39 pm #

    Respected , sir good morning

    This article is superb .And

    I would like to do Ph.D related to this field , can you guide me, now I am persuing Post graduate program in machine learning and artificial intelligence.

    Objective : to find chemical level of fruits and vegetables . Because here, in my country nowadays there are using lots of chemicals to grow the plants. So, if we find the chemical level of fruits and vegetables, it is help us to avoid that food to eat . And give awareness of that eagerness of that chemical to the people .

    So please can you guide me?

    • Jason Brownlee February 10, 2021 at 7:59 am #

      Thanks.

      That sounds like a great project.

      Sorry, I don’t have the capacity to be your research advisor – I recommend discussing your project with your research group/advisor at your university.

  3. Vasanth February 12, 2021 at 5:40 am #

    These are awesome details, Jason. I will investigate thoroughly and get back to you.

  4. S Ray February 15, 2021 at 12:18 am #

    Hi
    I am trying to solve a project where it is Intended to find optimum incentive for insurance agents to maximise Total revenue from premiums paid by subscribers. The agents efforts in hours is a complex exponential function of incentives given and %increase in probability to pay premius is complex exponential function of agents efforts in hours. The total revenue is summation(for all subscribers) of benchmark probability plus %ibcrease in probability to pay premium multiplied by premium amount minus incentive paid to agents. Can I use optimization algorithm here, if yes how? Can you please help me to solve this?

    • Jason Brownlee February 15, 2021 at 5:45 am #

      That sounds like a great project!

      Perhaps start by trying to define an evaluation function for candidate solutions – it might be a simulation or complex calculation. If you can define an objective function and evaluate candidate solutions, you’re on the right track, then start trying different hand-crafted solutions and eventually an optimization algorithm.

  5. Lina April 13, 2021 at 4:38 pm #

    Thanks alot, It is really helpful!!

    do you have any idea about how to use the trained neural model as an objective function to apply optimize.minimize function on it ?

    • Jason Brownlee April 14, 2021 at 6:20 am #

      Sure, create a function that uses your model with vector input and prediction value output.

      • Barnabas November 20, 2021 at 12:15 pm #

        Hi Jason,

        Thank you for your awesome work. I am trying to use a NN model to minimize the opex of an LNG plant. Can you give an example of how a NN model can be used within the scipy.optimize package?

        Thanks in anticipation of your response.

        • Adrian Tam
          Adrian Tam November 20, 2021 at 1:46 pm #

          Not sure I understand it but scipy.optimize is to find the minimum of a user-defined function. Hence you want to define a function that take parameters as input, then in the function, you build a NN model using the parameter, do the training, and use the model to produce some value (e.g., error metric). Then this function can be optimized using scipy.

  6. Aman May 14, 2021 at 12:42 am #

    Jason, any link or blog where I can check the working of scipy optimizers from scratch?

Leave a Reply

Machine Learning Mastery is part of Guiding Tech Media, a leading digital media publisher focused on helping people figure out technology. Visit our corporate website to learn more about our mission and team.