gpt4 book ai didi

python - 泊松分布拟合

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:05 25 4
gpt4 key购买 nike

我需要对一组数据拟合泊松分布:

fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/math.gamma(x+1) # Target function
errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function
p0 = [1., 2.] # Initial guess for the parameters
p1, success = optimize.leastsq(errfunc, p0[:], args=(bins_mean, n))

我使用 SciPy documentation 中给出的示例进行了此操作.

如果我评论 Gamma 函数部分,它就像一个魅力,所以问题就在那里,但我不知道如何解决它。

我收到以下错误:

TypeError: can only concatenate list (not "int") to list

fit的输入参数是plt.hist的输出,我查了一下,类型是numpy ndarray

最佳答案

fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/math.gamma(x+1) # Target function
errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function
p0 = [1., 2.] # Initial guess for the parameters
p1, success = optimize.leastsq(errfunc, p0[:], args=(bins_mean, n))

既然你说它在没有 math.gamma(x+1) 部分的情况下工作,我猜如果你改变它会工作

fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/math.gamma(x+1)

from scipy.misc import factorial 
fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/factorial(x)

因为 math.gamma 不喜欢列表(或者我猜任何不是 float 的东西),而阶乘可以与列表一起正常工作?

附带问题:为什么要使用 pow,而不是只使用 **?

关于python - 泊松分布拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20198538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com