<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>scipy.optimize.leastsq — SciPy v1.1.0.dev0+4e64658 Reference Guide</title> <link rel="stylesheet" type="text/css" href="../_static/css/spc-bootstrap.css"> <link rel="stylesheet" type="text/css" href="../_static/css/spc-extend.css"> <link rel="stylesheet" href="../_static/scipy.css" type="text/css" > <link rel="stylesheet" href="../_static/pygments.css" type="text/css" > <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.1.0.dev0+4e64658', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: false }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/scipy-mathjax/MathJax.js?config=scipy-mathjax"></script> <script type="text/javascript" src="../_static/js/copybutton.js"></script> <link rel="index" title="Index" href="../genindex.html" > <link rel="search" title="Search" href="../search.html" > <link rel="top" title="SciPy v1.1.0.dev0+4e64658 Reference Guide" href="../index.html" > <link rel="up" title="Optimization and root finding (scipy.optimize)" href="../optimize.html" > <link rel="next" title="scipy.optimize.least_squares" href="scipy.optimize.least_squares.html" > <link rel="prev" title="scipy.optimize.golden" href="scipy.optimize.golden.html" > </head> <body> <div class="container"> <div class="header"> </div> </div> <div class="container"> <div class="main"> <div class="row-fluid"> <div class="span12"> <div class="spc-navbar"> <ul class="nav nav-pills pull-left"> <li class="active"><a href="../index.html">SciPy v1.1.0.dev0+4e64658 Reference Guide</a></li> <li class="active"><a href="../optimize.html" accesskey="U">Optimization and root finding (<code class="docutils literal"><span class="pre">scipy.optimize</span></code>)</a></li> </ul> <ul class="nav nav-pills pull-right"> <li class="active"> <a href="../genindex.html" title="General Index" accesskey="I">index</a> </li> <li class="active"> <a href="../py-modindex.html" title="Python Module Index" >modules</a> </li> <li class="active"> <a href="scipy.optimize.least_squares.html" title="scipy.optimize.least_squares" accesskey="N">next</a> </li> <li class="active"> <a href="scipy.optimize.golden.html" title="scipy.optimize.golden" accesskey="P">previous</a> </li> </ul> </div> </div> </div> <div class="row-fluid"> <div class="spc-rightsidebar span3"> <div class="sphinxsidebarwrapper"> <p class="logo"><a href="../index.html"> <img class="logo" src="../_static/scipyshiny_small.png" alt="Logo"> </a></p> <h4>Previous topic</h4> <p class="topless"><a href="scipy.optimize.golden.html" title="previous chapter">scipy.optimize.golden</a></p> <h4>Next topic</h4> <p class="topless"><a href="scipy.optimize.least_squares.html" title="next chapter">scipy.optimize.least_squares</a></p> </div> </div> <div class="span9"> <div class="bodywrapper"> <div class="body" id="spc-section-body"> <div class="section" id="scipy-optimize-leastsq"> <h1>scipy.optimize.leastsq<a class="headerlink" href="#scipy-optimize-leastsq" title="Permalink to this headline">¶</a></h1> <dl class="function"> <dt id="scipy.optimize.leastsq"> <code class="descclassname">scipy.optimize.</code><code class="descname">leastsq</code><span class="sig-paren">(</span><em>func</em>, <em>x0</em>, <em>args=()</em>, <em>Dfun=None</em>, <em>full_output=0</em>, <em>col_deriv=0</em>, <em>ftol=1.49012e-08</em>, <em>xtol=1.49012e-08</em>, <em>gtol=0.0</em>, <em>maxfev=0</em>, <em>epsfcn=None</em>, <em>factor=100</em>, <em>diag=None</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/scipy/scipy/blob/4e64658/scipy/optimize/minpack.py#L261-L448"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scipy.optimize.leastsq" title="Permalink to this definition">¶</a></dt> <dd><p>Minimize the sum of squares of a set of equations.</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">arg</span> <span class="nb">min</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">func</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">))</span> <span class="n">y</span> </pre></div> </div> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><p class="first"><strong>func</strong> : callable</p> <blockquote> <div><p>should take at least one (possibly length N vector) argument and returns M floating point numbers. It must not return NaNs or fitting might fail.</p> </div></blockquote> <p><strong>x0</strong> : ndarray</p> <blockquote> <div><p>The starting estimate for the minimization.</p> </div></blockquote> <p><strong>args</strong> : tuple, optional</p> <blockquote> <div><p>Any extra arguments to func are placed in this tuple.</p> </div></blockquote> <p><strong>Dfun</strong> : callable, optional</p> <blockquote> <div><p>A function or method to compute the Jacobian of func with derivatives across the rows. If this is None, the Jacobian will be estimated.</p> </div></blockquote> <p><strong>full_output</strong> : bool, optional</p> <blockquote> <div><p>non-zero to return all optional outputs.</p> </div></blockquote> <p><strong>col_deriv</strong> : bool, optional</p> <blockquote> <div><p>non-zero to specify that the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation).</p> </div></blockquote> <p><strong>ftol</strong> : float, optional</p> <blockquote> <div><p>Relative error desired in the sum of squares.</p> </div></blockquote> <p><strong>xtol</strong> : float, optional</p> <blockquote> <div><p>Relative error desired in the approximate solution.</p> </div></blockquote> <p><strong>gtol</strong> : float, optional</p> <blockquote> <div><p>Orthogonality desired between the function vector and the columns of the Jacobian.</p> </div></blockquote> <p><strong>maxfev</strong> : int, optional</p> <blockquote> <div><p>The maximum number of calls to the function. If <em class="xref py py-obj">Dfun</em> is provided then the default <em class="xref py py-obj">maxfev</em> is 100*(N+1) where N is the number of elements in x0, otherwise the default <em class="xref py py-obj">maxfev</em> is 200*(N+1).</p> </div></blockquote> <p><strong>epsfcn</strong> : float, optional</p> <blockquote> <div><p>A variable used in determining a suitable step length for the forward- difference approximation of the Jacobian (for Dfun=None). Normally the actual step length will be sqrt(epsfcn)*x If epsfcn is less than the machine precision, it is assumed that the relative errors are of the order of the machine precision.</p> </div></blockquote> <p><strong>factor</strong> : float, optional</p> <blockquote> <div><p>A parameter determining the initial step bound (<code class="docutils literal"><span class="pre">factor</span> <span class="pre">*</span> <span class="pre">||</span> <span class="pre">diag</span> <span class="pre">*</span> <span class="pre">x||</span></code>). Should be in interval <code class="docutils literal"><span class="pre">(0.1,</span> <span class="pre">100)</span></code>.</p> </div></blockquote> <p><strong>diag</strong> : sequence, optional</p> <blockquote> <div><p>N positive entries that serve as a scale factors for the variables.</p> </div></blockquote> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first"><strong>x</strong> : ndarray</p> <blockquote> <div><p>The solution (or the result of the last iteration for an unsuccessful call).</p> </div></blockquote> <p><strong>cov_x</strong> : ndarray</p> <blockquote> <div><p>Uses the fjac and ipvt optional outputs to construct an estimate of the jacobian around the solution. None if a singular matrix encountered (indicates very flat curvature in some direction). This matrix must be multiplied by the residual variance to get the covariance of the parameter estimates – see curve_fit.</p> </div></blockquote> <p><strong>infodict</strong> : dict</p> <blockquote> <div><p>a dictionary of optional outputs with the key s:</p> <dl class="docutils"> <dt><code class="docutils literal"><span class="pre">nfev</span></code></dt> <dd><p class="first last">The number of function calls</p> </dd> <dt><code class="docutils literal"><span class="pre">fvec</span></code></dt> <dd><p class="first last">The function evaluated at the output</p> </dd> <dt><code class="docutils literal"><span class="pre">fjac</span></code></dt> <dd><p class="first last">A permutation of the R matrix of a QR factorization of the final approximate Jacobian matrix, stored column wise. Together with ipvt, the covariance of the estimate can be approximated.</p> </dd> <dt><code class="docutils literal"><span class="pre">ipvt</span></code></dt> <dd><p class="first last">An integer array of length N which defines a permutation matrix, p, such that fjac*p = q*r, where r is upper triangular with diagonal elements of nonincreasing magnitude. Column j of p is column ipvt(j) of the identity matrix.</p> </dd> <dt><code class="docutils literal"><span class="pre">qtf</span></code></dt> <dd><p class="first last">The vector (transpose(q) * fvec).</p> </dd> </dl> </div></blockquote> <p><strong>mesg</strong> : str</p> <blockquote> <div><p>A string message giving information about the cause of failure.</p> </div></blockquote> <p><strong>ier</strong> : int</p> <blockquote class="last"> <div><p>An integer flag. If it is equal to 1, 2, 3 or 4, the solution was found. Otherwise, the solution was not found. In either case, the optional output variable ‘mesg’ gives more information.</p> </div></blockquote> </td> </tr> </tbody> </table> <p class="rubric">Notes</p> <p>“leastsq” is a wrapper around MINPACK’s lmdif and lmder algorithms.</p> <p>cov_x is a Jacobian approximation to the Hessian of the least squares objective function. This approximation assumes that the objective function is based on the difference between some observed target data (ydata) and a (non-linear) function of the parameters <em class="xref py py-obj">f(xdata, params)</em></p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">func</span><span class="p">(</span><span class="n">params</span><span class="p">)</span> <span class="o">=</span> <span class="n">ydata</span> <span class="o">-</span> <span class="n">f</span><span class="p">(</span><span class="n">xdata</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span> </pre></div> </div> <p>so that the objective function is</p> <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="nb">min</span> <span class="nb">sum</span><span class="p">((</span><span class="n">ydata</span> <span class="o">-</span> <span class="n">f</span><span class="p">(</span><span class="n">xdata</span><span class="p">,</span> <span class="n">params</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="n">params</span> </pre></div> </div> </dd></dl> </div> </div> </div> </div> </div> </div> </div> <div class="container container-navbar-bottom"> <div class="spc-navbar"> </div> </div> <div class="container"> <div class="footer"> <div class="row-fluid"> <ul class="inline pull-left"> <li> © Copyright 2008-2016, The Scipy community. </li> <li> Last updated on Sep 21, 2017. </li> <li> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.6.3. </li> </ul> </div> </div> </div> </body> </html>