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

SciPy · Function

optimize.least_squares

Solves nonlinear least-squares problems, optionally with variable bounds and robust losses. It returns an OptimizeResult describing the fitted parameters, residuals, convergence, and local optimality.

How it works

Pass a callable that maps the parameter vector to residuals, then choose an initial point and optional bounds, Jacobian strategy, solver, or robust loss. The function iteratively searches for a local minimum of the residual-based cost and returns an OptimizeResult containing the solution, residuals, Jacobian, optimality information, and termination status.

Syntax

scipy.optimize.least_squares(fun, x0, jac='2-point', bounds=(-inf, inf), method='trf', ftol=1e-08, xtol=1e-08, gtol=1e-08, x_scale=None, loss='linear', f_scale=1.0, diff_step=None, tr_solver=None, tr_options=None, jac_sparsity=None, max_nfev=None, verbose=0, args=(), kwargs=None, callback=None, workers=None)

Parameters

NameTypeDescription
funcallableResidual function mapping an n-dimensional parameter vector to m residuals.
x0array_likeInitial estimate of the parameters.
jac{'2-point', '3-point', 'cs', callable} or array_likeJacobian specification: callable, array, or finite-difference scheme such as '2-point' or '3-point'.
bounds2-tuple of array_likeLower and upper bounds for the variables; defaults to no bounds.
method{'trf', 'dogbox', 'lm'}Algorithm used for minimization: 'trf', 'dogbox', or 'lm'.
ftol, xtol, gtolfloat or NoneTermination tolerances for cost change, parameter change, and gradient optimality.
x_scalearray_like or 'jac' or 1.0Characteristic scale of each variable used to adjust the step size.
lossstr or callableResidual-loss model, including 'linear', 'soft_l1', 'huber', 'cauchy', and 'arctan'.
f_scalefloatResidual scale used by the loss function.
diff_step, tr_solver, tr_options, jac_sparsity, max_nfev, verbose, args, kwargsvariousFinite-difference step size, solver options, sparsity pattern, evaluation limit, verbosity, and extra arguments.

Returns

OptimizeResult containing the solution vector x, cost, residual vector fun, Jacobian jac, optimality, active bounds, number of function evaluations, status, message, and success flag.

Key points

  • Use least_squares for nonlinear residual fitting when variables may need lower and upper limits.
  • Choose a robust loss to limit how strongly unusually large residuals affect the fitted parameters.
  • The bounds argument accepts lower and upper limits; its default represents an unconstrained problem.
  • The trf method is a strong general default for bounded problems and scales well to sparse systems.
  • dogbox is intended mainly for smaller bounded systems and uses rectangular trust regions.

Common mistakes

  • Avoid the dogbox method when the Jacobian may be rank deficient.

Example: Solve nonlinear residuals

import numpy as np
from scipy.optimize import least_squares

def residuals(x):
    return np.array([10 * (x[1] - x[0]**2), 1 - x[0]])

result = least_squares(residuals, x0=np.array([-1.2, 1.0]))
print(result.x)

Defines residuals for a two-variable nonlinear system and minimizes their squared values from an initial guess.

When to use it

Nonlinear regression

Fit parameters in a nonlinear model by returning the difference between predictions and observations.

Bounded calibration

Constrain physical or operational parameters to valid intervals during optimization.

Robust estimation

Fit data containing atypical measurements without letting a few outliers dominate.

Interview questions

QWhat problem does least_squares solve?

The callable receives the current parameter vector and must return an m-dimensional residual array. The solver minimizes one half of the sum of the transformed squared residuals, subject to any supplied bounds.

Follow-up: How would you reduce the effect of outliers? Set a robust loss such as soft_l1 or huber and tune f_scale so residuals below that scale remain close to ordinary least squares.

QHow do the method choices differ?

trf is the default and is generally robust, especially for bounded or large sparse problems. dogbox uses rectangular trust regions and is aimed at smaller bounded problems, while lm is a MINPACK Levenberg–Marquardt method.

Follow-up: Which method supports sparse Jacobians? trf and dogbox support them; lm does not.

QHow can Jacobian computation be controlled?

The Jacobian may be supplied as a callable, an array, or a finite-difference scheme such as 2-point or 3-point. jac_sparsity can describe its sparsity pattern and can substantially reduce finite-difference work for sparse problems.

Follow-up: What happens if the Jacobian is poorly scaled? Provide a suitable x_scale or use the solver's scaling option so steps reflect parameter sensitivity.

  • scipy.optimize.least_squares
  • scipy.optimize.curve_fit
  • scipy.optimize.minimize
  • scipy.optimize.root

Source: scipy.optimize.least_squares — SciPy v1.18.0 Manual. Checked 2026-10-15.