Skip to main content
https://dev.azure.com/lmfit/lmfit-py/_apis/build/status/lmfit.lmfit-py?branchName=master https://codecov.io/gh/lmfit/lmfit-py/branch/master/graph/badge.svg https://img.shields.io/pypi/v/lmfit.svg https://img.shields.io/pypi/dm/lmfit.svg https://img.shields.io/badge/docs-read-brightgreen https://zenodo.org/badge/4185/lmfit/lmfit-py.svg

Overview

The lmfit Python library provides tools for non-linear least-squares minimization and curve fitting. The goal is to make these optimization algorithms more flexible, more comprehensible, and easier to use, with the key feature of casting variables in minimization and fitting routines as named parameters that can have many attributes beside just a current value.

LMfit is a pure Python package – built on top of Scipy and Numpy – and is easy to install with pip install lmfit.

For questions, comments, and suggestions, please use the LMfit google mailing list or Github discussions. For software issues and bugs, use Github Issues, but please read Contributing.md before creating an Issue.

Parameters and Minimization

LMfit provides optimization routines similar to (and based on) those from scipy.optimize, but with a simple, flexible approach to parameterizing a model for fitting to data using named parameters. These named Parameters can be held fixed or freely adjusted in the fit, or held between lower and upper bounds. Parameters can also be constrained as a simple mathematical expression of other Parameters.

A Parameters object (which acts like a Python dictionary) contains named parameters, and can be built as with:

import lmfit
fit_params = lmfit.Parameters()
fit_params['amp'] = lmfit.Parameter(value=1.2)
fit_params['cen'] = lmfit.Parameter(value=40.0, vary=False)
fit_params['wid'] = lmfit.Parameter(value=4, min=0)
fit_params['fwhm'] = lmfit.Parameter(expr='wid*2.355')

or using the equivalent:

fit_params = lmfit.create_params(amp=1.2,
                                 cen={'value':40, 'vary':False},
                                 wid={'value': 4, 'min':0},
                                 fwhm={'expr': 'wid*2.355'})

In the general minimization case (see below for Curve-fitting), the user will also write an objective function to be minimized (in the least-squares sense) with its first argument being this Parameters object, and additional positional and keyword arguments as desired:

def myfunc(params, x, data, someflag=True):
    amp = params['amp'].value
    cen = params['cen'].value
    wid = params['wid'].value
    ...
    return residual_array

For each call of this function, the values for the params may have changed, subject to the bounds and constraint settings for each Parameter. The function should return the residual (i.e., data-model) array to be minimized.

The advantage here is that the function to be minimized does not have to be changed if different bounds or constraints are placed on the fitting Parameters. The fitting model (as described in myfunc) is instead written in terms of physical parameters of the system, and remains independent of what is actually varied in the fit. In addition, which parameters are adjusted and which are fixed happens at run-time, so that changing what is varied and what constraints are placed on the parameters can easily be modified by the user in real-time data analysis.

To perform the fit, the user calls:

result = lmfit.minimize(myfunc, fit_params, args=(x, data), kws={'someflag':True}, ....)

After the fit, a MinimizerResult class is returned that holds the results of the fit (e.g. fitting statistics and optimized parameters). The dictionary result.params contains the best-fit values, estimated standard deviations, and correlations with other variables in the fit.

By default, the underlying fit algorithm is the Levenberg-Marquardt algorithm with numerically-calculated derivatives from MINPACK’s lmdif function, as used by scipy.optimize.leastsq. Most other solvers that are present in scipy (e.g. Nelder-Mead, differential_evolution, basin-hopping, and more) are also supported.

Curve-Fitting with lmfit.Model

One of the most common use of least-squares minimization is for curve fitting, where minimization of data-model, or (data-model)*weights. Using lmfit.minimize as above, the objective function would take data and weights, effectively calculate the model, and return the value of (data-model)*weights.

To simplify this, and make curve-fitting more flexible, lmfit provides a Model class that wraps a model function that represents the model (without the data or weights). Parameters are then automatically found from the named arguments of the model function. In addition, simple model functions can be readily combined and reused, and several common model functions are included in lmfit.

Exploration of Confidence Intervals

LMfit tries to always estimate uncertainties in fitting parameters and correlations between them. It does this even for those methods where the corresponding scipy.optimize routines do not estimate uncertainties. LMfit also provides methods to explicitly explore and evaluate the confidence intervals in fit results.

Release files for lmfit 1.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lmfit 1.3.4
File Size Uploaded
lmfit-1.3.4.tar.gz 630.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lmfit 1.3.4
File Interpreter ABI Platform
lmfit-1.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 728.4 kB

Release files / lmfit-1.3.4.tar.gz

Download URL lmfit-1.3.4.tar.gz
Size 630.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3c22c28c43f717f6c5b4a3bd81e893a2149739c26a592c046f2e33c23cfbe497
BLAKE2b-256 checksum
How to use checksums
5ae5a35942aed2de95e228728c34609b51fe3ec9182398eac50d288eef313aa2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / lmfit-1.3.4-py3-none-any.whl

Download URL lmfit-1.3.4-py3-none-any.whl
Size 97.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
afce1593b42324d37ae2908249b0c55445e2f4c1a0474ff706a8e2f7b5d949fa
BLAKE2b-256 checksum
How to use checksums
387e7b91c89a4cf0f543a83be978657afb20c86af6d725253e319589dcc4ce52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

1.3.4 This release

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.9.15

1 release file

0.9.14

1 release file

0.9.13

1 release file

0.9.12

1 release file

0.9.11

1 release file

0.9.10

1 release file

0.9.9

1 release file

0.9.8

1 release file

0.9.7

1 release file

0.9.6

1 release file

0.9.5

3 release files

0.9.4

3 release files

0.9.3

1 release file

0.9.2

1 release file

0.9.1

1 release file

0.9.0

1 release file

0.8.3

1 release file

0.8.2

1 release file

0.8.1

3 release files

0.8.0

4 release files

0.7.4

4 release files

0.7.2

4 release files

0.7

4 release files

0.6

4 release files

0.5

4 release files

0.4

4 release files

0.3

4 release files

0.2

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page