0
0
SciPydata~5 mins

Why optimization finds best solutions in SciPy

Choose your learning style9 modes available
Introduction

Optimization helps us find the best answer to a problem by trying different options and picking the one that works best.

Choosing the cheapest way to deliver packages to many places.
Finding the best mix of ingredients to make a tasty recipe.
Adjusting settings in a machine to get the highest output.
Planning the shortest route for a road trip.
Setting prices to maximize profit in a store.
Syntax
SciPy
from scipy.optimize import minimize

result = minimize(function_to_minimize, initial_guess)

function_to_minimize is the rule that tells how good or bad a choice is.

initial_guess is where the search starts to find the best answer.

Examples
This finds the number x that makes (x - 3)^2 smallest. The answer is close to 3.
SciPy
from scipy.optimize import minimize

def f(x):
    return (x - 3) ** 2

result = minimize(f, 0)
print(result.x)
This finds the point (x[0], x[1]) closest to (1, 2) by minimizing the sum of squares.
SciPy
from scipy.optimize import minimize

def f(x):
    return (x[0] - 1) ** 2 + (x[1] - 2) ** 2

result = minimize(f, [0, 0])
print(result.x)
Sample Program

This program finds the value of x that makes the cost smallest. The cost is smallest when x is 5.

SciPy
from scipy.optimize import minimize

def cost(x):
    return (x - 5) ** 2 + 10

start = 0
result = minimize(cost, start)
print(f"Best x: {result.x[0]:.2f}")
print(f"Minimum cost: {result.fun:.2f}")
OutputSuccess
Important Notes

Optimization tries many values to find the best one, but it may get stuck if the problem is tricky.

Choosing a good starting point can help find the best solution faster.

Summary

Optimization helps find the best answer by testing options.

We use functions to measure how good each option is.

Scipy's minimize tries to find the smallest value of a function.