lmfit指数高斯模型的应用()

2024-09-29 01:23:39 发布

您现在位置:Python中文网/ 问答频道 /正文

正在尝试从lmfit匹配ExponentialGaussianModel(),但得到以下错误消息:The input contains nan values

我在windows上使用Jupyternotebook,对python和lmfit还不熟悉。我发现lmfit文档对于初学者来说有点晦涩难懂,希望能在这里找到帮助。下面是我的代码:我想生成指数高斯直方图,提取数据点并练习使用lmfit库进行拟合。(我想练习拟合并找到最少数量的点来重现生成直方图所用的参数)

from scipy.stats import exponnorm
from lmfit.models import ExponentialGaussianModel

K2 = 1.5
r2 = exponnorm.rvs(K2, size=500, loc = 205, scale = 40)

Q           =  np.histogram(r2,500)
exp_gaus_x  =  Q[1]
exp_gaus_y  =  Q[0]

tof_x       =  exp_gaus_x[1:]
tof_y       =  exp_gaus_y

mod =  ExponentialGaussianModel()
pars = mod.guess(tof_y, x=tof_x)
out  = mod.fit(tof_y, pars, x=tof_x)
(out.fit_report(min_correl=0.25))

我得到一个错误,即有一个输入值。我在等手册上显示的报告。在


Tags: fromimportmod错误k2直方图outfit
1条回答
网友
1楼 · 发布于 2024-09-29 01:23:39

lmfit中使用的指数高斯的定义来自https://en.wikipedia.org/wiki/Exponentially_modified_Gaussian_distribution。 指数项为

exp[center*gamma + (gamma*sigma)**2/2 - gamma*x]

对于sigma和{}和/或{}的大ish值,这有可能给出{}。我相信您得到了这样的Inf值,这就是您看到的NaN消息的原因。fitting例程(在Fortran中)并不优雅地处理NaN或{}(实际上是“根本”)。这是那个特定模式的一个真正的局限性。在这个页面上,{cd4}的顺序比cd5}的顺序更接近。在

我认为对于指数修正高斯函数,一个更简单的定义会更好。与

def expgaussian(x, amplitude=1, center=0, sigma=1.0, gamma=1.0):
    """ an alternative exponentially modified Gaussian."""
    dx = center-x
    return amplitude* np.exp(gamma*dx) * erfc( dx/(np.sqrt(2)*sigma))

虽然参数的含义已经发生了一些变化,但是您将得到一个合适的匹配,并且需要显式地给出起始值,而不是依赖于guess()过程。它们不必离得很近,只是离得不远。在

完整的脚本可以是:

^{pr2}$

会打印出来的

[[Model]]
    Model(expgaussian)
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 65
    # data points      = 500
    # variables        = 4
    chi-square         = 487.546692
    reduced chi-square = 0.98295704
    Akaike info crit   = -4.61101662
    Bayesian info crit = 12.2474158
[[Variables]]
    gamma:      0.01664876 +/- 0.00239048 (14.36%) (init = 0.1)
    sigma:      39.6914678 +/- 3.90960254 (9.85%) (init = 20)
    center:     235.600396 +/- 11.8976560 (5.05%) (init = 250)
    amplitude:  3.43975318 +/- 0.15675053 (4.56%) (init = 2)
[[Correlations]] (unreported correlations are < 0.100)
    C(gamma, center)     =  0.930
    C(sigma, center)     =  0.870
    C(gamma, amplitude)  =  0.712
    C(gamma, sigma)      =  0.693
    C(center, amplitude) =  0.572
    C(sigma, amplitude)  =  0.285

展示一个情节

enter image description here

希望有帮助。在

相关问题 更多 >