Python中的指数曲线拟合

时间:2018-06-04 15:15:31

标签: python-3.x scipy curve-fitting

Hello Guys我的曲线拟合有问题,我得到了奇怪的结果, 下面是我的代码:

x=np.array([     0.   ,    117.999,    239.843,    307.682,    478.998,
          599.465,    719.569,    839.406,    959.895,   1079.811,
         1199.307,   1319.546,   1439.448,   1560.056,   1679.475,
         1799.219,   1919.637,   2039.599,   2160.254,   2279.731,
         2399.728,   2519.749,   2639.661,   2759.891,   2879.454,
         2999.56 ,   3119.91 ,   3239.72 ,   3359.448,   3479.005,
         3599.566,   3719.498,   3839.534,   3959.571,   4079.377,
         4199.786,   4319.232,   4438.888,   4560.006,   4679.155,
         4799.745,   4919.229,   5039.53 ,   5159.228,   5279.553,
         5400.278,   5518.791,   5638.914,   5759.079,   5880.445,
         5999.498,   6119.269,   6239.705,   6359.813,   6480.192,
         6600.37 ,   6719.434,   6839.191,   6959.195,   7079.549,
         7198.495,   7318.533,   7438.822,   7559.135,   7678.648,
         7798.731,   7918.261,   8038.651,   8158.605,   8279.093,
         8398.671,   8519.004,   8638.563,   8759.005,   8878.764,
         8998.315,   9118.957,   9239.002,   9358.446,   9478.628,
         9598.738,   9719.122,   9839.224,   9958.617,  10078.85 ,
        10199.199,  10319.528,  10438.573,  10559.071,  10679.363])

y=np.array([ 121.32,  129.31,  135.11,  139.71,  147.66,  156.09,  163.03,
    170.  ,  177.08,  184.77,  191.38,  198.73,  204.51,  211.83,
    219.51,  225.53,  232.54,  238.21,  245.94,  252.82,  259.15,
    266.75,  274.07,  280.93,  287.73,  294.88,  302.89,  309.8 ,
    316.32,  322.87,  331.42,  336.98,  344.63,  348.29,  354.48,
    360.99,  368.03,  372.79,  376.91,  384.85,  388.97,  394.49,
    396.82,  401.43,  408.19,  407.6 ,  415.95,  416.8 ,  416.2 ,
    424.01,  426.7 ,  428.67,  431.59,  434.18,  437.4 ,  441.59,
    437.17,  441.6 ,  445.85,  446.06,  449.68,  449.19,  449.63,
    451.75,  451.05,  453.37,  452.8 ,  457.66,  459.33,  460.5 ,
    458.22,  461.3 ,  461.22,  462.81,  461.62,  462.99,  457.83,
    462.3 ,  464.88,  466.13,  464.85,  468.6 ,  467.93,  467.19,
    468.06,  469.46,  469.82,  471.9 ,  469.01,  469.06])

enter image description here

我正在使用scipy.optimize.curve_fit进行指数拟合。

def func(x, a, b, c):
        return a*np.exp(-b*x)+c


a0 = y.ptp()
b0 = -1/x.ptp()
c0 = y.min()
popt, pcov = curve_fit(func, x, y, p0=(a0, b0, c0), maxfev = 2000)
plt.figure()
plt.plot(x, y, 'ko', label="Original Noised Data")
plt.plot(x, func(x, *popt), 'r-', label="Fitted Curve")
plt.legend()
plt.show()

enter image description here

拟合曲线看起来不太好

编辑:popt和pcov值 edit2:我修改了值,看起来我的数据是指数

popt
array([  2.23557884e+06,  -1.48238130e-08,  -2.23539443e+06])

pcov
array([[  5.35480790e+16,   3.55032838e+02,  -5.35480791e+16],
       [  3.55032838e+02,   2.35392818e-12,  -3.55032839e+02],
       [ -5.35480791e+16,  -3.55032839e+02,   5.35480792e+16]])

1 个答案:

答案 0 :(得分:1)

You are trying to fit your data with an exponential decay, which is clearly not the case. Use a bounded exponential instead, and provide better initial guess:

def func(x, a, b, c):
        return a*(1-np.exp(-b*x))+c

popt, pcov = curve_fit(func,x, y,p0=(600,0.1,1))