使用scipy.optimize.curvefit进行非线性拟合

时间:2017-04-14 10:33:30

标签: python scipy curve-fitting

我正在尝试使用指数模型拟合数据:

def f(x, F_0, t0, tau):
    return F_0 * np.exp (-(x-t0)/tau)

我像这样使用scipy.optimize.curve_fit

popt,pcov = curve_fit(f,t,y,maxfev=1000)

理论上应该是popt应该给出F_0t0tau的值吗?那么它给了我[1,1,1]pcov=inf。发生了什么事?

我正在运行的数据是:

t = array([ 6.0989933 ,  6.13041162,  6.16183043,  6.19324923,  6.22466803, 6.25608683,  6.28750563,  6.31892443,  6.35034323,  6.38176203, 6.41318083,  6.44459963,  6.47601843,  6.50743675,  6.53885555, 6.57027435,  6.60169315,  6.63311195,  6.66453075,  6.69594955, 6.72736835,  6.75878716,  6.79020596,  6.82162476,  6.85304356, 6.88446236,  6.91588116,  6.94729996,  6.97871828,  7.01013708, 7.04155588,  7.07297468,  7.10439348,  7.13581228,  7.16723108, 7.19864988,  7.23006868,  7.26148748,  7.29290581,  7.32432461, 7.35574341,  7.38716221,  7.41858101,  7.44999981], dtype=np.float32)
y = array([  2.70034180e+03,   2.31075024e+03,   1.87097400e+03, 1.37248645e+03,   1.12983032e+03,   1.10378296e+03, 8.75943909e+02,   6.74242004e+02,   6.01329895e+02, 5.63800049e+02,   4.25704498e+02,   4.04536774e+02, 2.78763947e+02,   2.24465073e+02,   1.85565216e+02, 2.08205307e+02,   1.77108429e+02,   1.38598465e+02, 1.22449684e+02,   9.47472382e+01,   8.18683701e+01, 5.92218361e+01,   5.29379654e+01,   4.54894447e+01, 3.65242805e+01,   2.95051060e+01,   2.47274208e+01, 2.26564598e+01,   1.64215832e+01,   1.41874876e+01, 1.23035212e+01,   1.25391512e+01,   8.86111355e+00, 7.04269171e+00,   6.07329178e+00,   4.73968601e+00, 4.60453558e+00,   3.97630429e+00,   3.36820292e+00, 2.52790380e+00,   2.28720117e+00,   2.09139872e+00, 1.46813798e+00,   1.18916214e+00], dtype=np.float32)

我尝试将初始值添加到接近实际值的位置

x0 = np.array([2500,6,0.15])
popt,pcov = curve_fit(f,t,y,x0,maxfev=1000)`

popt现在为我提供x0

的值

1 个答案:

答案 0 :(得分:0)

我明白了:

F_0 =  6.7311012141178690E+00
t0 =  7.0998469795908647E+00
tau =  1.6713682175557676E-01

见附图。

Degrees of freedom (error): 41
Degrees of freedom (regression): 2
Chi-squared: 72831.3559855
R-squared: 0.995880600583
R-squared adjusted: 0.99567965427
Model F-statistic: 4955.95358568
Model F-statistic p-value: 1.11022302463e-16
Model log-likelihood: -225.490964337
AIC: 10.3859529244
BIC: 10.5076022176
Root Mean Squared Error (RMSE): 40.6848631628

F_0 = 6.7311012141178690E+00
       std err: 1.09504E+14
       t-stat: 6.43238E-07
       p-stat: 9.99999E-01
       95% confidence intervals: [-2.11333E+07, 2.11333E+07]

t0 = 7.0998469795908647E+00
       std err: 6.75151E+10
       t-stat: 2.73243E-05
       p-stat: 9.99978E-01
       95% confidence intervals: [-5.24744E+05, 5.24758E+05]

tau = 1.6713682175557676E-01
       std err: 1.10988E-05
       t-stat: 5.01689E+01
       p-stat: 0.00000E+00
       95% confidence intervals: [1.60409E-01, 1.73865E-01]

Coefficient Covariance Matrix
[  6.16444921e+10  -1.53066514e+09   9.05030951e+00]
[ -1.53066514e+09   3.80072201e+07  -2.24723915e-01]
[  9.05030951e+00  -2.24723915e-01   6.24798951e-09]

plot