Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

How to Fit a Nonlinear Model with SciPy `leastsq`

Use SciPy's `leastsq` to minimize squared residuals for an unbounded nonlinear fit. This guide covers residual functions, initial guesses, Jacobians, solver status, covariance estimates, and when to choose `least_squares` or `curve_fit`.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

scipy.optimize.leastsq fits a nonlinear model by minimizing the sum of squared residuals between observed values and model predictions. Give it a function that returns one residual per observation, an initial parameter guess, and any fixed data the function needs. The method is a local iterative solver: the starting guess and model formulation matter, and you should check its termination status rather than treating every returned parameter vector as a successful fit.

What `leastsq` minimizes

For a parameter vector x, your function returns a vector of residuals. SciPy seeks parameters that minimize the sum of those residuals squared. For data fitting, a residual is commonly the observed value minus the model prediction:

As an Amazon Associate I earn from qualifying purchases.

residual = observed - model(xdata, parameters)

Return the residual vector itself—not a scalar sum of squares and not a vector you have already squared. The routine performs the squaring and summation. It expects floating-point residuals without NaNs, and the number of residuals, M, must be at least the number of unknown parameters, N.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Fit a nonlinear model step by step

This example fits an exponential model, y = a * exp(b * t) + c, to measured values. The parameter vector contains a, b, and c; time and observations are fixed data passed through args.

  1. Import the solver and prepare the observations.
    import numpy as np
    from scipy.optimize import leastsq
    
    t = np.array([0.0, 1.0, 2.0, 3.0, 4.0])
    y_observed = np.array([1.1, 2.0, 3.5, 6.1, 10.8])
  2. Define the model and return one residual per observation.
    def model(t, a, b, c):
        return a * np.exp(b * t) + c
    
    def residuals(params, t, y):
        a, b, c = params
        return y - model(t, a, b, c)

    The parameter vector is the first argument to the residual function. Additional fixed values belong in args.

  3. Choose an initial estimate and call the solver.
    x0 = np.array([1.0, 0.5, 0.0])
    
    result = leastsq(
        residuals,
        x0,
        args=(t, y_observed),
        full_output=True,
    )
    
    params, cov_x, infodict, mesg, ier = result
    print(params)
    print(ier, mesg)

    x0 is the starting estimate, not a constraint or a guarantee of the final answer. The example illustrates the API; whether a particular fit is appropriate depends on the data and model.

  4. Check the termination result before using the fit.

    The documented solution-found status values are ier equal to 1, 2, 3, or 4. Read mesg and inspect diagnostics in infodict as well. If the call fails, the returned parameter vector is the last iterate, not a confirmed solution.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Supply a Jacobian when appropriate

You can pass Dfun to provide the Jacobian of the residual function, replacing SciPy’s numerical estimation. The Jacobian must correspond to the residuals your function returns. The col_deriv option indicates whether derivatives are supplied down columns rather than across rows; set it to match the orientation of your Jacobian. An orientation mismatch can produce incorrect results or errors.

Set stopping and evaluation options deliberately

The SciPy v1.18.0 reference documents these controls. They govern stopping behavior and scaling, not a promise that the fitted parameters are accurate.

  • ftol sets a stopping criterion based on change in the objective function.
  • xtol sets a stopping criterion based on change in the solution.
  • gtol sets a criterion based on residual/Jacobian orthogonality.
  • maxfev limits function evaluations. Its documented default is 200 * (N + 1) when Dfun is not supplied, and 100 * (N + 1) when it is supplied.
  • diag accepts positive scale factors for the variables. Scaling can matter when parameters have very different magnitudes.
  • factor controls the initial step bound and should be in the interval (0.1, 100).

Defaults and behavior can change between SciPy releases; check the API reference matching the version installed in your environment. These details refer to the SciPy v1.18.0 reference.

Interpret the returned values and covariance estimate

Without full_output=True, the routine returns the solution and an integer termination flag. With that option enabled, it also returns cov_x, infodict, mesg, and ier, as shown above. Use the status and message to distinguish a successful termination from a last iterate returned after failure.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

cov_x is an inverse-Hessian/Jacobian-based approximation, not by itself a parameter covariance matrix. SciPy’s reference says to multiply it by the residual variance to obtain a covariance estimate. If cov_x is None, the approximation is unavailable because the curvature is numerically flat in at least one parameter direction. This calculation is not a general guarantee of parameter uncertainty.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to use another SciPy fitting API

Task API Why it may fit better
Unbounded residual least squares with the MINPACK interface leastsq A focused wrapper around MINPACK’s lmdif and lmder algorithms.
Parameter bounds or robust loss functions least_squares Supports bounds and selectable methods and loss functions; its lm method is also MINPACK-based.
Fit a named model function to observed xdata and ydata curve_fit Provides a higher-level model-fitting interface with parameter guesses, bounds, and method selection. Its lm method routes to leastsq; other methods route to least_squares.

These API distinctions and the defaults above are documented in SciPy’s v1.18.0 online reference; consult the documentation for your installed release when relying on specific options or defaults.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.