Non-Linear Least Squares Minimization, with flexible Parameter settings, based on scipy.optimize.leastsq, and with many additional classes and methods for curve fitting
Python Other
Failed to load latest commit information.
.github DOC: update May 18, 2018
NIST_STRD rearrange NIST tests to make available from examples and tests May 1, 2015
asv_benchmarking update asv conf May 30, 2017
conda-recipe conda recipe… Oct 15, 2015
doc DOC: update solvers in docstrings/documentation. May 13, 2018
examples DOC: set random.seed for reproducible examples in the documentation. May 13, 2018
lmfit fixed #486: Errors while setting Parameter expressions persisting Aug 4, 2018
tests fixed #486: Errors while setting Parameter expressions persisting Aug 4, 2018
.gitattributes add line-ending preference Sep 20, 2015
.gitignore BLD: add the examples to the distribution and ignore .pytest* files May 15, 2018
.travis.yml TST: add python3.7 to Travis CI testing Jul 16, 2018
INSTALL DOC: update INSTALL. May 13, 2018
LICENSE MAINT: Added scipy licence to reflect that _nan_policy was modified f… Jun 27, 2016 BLD: add missing Jupyter notebook examples May 18, 2018
README.rst DOC: update README.rst to reflect that since v0.9 the parameters pass… Oct 30, 2017
THANKS.txt update THANKS May 14, 2018 many changes for 0.2 release Aug 12, 2011
requirements.txt MAINT: use external 'uncertainties' package. Apr 29, 2018
setup.cfg update to versioneer 0.18 Jan 16, 2017 MAINT: remove fork of uncertainties package. Apr 29, 2018 update to versioneer 0.18 Jan 16, 2017




LMfit-py provides a Least-Squares Minimization routine and class with a simple, flexible approach to parameterizing a model for fitting to data.

LMfit is a pure Python package, and so easy to install from source or with pip install lmfit.

For questions, comments, and suggestions, please use the LMfit mailing list, Using the bug tracking software in GitHub Issues is encouraged for known problems and bug reports. Please read before creating an Issue.

Parameters and Fitting

LMfit-py provides a Least-Squares Minimization routine and class with a simple, flexible approach to parameterizing a model for fitting to data. Named Parameters can be held fixed or freely adjusted in the fit, or held between lower and upper bounds. In addition, parameters can be constrained as a simple mathematical expression of other Parameters.

To do this, the programmer defines a Parameters object, an enhanced dictionary, containing named parameters:

fit_params = Parameters()
fit_params['amp'] = Parameter(value=1.2, min=0.1, max=1000)
fit_params['cen'] = Parameter(value=40.0, vary=False)
fit_params['wid'] = Parameter(value=4, min=0)

or using the equivalent:

fit_params = Parameters()
fit_params.add('amp', value=1.2, min=0.1, max=1000)
fit_params.add('cen', value=40.0, vary=False)
fit_params.add('wid', value=4, min=0)

The programmer will also write a 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 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 consumer in real-time data analysis.

To perform the fit, the user calls:

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

After the fit, a MinimizerResult class is returned that holds the results 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-Marquart algorithm with numerically-calculated derivatives from MINPACK's lmdif function, as used by scipy.optimize.leastsq. Other solvers (Nelder-Mead, etc) are also available, though slightly less well-tested and supported.