Parametric regression
Estimate physical or statistical model parameters from paired measurements.
SciPy · Function
Fits a callable model to data by nonlinear least squares and returns the optimized parameters with an estimated covariance matrix. Bounds, measurement uncertainties, optimization methods, and solver options can be supplied.
curve_fit minimizes nonlinear least-squares residuals between a callable model and measured data. The model receives the independent variable first and each fitted parameter as a separate argument; the function returns fitted parameters and their estimated covariance matrix.
scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=None, bounds=(-inf, inf), method=None, jac=None, *, full_output=False, nan_policy=None, **kwargs)| Name | Type | Description |
|---|---|---|
f | callable | Model function. Its first argument is the independent variable; remaining arguments are fitted parameters. |
xdata | array_like or object | Independent-variable observations where the data was measured. |
ydata | array_like | Dependent observations, nominally f(xdata, ...). |
p0 | array_like, optional | Initial parameter estimates. If omitted, the initial values default to 1 for each parameter when they can be determined. |
sigma | None or scalar or M-length sequence or M×M array, optional | Measurement uncertainties as a scalar, 1-D standard-deviation array, or 2-D covariance matrix. |
absolute_sigma | bool, optional | Whether sigma represents absolute uncertainty; otherwise covariance is scaled by reduced chi-square. |
check_finite | bool, optional | Whether to check input arrays for finite values before fitting. |
bounds | 2-tuple of array_like or Bounds, optional | Lower and upper parameter bounds. |
method | {‘lm’, ‘trf’, ‘dogbox’}, optional | Optimization method: lm, trf, or dogbox. |
jac | callable or string, optional | Derivative of the model with respect to parameters; forwarded to the least-squares solver. |
kwargs | dict | Additional keyword arguments passed to least_squares. |
full_output | bool, optional | Whether to return additional solver information. |
Returns popt, a 1-D array of optimal parameter values minimizing the squared residuals, and pcov, a 2-D estimated covariance matrix for popt. With full_output=True, it additionally returns infodict, mesg, and ier.
import numpy as np
from scipy.optimize import curve_fit
def model(x, a, b):
return a * np.exp(-b * x)
x = np.linspace(0, 4, 20)
y = model(x, 2.5, 0.8) + 0.05 * np.random.default_rng(1).normal(size=x.size)
popt, pcov = curve_fit(model, x, y, p0=(1, 1))
print(popt)
print(np.sqrt(np.diag(pcov)))Fits an exponential model and prints the optimized parameters plus their estimated standard deviations.
Estimate physical or statistical model parameters from paired measurements.
Incorporate known observation errors or correlated measurement uncertainty.
Enforce physically meaningful lower and upper limits on fitted parameters.
curve_fit performs nonlinear least-squares fitting under the model assumption ydata = f(xdata, *params) + eps. It returns popt, which minimizes the squared residuals, and pcov, an estimated covariance matrix for those parameters.
Follow-up: How would you constrain parameters? Pass bounds, either as a pair of lower and upper bounds or as Bounds objects; bounded problems use a method such as trf or dogbox.
The model callable must accept the independent variable as its first argument and the parameters being fitted as separate subsequent arguments. xdata contains measured independent-variable values, while ydata contains the corresponding dependent observations.
Follow-up: How can uncertainty be reported? Approximate parameter standard deviations are commonly obtained from sqrt of the diagonal of pcov, provided the covariance estimate is reliable.
sigma can represent standard deviations for individual observations or a full error covariance matrix. With a 1-D sigma, residuals are weighted by sigma; with a 2-D sigma, the objective uses the covariance-weighted quadratic form.
Follow-up: What does absolute_sigma change? If false, the covariance is scaled using the reduced chi-square; if true, sigma is treated in absolute terms.
scipy.optimize.least_squaresscipy.optimize. leastsqscipy.optimize. minimize