Python-错误的适合

问题描述 投票:1回答:1

我正在尝试再现已知的拟合结果(在期刊论文中报告):对数据应用幂律模型。从下面的图A中可以看出,我能够通过使用已知的最佳拟合参数来重现结果。

Both axis: log-log scale

但是,我无法自己得出最适合的参数。

wrong

A案件返回,

OptimizeWarning: Covariance of the parameters could not be estimated(如果我省略了几个初始数据点,则拟合返回一些不错的结果,但仍与已知的最佳拟合结果不同)。编辑:现在,这次我刚刚发现了新的其他错误消息。(1)RuntimeWarning: overflow encountered in power(2)RuntimeWarning: invalid value encountered in power

案例B(初始猜测更接近最佳拟合参数),

RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 5000.如果我将maxfev设置得更高,以考虑到此错误消息,则拟合将起作用,但会返回错误的结果(与最佳拟合结果相比,拟合度非常错误)。

python curve-fitting covariance power-law loglog
1个回答
2
投票

我评论过:

由于您将数据绘制在对数-对数图上,您是否也适合对数(y)和对数(x)?由于您的y数据相差5或6阶大小,如果您不适合对数空间,则仅那些3或4个数据y值最高的点将很重要。

显然,这有点暗示。所以我会更直接:如果您要在对数空间中绘图,请在对数空间中进行拟合。

但是,您的模型很容易从negative**fraction和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()

希望有帮助...

© www.soinside.com 2019 - 2024. All rights reserved.