具有积分功能的python拟合曲线 [英] python fitting curve with integral function

查看:182
本文介绍了具有积分功能的python拟合曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用整数函数(截断的伽马分布)拟合数据。
我尝试了以下代码,但发生错误。如果您能帮助我,我将不胜感激。

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()

发生以下错误。

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


推荐答案

您的代码具有以下错误:

Your code has the following errors:


  • 由于初始值为零,因此初始值不足,并且该函数在该参数之间划分由于未定义0之间的除法,因此会引起问题。

  • 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.

quad()函数将第二次接收第三个参数是数字数据,而不是列表,也不是 np.ndarray()的可迭代对象,但在您的情况下,函数x中的参数x fun() np.ndarray(),您要做的是遍历x并将该参数传递给 quad()

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()返回2个参数,第一个是积分的值,第二个是误差的值,因此仅应使用第一个参数。

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.

您必须使用 ** 而不是 ^

考虑到上述情况,我提出以下建议机翼代码:

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()

这篇关于具有积分功能的python拟合曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆