Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Module

scipy.optimize

scipy.optimize provides numerical routines for minimization, global optimization, least-squares fitting, curve fitting, and root solving. It includes methods for scalar, multivariate, constrained, unconstrained, and equation-system problems.

How it works

The module groups numerical routines for choosing minima or solving equations. For multivariate scalar minimization, pass an objective function and initial point to minimize; select other routines when the problem is global, least-squares, scalar, or an equation-system task.

Syntax

import scipy.optimize

Parameters

NameTypeDescription
funcallableObjective function that maps the candidate variable vector to a scalar value.
x0array_likeInitial guess for the variables.

Returns

Optimization routines return method-specific result objects or values; the cited overview page does not define a single module-wide return type.

Key points

  • The package is a collection of numerical optimization algorithms rather than one single optimizer.
  • minimize handles both constrained and unconstrained scalar objectives with multiple variables.
  • Global-search functionality includes basinhopping, differential_evolution, shgo, and dual_annealing.
  • root addresses systems of equations and offers several solver families.

Common mistakes

  • Algorithm choice affects convergence behavior; select a method suited to the problem and its constraints rather than assuming one default is universally best.
  • Global routines target broader search problems than local minimizers; use them when local convergence may miss the desired basin.

Example: Minimize a scalar function

import numpy as np
from scipy.optimize import minimize

result = minimize(lambda x: (x[0] - 3)**2, x0=np.array([0.0]))
print(result.x, result.fun)

Minimizes a one-variable quadratic from an initial guess and prints the solution and objective value.

When to use it

Constrained model tuning

Find parameters that minimize a scalar objective with bounds or constraints.

Least-squares fitting

Fit parameters by minimizing residual errors.

Root finding

Locate zeros of a scalar function or solve an equation system.

Global optimization

Search for optima when local methods may be insufficient.

Interview questions

QWhat kinds of problems does scipy.optimize cover?

scipy.optimize provides routines for several numerical problem types, including constrained and unconstrained multivariate minimization, global optimization, least-squares fitting, scalar minimization and root finding, and multivariate equation solving.

Follow-up: Which routine fits nonlinear residual fitting? least_squares; for parametric curve fitting, the documentation also identifies curve_fit.

QWhen would you use minimize?

Use minimize for unconstrained or constrained minimization of a multivariate scalar function. It supports multiple algorithms, so the method can be selected according to the problem.

Follow-up: What if the objective is one-dimensional? Use minimize_scalar rather than treating it as a multivariate minimization problem.

QHow does equation solving differ from minimization in this module?

Use root to solve multivariate systems of equations. The documented algorithm families include hybrid Powell, Levenberg–Marquardt, and large-scale Newton–Krylov approaches.

Follow-up: What is the scalar counterpart? Use root_scalar for univariate root finding.

  • minimize
  • basinhopping
  • differential_evolution
  • shgo
  • dual_annealing
  • least_squares
  • curve_fit
  • minimize_scalar

Source: Optimization (scipy.optimize). Checked 2026-10-15.