python fitting curve with integral function

谁都会走 提交于 2020-01-23 11:12:05

问题


I would like to fit data with integral function(truncated gamma distribution). I tried following code, but errors occur. I am appreciate if you would kind help me. Thank you very much in advance.

%matplotlib inline
import numpy as np
from scipy import integrate
import scipy.optimize
import matplotlib.pyplot as plt

xlist=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
ylist=[1.0, 0.7028985507246377, 0.4782608695652174, 0.36231884057971014,
   0.2536231884057971, 0.1811594202898551, 0.12318840579710147,
   0.08695652173913046, 0.057971014492753645, 0.04347826086956524,
   0.02173913043478263, 0.007246376811594223]

xdata=np.array(xlist)
ydata=np.array(ylist)

parameter_initial=np.array([0.0,0.0,0.0])#a,b,c

def func(x,a,b,c):
    return integrate.quad(lambda t:t^(a-1)*np.exp(-t),x/c,b/c)/integrate.quad(lambda t:t^(a-1)*np.exp(-t),0.0,b/c)

parameter_optimal,cov=scipy.optimize.curve_fit(func,xdata,ydata,p0=parameter_initial) 
print "paramater =", paramater_optimal
y = func(xdata,paramater_optimal[0],paramater_optimal[1],paramater_optimal[2])
plt.plot(xdata, ydata, 'o')
plt.plot(xdata, y, '-')
plt.show()

Following errors occur.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

回答1:


Your code has the following errors:

  • The initial values are inadequate since being zeroes, and in the functions divided between that parameter causing problems because the division between 0 is undefined.

  • The quad() function receives as a second and third parameter a numeric data, not a list, nor a np.ndarray() to some iterable, but in your case the parameter x in your function fun() is an np.ndarray(), What you do is iterate over x and pass that parameter to quad().

  • quad() returns 2 parameters, the first is the value of the integral and the second is the error, so only the first parameter should be used.

  • You must use ** instead of ^.

Considering the above, I propose the following code:

import numpy as np
from scipy import integrate
import scipy.optimize
import matplotlib.pyplot as plt

xlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
ylist = [1.0, 0.7028985507246377, 0.4782608695652174, 0.36231884057971014,
   0.2536231884057971, 0.1811594202898551, 0.12318840579710147,
   0.08695652173913046, 0.057971014492753645, 0.04347826086956524,
   0.02173913043478263, 0.007246376811594223]

xdata = np.array(xlist)
ydata = np.array(ylist)

parameter_initial = np.array([2.5,2.5,2.5]) # a, b, c


def func(x,a,b,c):
    fn = lambda t : t**(a-1)*np.exp(-t)
    den = integrate.quad(fn, 0.0, b/c)[0]
    num = np.asarray([integrate.quad(fn, _x/c, b/c)[0] for _x in x])
    return num/den

parameter_optimal, cov = scipy.optimize.curve_fit(func, xdata, ydata,p0=parameter_initial) 
print("paramater =", parameter_optimal)
y = func(xdata, *parameter_optimal)
plt.plot(xdata, ydata, 'o')
plt.plot(xdata, y, '-')
plt.show()



来源:https://stackoverflow.com/questions/48070610/python-fitting-curve-with-integral-function

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