python拟合曲线并求积分_具有积分功能的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()

Following errors occur.

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:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值