{"id":4979,"library":"lmfit","title":"LMFit","description":"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.","status":"active","version":"1.3.4","language":"en","source_language":"en","source_url":"https://github.com/lmfit/lmfit-py","tags":["minimization","curve-fitting","least-squares","optimization","parameters","model-fitting","scientific-computing"],"install":[{"cmd":"pip install lmfit","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core numerical operations and data structures.","package":"numpy","optional":false},{"reason":"Underlying optimization algorithms.","package":"scipy","optional":false},{"reason":"Expression evaluation for parameter constraints.","package":"asteval","optional":false},{"reason":"Improved estimation of parameter uncertainties and correlations for non-Levenberg-Marquardt solvers.","package":"numdifftools","optional":true},{"reason":"For handling transparent calculations with uncertainties and `ModelResult.uvars` output.","package":"uncertainties","optional":true},{"reason":"For pickling complex objects, including models and results.","package":"dill","optional":true}],"imports":[{"symbol":"Model","correct":"from lmfit import Model"},{"symbol":"Parameters","correct":"from lmfit import Parameters"},{"symbol":"minimize","correct":"from lmfit import minimize"}],"quickstart":{"code":"import numpy as np\nfrom lmfit import Model\n\n# 1. Generate some data\nx = np.linspace(0, 10, 100)\ny_true = 3.0 * np.exp(-0.5 * x) + 2.0\nnp.random.seed(0)\ny_data = y_true + np.random.normal(0, 0.2, x.shape)\n\n# 2. Define your model function\ndef exponential_decay(x, amplitude, decay, offset):\n    return amplitude * np.exp(-decay * x) + offset\n\n# 3. Create a Model instance from your function\nexp_model = Model(exponential_decay)\n\n# 4. Create initial parameters with guess() or manually\n# guess() method often requires x and y data for good initial estimates\nparams = exp_model.make_params(amplitude=5., decay=0.1, offset=1.)\n\n# Or, for more refined control:\n# params = exp_model.guess(y_data, x=x)\n\n# Optionally set bounds or fix parameters\nparams['amplitude'].set(min=0.0)\nparams['decay'].set(min=0.0)\n\n# 5. Fit the model to the data\nresult = exp_model.fit(y_data, params, x=x)\n\n# 6. Print the fitting report\nprint(result.fit_report())\n\n# You can also access best-fit parameters, statistics, etc.\n# print(f\"Best-fit amplitude: {result.params['amplitude'].value:.3f}\")\n# print(f\"Reduced Chi-square: {result.redchi:.3f}\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure `x` (independent variable data) is always passed to `Model.guess(data, x=x_data)`.","message":"Since version 1.0.3, the `guess()` method of `lmfit.Model` (and built-in models) now explicitly requires the `x` (independent variable) argument, even if it worked without it in older versions. Scripts not providing `x` will raise an error.","severity":"breaking","affected_versions":">=1.0.3"},{"fix":"Use the `nan_policy='omit'` argument when creating a `Model` or calling `Model.fit()` / `lmfit.minimize()` if `NaN`s in data should be ignored. Alternatively, preprocess data to remove or impute `NaN`s.","message":"Fit procedures will stop if `NaN` values are encountered in the objective function or model output. If `NaN`s are present in your input data and are meant to represent missing values, they must be explicitly handled.","severity":"gotcha","affected_versions":"All"},{"fix":"Provide reasonable initial guesses for parameters. For models with discrete-like transitions, consider using smoother functions (e.g., error functions for steps) or a 'brute' force method for initial scanning. Review the `fit_report` to check for parameters with zero standard error or very small changes.","message":"Parameters can sometimes get 'stuck' at their initial values or fail to converge if small changes to their values do not significantly affect the residual (e.g., discrete steps in the model, or initial guesses are extremely far from the true minimum).","severity":"gotcha","affected_versions":"All"},{"fix":"Upgrade your Python environment to 3.9 or newer to use current `lmfit` versions.","message":"Older Python versions are no longer supported. For instance, `lmfit` version 1.2.0 dropped support for Python 3.6, and version 1.3.3 dropped support for Python 3.8. The current minimum required Python version is 3.9.","severity":"deprecated","affected_versions":"<1.2.0 (Python 3.6), <1.3.3 (Python 3.8)"},{"fix":"Always pass a `copy.deepcopy()` of the `Parameters` object to `minimize()` if you intend to reuse the original `Parameters` for subsequent fits or if a `Minimizer` instance is reused across multiple fits without re-initialization.","message":"When using `lmfit.minimize` directly (rather than `Model.fit`), the `Parameters` object passed as an argument can be modified in-place by the minimization routine. Reusing the same `Parameters` object for multiple fits without explicit copying can lead to unexpected results.","severity":"gotcha","affected_versions":"<=0.9.x (potentially older, though modern docs imply better handling)"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}