Minimizing a function with scipy.optimize

Einblick Content Team - February 27th, 2023

Minimization is one of the key pillars (and problems to be solved) in data science. For example, we want to minimize cost, while maximizing (another optimization problem) profit. We want to figure out how to minimize cost, maximize profit, given a particular set of constraints, such as shipping budget, manufacturing hours, etc. The scipy library provides the scipy.optimize.minimize() function, which is used to find the minimum value of a given objective function. In this tutorial, we'll be discussing the steps to use this function in Python.

The code is available in the yellow data zone of the below Python canvas. Feel free to open and fork it or continue reading for more in-depth information about the function.

1. Imports

import numpy as np
import scipy.optimize as opt

2. Define the function to minimize

In this case, we are turning the below function into Python:

f(x)=x2+10sinxf(x) = x^2 + 10\sin{x}
# Define the function to minimize
def f(x):
    return x**2 + 10*np.sin(x)

3. Minimize the function

# Find the minimum value of the function
result = opt.minimize(f, x0=-2)

NOTE: scipy.optimize.minimize() has two mandatory arguments (along with many optional ones that we won't expand on here). You can read the details on their documentation site.

  • fun: the objective function that will be minimized
  • x0: an initial guess, can be a single element or an array if you are working with a multivariate function, of where the minimum lies

In practice, if your initial guess, x0, is near a local minimum, but not the global minimum, the function may return the local minimum rather than the global minimum.

4. Print and check results

# Print message indicating why the process terminated
print(result.message)

# Print the minimum value of the function
print(result.fun)

# Print the x-value resulting in the minimum value
print(result.x)

The scipy.optimize.minimize() function has several ways to check on whether or not a minimum was successfully found, and what that minimum is. In the above code chunk, we used three of these attributes:

  • message: gives a short explanation of why the minimization process terminated
  • fun: gives the minimum value of the function
  • x: gives the value of x that results in the minimum value

Output:

Optimization terminated successfully.
-7.945823375615284
[-1.30644002]

Based on the results, we know that the minimization process ended successfully. The minimum found is -7.946, when x = -1.306.

BONUS: scipy.optimize with Generative AI

In Einblick, we've implemented an AI agent, called Einblick Prompt, which can create data workflows from as little as one sentence. In the below canvas, we used generative AI to get the same results we coded up manually above:

Using Generative AI in Einblick

  1. Open the canvas
  2. Fork the canvas
  3. Right-click anywhere in the canvas > Prompt
  4. Type in: "Use scipy.optimize to find the minimum value of function f(x) = x^2 + 10sinx"
  5. Run the code in Einblick's data notebook immediately

Test out different functions and prompts, and get results in seconds!

About

Einblick is an AI-native data science platform that provides data teams with an agile workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.

Start using Einblick

Pull all your data sources together, and build actionable insights on a single unified platform.

  • All connectors
  • Unlimited teammates
  • All operators