How To Use The Scipy Minimize Function in Python?

This easy-to-understand Python code shows you how to minimize a SciPy function using the Scipy ‘minimize()’ function in Python.

Objective: How To Use The Scipy Minimize Function in Python?

This beginner-friendly Python code example will show you how to minimize a function using the SciPy minimize() function in Python

How To Minimize A Function in Python?

There are two ways to minimize a function in Python-

  1. Using The SciPy ‘minimize’ Function- The SciPy minimize function is a powerful tool for optimizing functions in Python. It can be used to find the minimum or maximum value of a function, given a set of constraints. 

  2. Using Gradient Descent- The gradient descent algorithm works by iteratively moving in the direction of the negative gradient of the objective function. The gradient of a function is a vector that points in the direction of the steepest ascent of the function.

There are a few commonly used optimization algorithms that can be used to minimize functions, such as CG, BFGS, Newton-CG, L-BFGS-B, TNC, COBYLA, and SLSQP. In this example, we will use the CG, BFGS, and SLSQP algorithms along with the ‘minimize()’ SciPy function

How To Use Python Minimize() Function in SciPy?

The SciPy minimize function is a powerful tool for optimizing functions in Python. To use the minimize function, you must first define the objective function you want to optimize. The objective function is a function that takes one or more variables as input and returns a single value. The minimize function will then try to find the set of input values that minimizes the objective function.

Along with the objective function, you can also specify a few other options for the minimize function, such as

  • The initial guess for the solution,

  • The type of optimization algorithm to use,

  • Any constraints on the solution.

Once you have specified all options, you can call the minimize function to find the optimal solution. The minimize function will return a result object that contains information about the solution, such as the optimal solution values, the objective function value at the solution, and the number of iterations required to find the solution.

scipy.optimize.minimize Example

Here is a simple SciPy minimize example showing how to use the ‘minimize()’ Python function to find the minimum value of the quadratic function f(x) = x^2 + 2x + 1-

import scipy.optimize as opt

def func(x):

    return x**2 + 2*x + 1

x0 = 0

res = opt.minimize(func, x0)

print(res)

The output of the above code is-

fun: 0.0

 hess_inv: array([[0.5]])

      jac: array([0.])

  message: 'Optimization terminated successfully.'

     nfev: 6

      nit: 2

     njev: 3

   status: 0

  success: True

        x: array([-1.00000001])

How To Use Python Minimize Function With Parameters

Let us understand how to use the Python minimization function and the CG, BFGS, and SLSQP algorithms with the help of simple scipy.optimize.minimize examples.

Python- Minimize A Function Using SciPy ‘minimize()’ function With CG

The following steps will show you how to use the Python minimize() SciPy function with the CG algorithm to minimize a function.

Step 1 - Import Necessary Libraries

The first step is to import the minimize function from the scipy.optimize module.

from scipy.optimize import minimize

Check Out These End-To-End Solved Python Projects To Accelerate Your Data Science Career

Step 2 - Define The Objective Function

The next step is to define the objective function we want to minimize. In this case, the objective function is simply x^2 + x + 2.

def eqan(x):

  return x**2 + x + 2

Step 3 - Minimize The Objective Function Using 'minimize()' And CG

The final step is to call the minimize function to minimize the objective function using the CG method. The 0 argument is the initial guess for the solution.

min_val = minimize(eqan, 0, method=’CG’)

print(min_val)

The output of the above code is-

 fun: 1.75

     jac: array([-0.])

 message: 'Optimization terminated successfully.'

    nfev: 10

     nit: 2

    njev: 5

  status: 0

 success: True

       x: array([-0.49999999])

Minimize A Function (Python) Using SciPy ‘minimize()’ With BFGS

The following steps will show you how to use the Python minimize() SciPy function with the CG algorithm to minimize a function.

Step 1 - Import Necessary Libraries

The first step is to import the minimize function from the scipy.optimize module.

from scipy.optimize import minimize

Step 2 - Define The Objective Function

The next step is to define the objective function we want to minimize. In this case, the objective function is simply x^3 + x + 2.

def eqan1(x):

  return x**3 + x + 2

Step 3 - Minimize The Objective Function Using 'minimize()' And CG

The final step is to call the minimize function to minimize the objective function using the BFGS method. The 0 argument is the initial guess for the solution.

min_val_bfgs = minimize(eqan1, 0, method=’BFGS’)

print(min_val_bfgs)

The output of the above code is-

fun: array([-1.07374285e+09])

 hess_inv: array([[-0.00032552]])

      jac: array([3145728.])

  message: 'Desired error not necessarily achieved due to precision loss.'

     nfev: 236

      nit: 1

     njev: 112

   status: 2

  success: False

        x: array([-1024.])

Using SciPy ‘minimize()’ Function Python With SLSQP

The following steps will show you how to use the Python minimize() SciPy function with the SLSQP algorithm to minimize a function.

Step 1 - Import Necessary Libraries

The first step is to import the minimize function from the scipy.optimize module.

from scipy.optimize import minimize

Step 2 - Define The Objective Function

The next step is to define the objective function we want to minimize. In this case, the objective function is simply x^2 + x + 2.

def eqan2(x):

  return x**2 + x + 2

Step 3 - Minimize The Objective Function Using 'minimize()' And SLSQP

The final step is to call the minimize function to minimize the objective function using the SLSQP method. The 0 argument is the initial guess for the solution.

min_val_slsqp = minimize(eqan2, 0, method=’SLSQP’)

print(min_val_slsqp)

The output of the above code is-

 fun: array([1.75])

     jac: array([-1.49011612e-08])

 message: 'Optimization terminated successfully'

    nfev: 5

     nit: 2

    njev: 2

  status: 0

 success: True

       x: array([-0.5])

Dive Deeper Into The SciPy Minimize Function In Python With ProjectPro

This step-by-step Python code example offers valuable insights into using the SciPy minimize() function in Python for optimizing functions. We have covered the fundamental steps to minimize a function and learned about its application with various optimization algorithms such as CG, BFGS, and SLSQP. Furthermore, if you want to expand your Python skillset and expertise and apply it to real-world data science and machine learning solutions, we recommend you explore the ProjectPro platform. By engaging with over 270 end-to-end solved projects in the ProjectPro repository, you can gain the skills and expertise needed to excel in data science and machine learning.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.