scipy curve_fit error: divide by zero encountered

╄→尐↘猪︶ㄣ 提交于 2019-12-04 12:13:37

Alright, two helpful tricks.

1st, replace 0 in your x with some really small number, such as 1e-8 (don't laugh, there is a core package in R actually does this, written by his name shall not be spoken and people use it all the time.) Actually I didn't get your RuntimeWarning at all. I am running scipy 0.12.0 and numpy 1.7.1. Maybe this is version dependent.

But we will get a very bad fit:

In [41]: popt, pcov
Out[41]: (array([  3.90107143e+01,  -3.08698757e+07,  -1.52971609e+02]), inf)

So, trick 2, instead of optimizing f function, we define a g function:

In [38]: def g(x, a, b, c):
   ....:     return b/a*x**c+1/a
   ....:

In [39]: curve_fit(g, x, 1/y) #Better fit
Out[39]:
(array([ 19.76748582,  -0.14499508,   0.44206688]),
 array([[ 0.29043958,  0.00899521,  0.01650935],
        [ 0.00899521,  0.00036082,  0.00070345],
        [ 0.01650935,  0.00070345,  0.00140253]]))

We can now use the resulting parameter vector as starting vector to optimize f(). As curve_fit is a nonlinear least square method, parameter optimizes g() is not necessary the parameter optimizes f(), but hopefully it will be close. The covariance matrices are very different of course.

In [78]: curve_fit(f, x, y, p0=curve_fit(g, x, 1/y)[0]) #Alternative Fit
Out[78]:
(array([ 18.0480446 ,  -0.22881647,   0.31200106]),
 array([[ 1.14928169,  0.03741604,  0.03897652],
        [ 0.03741604,  0.00128511,  0.00136315],
        [ 0.03897652,  0.00136315,  0.00145614]]))

The comparison of the results:

Now the result is pretty good.

Your x values start at 0. If for some reason the parameter c is negative during the calculation, then you will evaluate 0 raised to a negative exponent, which is a division by zero: for p>0 we have

0**(-p) = 1/(0**p)
        = 1/0
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!