gpt4 book ai didi

python - curve_fit、python和excel之间的幂律回归问题

转载 作者:太空宇宙 更新时间:2023-11-03 19:43:49 24 4
gpt4 key购买 nike

抱歉,我是 python 和堆栈流方面的新手。所以我无法发布图像。

我想用Python中的curve_fit函数进行幂律回归。但结果对我来说很奇怪。我用excel进一步检查了一下。看起来这两者差别很大。黑线是curve_fit的结果,红线的参数来自excel。有人可以让我知道其中的区别吗?谢谢你!

x=[164000,400,13000,700,57000,108,12000]
y=[0.011970,0.000098,0.066100,0.004300,0.042600,0.000061,0.002858 ]

def f(x,a,b):
return a*x**b

popt,pocv=curve_fit(f,x,y)

ax.set_xscale("log")
ax.set_yscale("log")
ax.set_ylim(0.00001,0.1)
ax.set_xlim(10,1000000)

ax.scatter(x,y)

px=np.linspace(10,1000000,1000)

#parameter form curve_fit
py=a*px**b
[enter image description here][1]
#parameter from excel
pyy=3E-6*px**0.8305

ax.loglog(px,pyy,color="red")
ax.loglog(px,py,color="k")

最佳答案

您在双对数空间中绘制数据这一事实应该会给您一个很好的提示:适合对数空间。也就是说,将 np.log(a*x**b) 拟合到 np.log(y) 中。对实际运行并获得良好适合的脚本的修改将是:

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

x=[164000,400,13000,700,57000,108,12000]
y=[0.011970,0.000098,0.066100,0.004300,0.042600,0.000061,0.002858 ]

def f(x, a, b):
return np.log(a*x**b)

popt,pcov=curve_fit(f, x, np.log(y), [1.e-6, 0.9])

ax = plt.gca()

ax.set_xscale("log")
ax.set_yscale("log")
ax.set_ylim(0.00001,0.1)
ax.set_xlim(10,1000000)

ax.scatter(x,y)

px = np.linspace(10,1000000,1000)
a, b = popt
print("Parameters: a=%g, b=%g" % (a, b))

#parameter form curve_fit
py=a*px**b

#parameter from excel
pyy=3e-6*px**0.8305

ax.loglog(px,pyy, color="red")
ax.loglog(px,py, color="k")
plt.show()

始终确保提供参数的初始值,并确保打印出结果。例如,运行此命令将打印出 Parameters: a=2.78612e-06, b=0.829763 并显示两条预测线几乎彼此重叠。

为了获得更好的曲线拟合体验,您可能会发现 lmfit ( https://lmfit.github.io/lmfit-py/ ) 很有用(是的,我是主要作者并且有偏见)。使用 lmfit,您的合身度可能是:

import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from lmfit import Model

x=[164000,400,13000,700,57000,108,12000]
y=[0.011970,0.000098,0.066100,0.004300,0.042600,0.000061,0.002858 ]

def f(x, a, b):
return np.log(a*x**b)

model = Model(f)
params = model.make_params(a=1.e-6, b=0.9)
result = model.fit(np.log(y), params, x=x)

print(result.fit_report())

px = np.linspace(10,1000000,1000)
plt.scatter(x,y)
plt.loglog(px, np.exp(result.eval(x=px)), color="k")
plt.show()

请注意,对于 lmfit,参数是使用 f() 模型函数中的名称​​命名的。这将打印出一份拟合报告,其中包括估计的不确定性:

[[Model]]
Model(f)
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 16
# data points = 7
# variables = 2
chi-square = 14.7591170
reduced chi-square = 2.95182340
Akaike info crit = 9.22165592
Bayesian info crit = 9.11347621
[[Variables]]
a: 2.7861e-06 +/- 6.3053e-06 (226.31%) (init = 1e-06)
b: 0.82976271 +/- 0.25700150 (30.97%) (init = 0.9)
[[Correlations]] (unreported correlations are < 0.100)
C(a, b) = -0.958

并生成一个图 enter image description here

关于python - curve_fit、python和excel之间的幂律回归问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60274794/

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