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

SciPy · Function

optimize.curve_fit

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.

Since
0.14.0

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
fcallableModel function. Its first argument is the independent variable; remaining arguments are fitted parameters.
xdataarray_like or objectIndependent-variable observations where the data was measured.
ydataarray_likeDependent observations, nominally f(xdata, ...).
p0array_like, optionalInitial parameter estimates. If omitted, the initial values default to 1 for each parameter when they can be determined.
sigmaNone or scalar or M-length sequence or M×M array, optionalMeasurement uncertainties as a scalar, 1-D standard-deviation array, or 2-D covariance matrix.
absolute_sigmabool, optionalWhether sigma represents absolute uncertainty; otherwise covariance is scaled by reduced chi-square.
check_finitebool, optionalWhether to check input arrays for finite values before fitting.
bounds2-tuple of array_like or Bounds, optionalLower and upper parameter bounds.
method{‘lm’, ‘trf’, ‘dogbox’}, optionalOptimization method: lm, trf, or dogbox.
jaccallable or string, optionalDerivative of the model with respect to parameters; forwarded to the least-squares solver.
kwargsdictAdditional keyword arguments passed to least_squares.
full_outputbool, optionalWhether to return additional solver information.

Returns

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.

Key points

  • Use curve_fit when a parametric callable should be fitted to observed data by nonlinear least squares.
  • Define the model with x first, followed by one positional argument for every parameter to estimate.
  • xdata supplies the independent-variable observations and can be array-like or another accepted object.
  • ydata contains the dependent observations and should correspond elementwise to the model evaluations.
  • pcov estimates parameter covariance, allowing diagonal-based uncertainty estimates when the fit is well-conditioned.
  • The default optimization method is lm without bounds and trf when bounds are supplied.

Common mistakes

  • Do not use the Levenberg–Marquardt method when there are fewer observations than fitted parameters; use a bounded-capable method instead.
  • Use float64-compatible inputs and model outputs to avoid incorrect optimization results.

Example: Fit an exponential curve

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.

When to use it

Parametric regression

Estimate physical or statistical model parameters from paired measurements.

Weighted and correlated fitting

Incorporate known observation errors or correlated measurement uncertainty.

Bounded optimization

Enforce physically meaningful lower and upper limits on fitted parameters.

Interview questions

QWhat does curve_fit return and optimize?

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.

QWhat signature must the model function have?

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.

QHow does sigma affect the fit?

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_squares
  • scipy.optimize. leastsq
  • scipy.optimize. minimize

Source: scipy.optimize.curve_fit — SciPy v1.16.0 Manual. Checked 2026-10-15.