Scipy.optimize.curvefit日志功能

时间:2014-12-31 15:15:31

标签: python logging scipy

我试图为这个等式生成方程参数:

y = -log((a + bx)/(c + x))

我有一组x,y和a,b和c的样本数据。

当我执行以下操作时:

from scipy.optimize import curve_fit
from scipy import log as log
from scipy import exp as exp
import numpy as np

#Should generate: a = 2.22726000005 , b = 0.1073, c = 2.68937000008

a=1
b=1e-6
c=1

yarr = np.array([0.31776,0.30324,0.28148,0.2651,0.24328,0.22144,0.19799,0.17431,0.14685,0.11521])
xarr = np.array([0.250,0.225,0.200,0.175,0.150,0.125,0.100,0.075,0.050,0.025])

def func(x, a, b, c):

    return (log(c+x)-log(a+(b*x)))

popt, pcov = curve_fit(func,  xarr, yarr, (a,b,c))

print "a = %s , b = %s, c = %s" % (popt[0], popt[1], popt[2])

这应该给我:

a = 2.22726000005,b = 0.1073,c = 2.68937000008

但我得到的是:

a = 0.37366276487,b = 0.415297976794,c = 0.406353416622

它给出了一个很好的曲线,但它没有接近正确的值。

我在这里已经阅读了几个类似的问题,但没有一个解决方案适用于我。

任何提示?

由于 米

1 个答案:

答案 0 :(得分:2)

我无法使用您提供的值与curve_fit生成的值一起很好地重现所提供的数据,因此您可能需要提供有关该问题的更多信息:

In [48]: pylab.plot(xarr, yarr, label='data')
In [49]: pylab.plot(xarr,func(xarr, *popt), label='curve_fit')
In [50]: ap, bp, cp = 2.22726000005, 0.1073, 2.68937000008
In [51]: pylab.plot(xarr,func(xarr, ap,bp,cp), label='supplied a,b,c')
In [52]: pylab.legend()
In [53]: pylab.show()

enter image description here