class_docstr: A general minimizer for curve fitting and optimization.
class_docstr_tok: ["A","general","minimizer","for","curve","fitting","and","optimization","."]
docstr: Use Levenberg-Marquardt minimization to perform a fit.
  
  It assumes that the input Parameters have been initialized, and a
  function to minimize has been properly set up. When possible, this
  calculates the estimated uncertainties and variable correlations
  from the covariance matrix.
  
  This method calls :scipydoc:`optimize.leastsq` and, by default,
  numerical derivatives are used.
  
  Parameters
  ----------
  params : Parameters, optional
      Parameters to use as starting point.
  max_nfev : int or None, optional
      Maximum number of function evaluations. Defaults to
      ``2000*(nvars+1)``, where ``nvars`` is the number of variable
      parameters.
  **kws : dict, optional
      Minimizer options to pass to :scipydoc:`optimize.leastsq`.
  
  Returns
  -------
  MinimizerResult
      Object containing the optimized parameters and several
      goodness-of-fit statistics.
  
  
  .. versionchanged:: 0.9.0
     Return value changed to :class:`MinimizerResult`.
docstr_tok: ["Use","Levenberg-Marquardt","minimization","to","perform","a","fit",".","It","assumes","that","the","input","Parameters","have","been","initialized",",","and","a","function","to","minimize","has","been","properly","set","up",".","When","possible",",","this","calculates","the","estimated","uncertainties","and","variable","correlations","from","the","covariance","matrix",".","This","method","calls",":","scipydoc",":","`","optimize.leastsq","`","and",",","by","default",",","numerical","derivatives","are","used",".","Parameters","--","--","--","--","--","params",":","Parameters",",","optional","Parameters","to","use","as","starting","point",".","max_nfev",":","int","or","None",",","optional","Maximum","number","of","function","evaluations",".","Defaults","to","``","2000","*","(","nvars+1",")","``",",","where","``","nvars","``","is","the","number","of","variable","parameters",".","*","*","kws",":","dict",",","optional","Minimizer","options","to","pass","to",":","scipydoc",":","`","optimize.leastsq","`",".","Returns","--","--","--","-","MinimizerResult","Object","containing","the","optimized","parameters","and","several","goodness-of-fit","statistics",".","..","versionchanged",":",":","0.9.0","Return","value","changed","to",":","class",":","`","MinimizerResult","`","."]
code: def leastsq(self, params=None, max_nfev=None, **kws):
      """Use Levenberg-Marquardt minimization to perform a fit.
  
      It assumes that the input Parameters have been initialized, and a
      function to minimize has been properly set up. When possible, this
      calculates the estimated uncertainties and variable correlations
      from the covariance matrix.
  
      This method calls :scipydoc:`optimize.leastsq` and, by default,
      numerical derivatives are used.
  
      Parameters
      ----------
      params : Parameters, optional
          Parameters to use as starting point.
      max_nfev : int or None, optional
          Maximum number of function evaluations. Defaults to
          ``2000*(nvars+1)``, where ``nvars`` is the number of variable
          parameters.
      **kws : dict, optional
          Minimizer options to pass to :scipydoc:`optimize.leastsq`.
  
      Returns
      -------
      MinimizerResult
          Object containing the optimized parameters and several
          goodness-of-fit statistics.
  
  
      .. versionchanged:: 0.9.0
         Return value changed to :class:`MinimizerResult`.
  
      """
      result = self.prepare_fit(params=params)
      result.method = "leastsq"
      result.nfev -= 2  # correct for "pre-fit" initialization/checks
      variables = result._init_vals_internal
  
      # Note: we set max number of function evaluations here, and send twice
      # that value to the solver so it essentially never stops on its own
      self.set_max_nfev(max_nfev, 2000 * (result.nvarys + 1))
  
      lskws = dict(
          Dfun=None,
          full_output=1,
          col_deriv=0,
          ftol=1.5e-8,
          xtol=1.5e-8,
          gtol=0.0,
          maxfev=2 * self.max_nfev,
          epsfcn=1.0e-10,
          factor=100,
          diag=None,
      )
  
      if "maxfev" in kws:
          warnings.warn(
              maxeval_warning.format("maxfev", thisfuncname()),
              RuntimeWarning,
          )
          kws.pop("maxfev")
  
      lskws.update(self.kws)
      lskws.update(kws)
      self.col_deriv = False
  
      if lskws["Dfun"] is not None:
          self.jacfcn = lskws["Dfun"]
          self.col_deriv = lskws["col_deriv"]
          lskws["Dfun"] = self.__jacobian
  
      # suppress runtime warnings during fit and error analysis
      orig_warn_settings = np.geterr()
      np.seterr(all="ignore")
      result.call_kws = lskws
      try:
          lsout = scipy_leastsq(self.__residual, variables, **lskws)
      except AbortFitException:
          pass
  
      if not result.aborted:
          _best, _cov, _infodict, errmsg, ier = lsout
      else:
          _best = result.last_internal_values
          _cov = None
          ier = -1
          errmsg = "Fit aborted."
  
      result.nfev -= 1
      if result.nfev >= self.max_nfev:
          result.nfev = self.max_nfev - 1
      self.result.nfev = result.nfev
      try:
          result.residual = self.__residual(_best)
          result._calculate_statistics()
      except AbortFitException:
          pass
  
      result.ier = ier
      result.lmdif_message = errmsg
      result.success = ier in [1, 2, 3, 4]
      if ier in {1, 2, 3}:
          result.message = "Fit succeeded."
      elif ier == 0:
          result.message = (
              "Invalid Input Parameters. I.e. more variables "
              "than data points given, tolerance < 0.0, or "
              "no data provided."
          )
      elif ier == 4:
          result.message = (
              "One or more variable did not affect the fit."
          )
      elif ier == 5:
          result.message = self._err_max_evals.format(lskws["maxfev"])
      else:
          result.message = "Tolerance seems to be too small."
  
      # self.errorbars = error bars were successfully estimated
      result.errorbars = _cov is not None
      if result.errorbars:
          # transform the covariance matrix to "external" parameter space
          result.covar = self._int2ext_cov_x(_cov, _best)
          # calculate parameter uncertainties and correlations
          self._calculate_uncertainties_correlations()
      else:
          result.message = (
              f"{result.message} Could not estimate error-bars."
          )
  
      np.seterr(**orig_warn_settings)
  
      return result
  
imports: from collections import namedtuple
  from copy import deepcopy
  import inspect
  import multiprocessing
  import numbers
  import warnings
  import numpy
  from scipy.linalg import LinAlgError, inv
  from scipy.optimize import basinhopping
  from scipy.optimize import brute
  from scipy.optimize import differential_evolution
  from scipy.optimize import dual_annealing
  from scipy.optimize import least_squares
  from scipy.optimize import leastsq
  from scipy.optimize import minimize
  from scipy.optimize import shgo
  from scipy.sparse import issparse
  from scipy.sparse.linalg import LinearOperator
  from scipy.stats import cauchy
  from scipy.stats import norm
  from ._ampgo import ampgo
  from .parameter import Parameter, Parameters
  from .printfuncs import fitreport_html_table
label_desc: Use image node_id 16 for calling the Minimizer obj's underlying member method code with example usage: obj.leastsq(params, max_nfev) and returns: result