import matplotlib.pyplot as plt
import numpy as np
from lmfit import Model
x=np.array([4698.031, 4698.027, 4698.024, 4698.021, 4698.017,
4698.014,4698.011, 4698.007, 4698.004, 4698.001, 4697.997, 4697.994,
4697.991, 4697.987, 4697.984, 4697.981, 4697.977, 4697.974, 4697.971,
4697.967, 4697.964, 4697.961, 4697.957, 4697.954, 4697.951, 4697.947,
4697.944, 4697.941, 4697.937, 4697.934, 4697.931, 4697.927, 4697.924,
4697.921, 4697.917])
y=np.array([0.56565, 0.586575, 0.70335, 0.991245, 1.447545, 4.944375,
11.97281, 18.22095, 19.7613, 17.13792, 13.35083, 10.26506, 7.898505,
5.084775, 2.4192, 1.34358, 0.829905, 1.31322, 3.2049, 4.0095, 2.83263,
1.51605, 0.643275, 0.48972, 0.432675, 0.084375, 0.135345, 0.362145,
0.34425, 0.307125, 0.469125, 0.297, 0.183255, 0.528855, 0.523125])
gmodel = Model(gaussian, prefix='p1_') + Model(gaussian, prefix='p2_')
params = gmodel.make_params(p1_amp=0.1, p1_cen=4697.97, p1_wid=0.005,
p2_amp=0.5, p2_cen=4698.00, p2_wid=0.005)
params['p1_cen'].min = x.min()
params['p1_cen'].max = 4697.98
params['p2_cen'].min = 4697.98
params['p2_cen'].max = x.max()
result = gmodel.fit(y, params, x=x)
我正在运行该程序,并且消息错误显示:
回溯(最近通话最近):文件 “”,第8行,在 gmodel = lt.Model(gaussian,prefix ='p1_')+ lt.Model(gaussian,prefix ='p2_')NameError:未定义名称“ gaussian”。
答案 0 :(得分:2)
高斯函数是您必须定义的函数,因此可以在模型中使用它。 在docs中对此进行了很好的解释。
您必须将此添加到您的代码中:
def gaussian(x, amp, cen, wid):
return amp * exp(-(x-cen)**2 / wid)
答案 1 :(得分:2)
您需要定义高斯:
import numpy as np
def gaussian(x, amp, cen, wid):
return amp * np.exp(-(x-cen)**2 / wid)
请参见本页上的模型类部分:https://lmfit.github.io/lmfit-py/model.html#lmfit.model.Model
答案 2 :(得分:0)
您需要定义from numpy import exp
def gaussian(x, amp, cen, wid):
return amp * exp(-(x-cen)**2 / wid)
函数:
GaussianModel
或使用内置的from lmfit.models import GaussianModel
gmodel = GaussianModel(prefix='p1_') + GaussianModel(prefix='p2_')
:
Set