- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
尝试从 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]
这有给出 Inf
的趋势对于 sigma
的大值和 gamma
, 和/或 center
.我相信你会得到这样的 Inf
值,这是 NaN
的原因你看到的消息。拟合例程(在 Fortran 中)不处理 NaN
或 Inf
优雅地(实际上,“完全”)。这是该特定模型的真正限制。你会看到维基百科页面上的例子都有 x
值更接近 1
比200
, 和 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
并显示类似的情节
希望对您有所帮助。
关于python - 使用 lmfit ExponentialGaussianModel( ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54197603/
尝试从 lmfit 拟合 ExponentialGaussianModel() 但收到以下错误消息:The input contains nan values 我在 Windows 上使用 Jupyt
我是一名优秀的程序员,十分优秀!