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.

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.

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.



Author: Shantun Parmar

Comments are closed.