class_docstr: A general minimizer for curve fitting and optimization.
class_docstr_tok: ["A","general","minimizer","for","curve","fitting","and","optimization","."]
docstr: Least-squares minimization using :scipydoc:`optimize.least_squares`.
  
  This method wraps :scipydoc:`optimize.least_squares`, which has
  built-in support for bounds and robust loss functions. By default
  it uses the Trust Region Reflective algorithm with a linear loss
  function (i.e., the standard least-squares problem).
  
  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.least_squares`.
  
  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: ["Least-squares","minimization","using",":","scipydoc",":","`","optimize.least_squares","`",".","This","method","wraps",":","scipydoc",":","`","optimize.least_squares","`",",","which","has","built-in","support","for","bounds","and","robust","loss","functions",".","By","default","it","uses","the","Trust","Region","Reflective","algorithm","with","a","linear","loss","function","(","i.e.",",","the","standard","least-squares","problem",")",".","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.least_squares","`",".","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 least_squares(self, params=None, max_nfev=None, **kws):
      """Least-squares minimization using :scipydoc:`optimize.least_squares`.
  
      This method wraps :scipydoc:`optimize.least_squares`, which has
      built-in support for bounds and robust loss functions. By default
      it uses the Trust Region Reflective algorithm with a linear loss
      function (i.e., the standard least-squares problem).
  
      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.least_squares`.
  
      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)
      result.method = "least_squares"
  
      replace_none = lambda x, sign: sign * np.inf if x is None else x
      self.set_max_nfev(max_nfev, 2000 * (result.nvarys + 1))
  
      start_vals, lower_bounds, upper_bounds = [], [], []
      for vname in result.var_names:
          par = self.params[vname]
          start_vals.append(par.value)
          lower_bounds.append(replace_none(par.min, -1))
          upper_bounds.append(replace_none(par.max, 1))
  
      least_squares_kws = dict(
          jac="2-point",
          method="trf",
          ftol=1e-08,
          xtol=1e-08,
          gtol=1e-08,
          x_scale=1.0,
          loss="linear",
          f_scale=1.0,
          diff_step=None,
          tr_solver=None,
          tr_options={},
          jac_sparsity=None,
          max_nfev=2 * self.max_nfev,
          verbose=0,
          kwargs={},
      )
  
      least_squares_kws.update(self.kws)
      least_squares_kws.update(kws)
  
      least_squares_kws["kwargs"].update(
          {"apply_bounds_transformation": False}
      )
      result.call_kws = least_squares_kws
  
      try:
          ret = least_squares(
              self.__residual,
              start_vals,
              bounds=(lower_bounds, upper_bounds),
              **least_squares_kws,
          )
          result.residual = ret.fun
      except AbortFitException:
          ret = None
          result.aborted = True
  
      # Note: scipy.optimize.least_squares is actually returning the
      # "last evaluation", which is not necessarily the "best result"; so we
      # do that here for consistency
      if not result.aborted:
          result.nfev -= 1
          result.residual = self.__residual(ret.x, False)
      elif result.nfev > self.max_nfev - 5:
          result.nfev -= 2
          _best = result.last_internal_values
          result.residual = self.__residual(_best, False)
      result._calculate_statistics()
  
      if not result.aborted:
          for attr in ret:
              outattr = attr
              if attr == "nfev":
                  outattr = "least_squares_nfev"
              setattr(result, outattr, ret[attr])
  
          result.x = np.atleast_1d(result.x)
  
          # calculate the cov_x and estimate uncertainties/correlations
          try:
              if issparse(ret.jac):
                  hess = (ret.jac.T * ret.jac).toarray()
              elif isinstance(ret.jac, LinearOperator):
                  identity = np.eye(
                      ret.jac.shape[1], dtype=ret.jac.dtype
                  )
                  hess = (ret.jac.T * ret.jac) * identity
              else:
                  hess = np.matmul(ret.jac.T, ret.jac)
              result.covar = np.linalg.inv(hess)
              self._calculate_uncertainties_correlations()
          except LinAlgError:
              pass
  
      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 15 for calling the Minimizer obj's underlying member method code with example usage: obj.least_squares(params, max_nfev) and returns: result