使用 lmfit ExponentialGaussianModel( )
the use of lmfit ExponentialGaussianModel( )
尝试从 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))
我得到一个错误,有 nan 输入值。我期待着手册中显示的报告。
lmfit
中使用的指数高斯定义来自https://en.wikipedia.org/wiki/Exponentially_modified_Gaussian_distribution。
指数项为
exp[center*gamma + (gamma*sigma)**2/2 - gamma*x]
对于 sigma
和 gamma
、and/or center
的大值,这有给出 Inf
的趋势。我相信您会得到这样的 Inf
值,这就是您看到的 NaN
消息的原因。拟合例程(在 Fortran 中)不能很好地处理 NaN
或 Inf
(实际上是 "at all")。这是该特定模型的真正限制。您会看到维基百科页面上的示例的 x
值都比 200
更接近 1
,gamma
和 sigma
也在订单上的 1,而不是 50 左右,这是自动 guess
给出的。
我认为指数修正高斯分布的更简单定义对您来说会更好。有
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()
程序。这些不必很近,只要相距不远即可。
完整的脚本可能是:
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()
这将打印出来
[[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
并显示类似
的情节
希望对您有所帮助。
尝试从 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))
我得到一个错误,有 nan 输入值。我期待着手册中显示的报告。
lmfit
中使用的指数高斯定义来自https://en.wikipedia.org/wiki/Exponentially_modified_Gaussian_distribution。
指数项为
exp[center*gamma + (gamma*sigma)**2/2 - gamma*x]
对于 sigma
和 gamma
、and/or center
的大值,这有给出 Inf
的趋势。我相信您会得到这样的 Inf
值,这就是您看到的 NaN
消息的原因。拟合例程(在 Fortran 中)不能很好地处理 NaN
或 Inf
(实际上是 "at all")。这是该特定模型的真正限制。您会看到维基百科页面上的示例的 x
值都比 200
更接近 1
,gamma
和 sigma
也在订单上的 1,而不是 50 左右,这是自动 guess
给出的。
我认为指数修正高斯分布的更简单定义对您来说会更好。有
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()
程序。这些不必很近,只要相距不远即可。
完整的脚本可能是:
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()
这将打印出来
[[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
并显示类似
的情节希望对您有所帮助。