[英]looping over legit parameters
I am iterating over N files and fitting all the data using lmfit Gaussian Model.我正在迭代 N 个文件并使用 lmfit Gaussian Model 拟合所有数据。 I would like to obtain an array of all the parameters for the different files so I can plot them against another variable.
我想获得一个包含不同文件的所有参数的数组,以便我可以针对另一个变量对它们进行 plot。 Any idea how to do so?
知道怎么做吗?
this is my code:这是我的代码:
def gaussian(x,center,sigma,amplitude):
return amplitude*(1/(sigma*(np.sqrt(2*np.pi))))*np.exp(((-(x-center)**2))/(2*(sigma**2)))
for f in os.listdir(directory):
f= os.path.join(directory, f)
data=np.loadtxt(f)
x=data[np.where(data[:,0]<wavemax),0]
y=data[np.where(data[:,0]<wavemax),1]
x=x[0,np.where(x[0]>wavemin)][0]
y=y[0,np.where(x>wavemin)][0]
gauss1 = Model(gaussian,prefix='g1_')
pars=gauss1.make_params()
pars['g1_center'].set(2855.0, True, 2830.0, 2900.0)
pars['g1_sigma'].set(6, True, 1.0, 20.0)
pars['g1_amplitude'].set(0.6, True, 0.01, 30)
gauss2 = Model(gaussian,prefix='g2_')
pars.update(gauss2.make_params())
pars['g2_center'].set(2900.0, True, 2850.0, 2950.0)
pars['g2_sigma'].set(10.0, True, 1.0, 20.0)
pars['g2_amplitude'].set(0.6, True, 0.01, 30)
gauss3 = Model(gaussian,prefix='g3_')
pars.update(gauss3.make_params())
pars['g3_center'].set(2925.0, True, 2850.0, 2950.0)
pars['g3_sigma'].set(10.0, True, 1.0, 50.0)
pars['g3_amplitude'].set(3, True, 0.01, 30)
gauss4 = Model(gaussian,prefix='g4_')
pars.update(gauss4.make_params())
pars['g4_center'].set(2970.0, True, 2850.0, 3000.0)
pars['g4_sigma'].set(14.0, True, 1.0, 20.0)
pars['g4_amplitude'].set(3, True, 0.01, 30)
gauss5= Model(gaussian,prefix='g5_')
pars.update(gauss5.make_params())
pars['g5_center'].set(3050, True, 2750, 3050, None, None)
pars['g5_sigma'].set(100, True, None, None, None, None)
pars['g5_amplitude'].set(5, True, None, None, None, None)
mod=gauss1+gauss2+gauss3+gauss4+gauss5 # fitting the entire curve
init = mod.eval(pars, x=x) #specifying the parameters for the fitting of all the curve
out = mod.fit(y, pars, x=x)
make a results
list before your loop, and for each fit, after the fit is complete, append out.params
to that results
list (and maybe other stuff to help mark fit details).在您的循环之前制作一个
results
列表,并且对于每个拟合,在拟合完成后,append out.params
到该results
列表(可能还有其他有助于标记拟合细节的东西)。
Then use the list of resulting fit parameters as you like.然后根据需要使用生成的拟合参数列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.