Nonlinear regression
Fit parameters in a nonlinear model by returning the difference between predictions and observations.
SciPy · Function
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.
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.
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)| Name | Type | Description |
|---|---|---|
fun | callable | Residual function mapping an n-dimensional parameter vector to m residuals. |
x0 | array_like | Initial estimate of the parameters. |
jac | {'2-point', '3-point', 'cs', callable} or array_like | Jacobian specification: callable, array, or finite-difference scheme such as '2-point' or '3-point'. |
bounds | 2-tuple of array_like | Lower and upper bounds for the variables; defaults to no bounds. |
method | {'trf', 'dogbox', 'lm'} | Algorithm used for minimization: 'trf', 'dogbox', or 'lm'. |
ftol, xtol, gtol | float or None | Termination tolerances for cost change, parameter change, and gradient optimality. |
x_scale | array_like or 'jac' or 1.0 | Characteristic scale of each variable used to adjust the step size. |
loss | str or callable | Residual-loss model, including 'linear', 'soft_l1', 'huber', 'cauchy', and 'arctan'. |
f_scale | float | Residual scale used by the loss function. |
diff_step, tr_solver, tr_options, jac_sparsity, max_nfev, verbose, args, kwargs | various | Finite-difference step size, solver options, sparsity pattern, evaluation limit, verbosity, and extra arguments. |
OptimizeResult containing the solution vector x, cost, residual vector fun, Jacobian jac, optimality, active bounds, number of function evaluations, status, message, and success flag.
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.
Fit parameters in a nonlinear model by returning the difference between predictions and observations.
Constrain physical or operational parameters to valid intervals during optimization.
Fit data containing atypical measurements without letting a few outliers dominate.
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.
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.
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_squaresscipy.optimize.curve_fitscipy.optimize.minimizescipy.optimize.root