Non-Linear Least-Squares Minimization and Curve-Fitting for Python¶
Lmfit provides a high-level interface to non-linear optimization and curve fitting problems for Python. It builds on and extends many of the optimization methods of scipy.optimize. Initially inspired by (and named for) extending the Levenberg-Marquardt method from scipy.optimize.leastsq, lmfit now provides a number of useful enhancements to optimization and data fitting problems, including:
Parameterobjects instead of plain floats as variables. A
Parameterhas a value that can be varied during the fit or kept at a fixed value. It can have upper and/or lower bounds. A Parameter can even have a value that is constrained by an algebraic expression of other Parameter values. As a Python object, a Parameter can also have attributes such as a standard error, after a fit that can estimate uncertainties.
Ease of changing fitting algorithms. Once a fitting model is set up, one can change the fitting algorithm used to find the optimal solution without changing the objective function.
Improved estimation of confidence intervals. While scipy.optimize.leastsq will automatically calculate uncertainties and correlations from the covariance matrix, the accuracy of these estimates is sometimes questionable. To help address this, lmfit has functions to explicitly explore parameter space and determine confidence levels even for the most difficult cases. Additionally, lmfit will use the
numdifftoolspackage (if installed) to estimate parameter uncertainties and correlations for algorithms that do not natively support this in SciPy.
Improved curve-fitting with the
Modelclass. This extends the capabilities of scipy.optimize.curve_fit, allowing you to turn a function that models your data into a Python class that helps you parametrize and fit data with that model.
Many built-in models for common lineshapes are included and ready to use.
The lmfit package is Free software, using an Open Source license. The software and this document are works in progress. If you are interested in participating in this effort please use the lmfit GitHub repository.
- Getting started with Non-Linear Least-Squares Fitting
- Downloading and Installation
- Getting Help
- Frequently Asked Questions
- What’s the best way to ask for help or submit a bug report?
- Why did my script break when upgrading from lmfit 0.8.3 to 0.9.0?
- I get import errors from IPython
- How can I fit multi-dimensional data?
- How can I fit multiple data sets?
- How can I fit complex data?
- How should I cite LMFIT?
- I get errors from NaN in my fit. What can I do?
- Why are Parameter values sometimes stuck at initial values?
- Why are uncertainties in Parameters sometimes not determined?
- Can Parameters be used for Array Indices or Discrete Values?
- Performing Fits and Analyzing Outputs
- Writing a Fitting Function
- Choosing Different Fitting Methods
MinimizerResult– the optimization result
- Getting and Printing Fit Reports
- Using a Iteration Callback Function
- Using the
Minimizer.emcee()- calculating the posterior probability distribution of parameters
- Modeling Data and Curve Fitting
- Built-in Fitting Models in the
- Peak-like models
- Linear and Polynomial Models
- Periodic Models
- Step-like models
- Exponential and Power law models
- Two dimensional Peak-like models
- User-defined Models
- Example 1: Fit Peak data to Gaussian, Lorentzian, and Voigt profiles
- Example 2: Fit data to a Composite Model with pre-defined models
- Example 3: Fitting Multiple Peaks – and using Prefixes
- Example 4: Using a Spline Model
- Calculation of confidence intervals
- Bounds Implementation
- Using Mathematical Constraints
- Release Notes
- Version 1.2.1 Release Notes (May 02, 2023)
- Version 1.2.0 Release Notes (April 05, 2023)
- Version 1.1.0 Release Notes (November 27, 2022)
- Version 1.0.3 Release Notes (October 14, 2021)
- Version 1.0.2 Release Notes (February 7, 2021)
- Version 1.0.1 Release Notes
- Version 1.0.0 Release Notes
- Version 0.9.15 Release Notes
- Version 0.9.14 Release Notes
- Version 0.9.13 Release Notes
- Version 0.9.12 Release Notes
- Version 0.9.10 Release Notes
- Version 0.9.9 Release Notes
- Version 0.9.6 Release Notes
- Version 0.9.5 Release Notes
- Version 0.9.4 Release Notes
- Version 0.9.3 Release Notes
- Version 0.9.0 Release Notes
- Examples gallery
- Examples from the documentation