Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Numerical algorithms tend to work better when not fed extremely small (or large) numbers.</p> <p>In this case, the graph shows your data has extremely small x and y values. If you scale them, the fit is remarkable better:</p> <pre><code>xData = np.load('xData.npy')*10**5 yData = np.load('yData.npy')*10**5 </code></pre> <hr> <pre><code>from __future__ import division import os os.chdir(os.path.expanduser('~/tmp')) import numpy as np import scipy.optimize as optimize import matplotlib.pyplot as plt def func(x,a,b,c): return a*np.exp(-b*x)-c xData = np.load('xData.npy')*10**5 yData = np.load('yData.npy')*10**5 print(xData.min(), xData.max()) print(yData.min(), yData.max()) trialX = np.linspace(xData[0], xData[-1], 1000) # Fit a polynomial fitted = np.polyfit(xData, yData, 10)[::-1] y = np.zeros(len(trialX)) for i in range(len(fitted)): y += fitted[i]*trialX**i # Fit an exponential popt, pcov = optimize.curve_fit(func, xData, yData) print(popt) yEXP = func(trialX, *popt) plt.figure() plt.plot(xData, yData, label='Data', marker='o') plt.plot(trialX, yEXP, 'r-',ls='--', label="Exp Fit") plt.plot(trialX, y, label = '10 Deg Poly') plt.legend() plt.show() </code></pre> <p><img src="https://i.stack.imgur.com/C7Zxy.png" alt="enter image description here"></p> <p>Note that after rescaling <code>xData</code> and <code>yData</code>, the parameters returned by <code>curve_fit</code> must also be rescaled. In this case, <code>a</code>, <code>b</code> and <code>c</code> each must be divided by 10**5 to obtain fitted parameters for the original data.</p> <hr> <p>One objection you might have to the above is that the scaling has to be chosen rather "carefully". (Read: Not every reasonable choice of scale works!)</p> <p>You can improve the robustness of <code>curve_fit</code> by providing a reasonable initial guess for the parameters. Usually you have some <em>a priori</em> knowledge about the data which can motivate ballpark / back-of-the envelope type guesses for reasonable parameter values.</p> <p>For example, calling <code>curve_fit</code> with</p> <pre><code>guess = (-1, 0.1, 0) popt, pcov = optimize.curve_fit(func, xData, yData, guess) </code></pre> <p>helps improve the range of scales on which <code>curve_fit</code> succeeds in this case.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload