gpt4 book ai didi

Python - 错误的配合

转载 作者:行者123 更新时间:2023-12-01 00:09:23 24 4
gpt4 key购买 nike

我正在尝试重现一个已知的拟合结果(在期刊论文中报告):将幂律模型应用于数据。正如下面的图 A 所示,我能够使用已知的最佳拟合参数重现结果。

< Plot-A:文献中已知的结果 > Both axis: log-log scale

但是,我无法自己重新推导最佳拟合参数。

< Plot-B: curve_fit 和 lmfit 的拟合不正确 > wrong

案例 A 返回,

OptimizeWarning:无法估计参数的协方差(如果我省略了几个初始数据点,拟合会返回一些不错的结果,但仍然与已知的最佳拟合结果不同) 。编辑:现在,我这次刚刚发现了新的附加错误消息..:(1) RuntimeWarning:电源遇到溢出(2) RuntimeWarning:电源中遇到无效值

情况 B(初始猜测更接近最佳拟合参数)返回,

运行时错误:未找到最佳参数:函数调用次数已达到 maxfev = 5000。如果我将 maxfev 设置得更高以考虑到此错误消息,则拟合可以工作,但会返回错误的结果(与最佳拟合结果相比,拟合非常错误)。

最佳答案

正如我评论的:

Since you plot the data on a log-log plot, are you also fitting with the log(y) and log(x)? Since your y data varies by 5 or 6 orders of magnitude, if you are not fitting in log-space, only those 3 or 4 data points with the highest y value will matter.

显然这个暗示有点太微妙了。所以我会更直接:如果您在对数空间中绘图,请适合对数空间。

而且,您的模型很容易从负**分数和 NaN 生成复数,这无疑会导致您的所有拟合出现问题。始终打印出最佳拟合参数和协方差矩阵。

因此,您可能需要对参数施加限制(当然,我不知道您的模型是否正确,或者实际上是您认为使用的“正确答案”)。也许可以从这样的事情开始:

import matplotlib.pyplot as plt
from lmfit import Model
import numpy as np

# the data can be found at the end of this post.
xx, yy = np.genfromtxt('the_data', unpack=True)

# the BPL function
def bendp(x, A, x_bend, allo, alhi, c):
numer = A * x**-allo
denom = 1 + (x/x_bend)**(alhi-allo)
out = (numer/denom) + c
return np.log(out) ## <- TAKE THE LOG


mod = Model(bendp)
para = mod.make_params(A = 0.01, x_bend=1e-2, allo=1., alhi=2., c=1e-2)

# Note: your model is very sensitive # make sure A, x_bend, alhi, and c cannot be negative
para['A'].min = 0
para['x_bend'].min = 0
para['alhi'].min = 0
para['alhi'].max = 3
para['c'].min = 0


result = mod.fit(np.log(yy), para, x=xx) ## <- FIT THE LOG

print(result.fit_report())

plt.loglog(xx, yy, linewidth=0, marker='o', markersize=6)
plt.loglog(xx, np.exp(result.best_fit), 'r', linewidth=5)
plt.show()

希望有帮助...

关于Python - 错误的配合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59734518/

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