LMFit

1.3.4 · active · verified Sun Apr 12

LMFit is a Python library providing a high-level interface for non-linear least-squares minimization and curve fitting. It extends and builds upon `scipy.optimize` by introducing `Parameter` objects, which simplify the process of defining, constraining, and managing fitting variables. Currently at version 1.3.4, `lmfit` maintains an active development cycle with regular releases addressing bug fixes, new features, and dependency updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates fitting an exponential decay model to noisy data using `lmfit.Model`. It covers defining the model function, creating `Model` and `Parameters` objects, setting initial guesses and constraints, performing the fit, and reporting the results.

import numpy as np
from lmfit import Model

# 1. Generate some data
x = np.linspace(0, 10, 100)
y_true = 3.0 * np.exp(-0.5 * x) + 2.0
np.random.seed(0)
y_data = y_true + np.random.normal(0, 0.2, x.shape)

# 2. Define your model function
def exponential_decay(x, amplitude, decay, offset):
    return amplitude * np.exp(-decay * x) + offset

# 3. Create a Model instance from your function
exp_model = Model(exponential_decay)

# 4. Create initial parameters with guess() or manually
# guess() method often requires x and y data for good initial estimates
params = exp_model.make_params(amplitude=5., decay=0.1, offset=1.)

# Or, for more refined control:
# params = exp_model.guess(y_data, x=x)

# Optionally set bounds or fix parameters
params['amplitude'].set(min=0.0)
params['decay'].set(min=0.0)

# 5. Fit the model to the data
result = exp_model.fit(y_data, params, x=x)

# 6. Print the fitting report
print(result.fit_report())

# You can also access best-fit parameters, statistics, etc.
# print(f"Best-fit amplitude: {result.params['amplitude'].value:.3f}")
# print(f"Reduced Chi-square: {result.redchi:.3f}")

view raw JSON →