首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >lmfit ExponentialGaussianModel( )的使用

lmfit ExponentialGaussianModel( )的使用
EN

Stack Overflow用户
提问于 2019-01-15 19:04:02
回答 1查看 719关注 0票数 2

尝试从lmfit拟合ExponentialGaussianModel(),但收到以下错误消息:The input contains nan values

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

代码语言:javascript
复制
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))

我得到的错误是有nan输入值。我期待着一份如手册中所示的报告。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-16 10:26:17

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

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

这倾向于为sigmagamma和/或center的大值提供Inf。我相信你得到了这样的Inf值,这就是你看到的NaN消息的原因。拟合例程(在Fortran中)不能很好地处理NaNInf (实际上,“完全”)。这是该特定模型的一个真正的限制。您将看到,维基百科页面上的所有示例的x值都更接近1,而不是200gammasigma的值也在1左右,而不是自动guess提供的50左右。

我认为指数修正高斯的更简单的定义对你来说会更好。使用

代码语言:javascript
复制
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()过程。这些不需要很近,只要不太远就行了。

完整的脚本可能是:

代码语言:javascript
复制
import numpy as np
from scipy.stats import exponnorm
from scipy.special import erfc
from lmfit import Model
import matplotlib.pyplot as plt

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))

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 =  Model(expgaussian)
pars = mod.make_params(sigma=20, gamma=0.1, amplitude=2, center=250)

out  = mod.fit(tof_y, pars, x=tof_x)

print(out.fit_report())

plt.plot(tof_x, tof_y, label='data')
plt.plot(tof_x, out.best_fit, label='fit')
plt.legend()
plt.show()

它将打印出来

代码语言:javascript
复制
[[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

并显示如下图

希望这能有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54197603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档