export fitted parameters progrematicall

58 views
Skip to first unread message

Zohreh Karimzadeh

unread,
Jul 7, 2022, 3:08:45 AM7/7/22
to lmfit-py
Hi lmfit
In many cases we might want to extract parameters and standard error estimates programmatically rather than by reading the fit report (e.g., if the fit will be used to produce a data point on another plot, then the standard error can be used for computing error bars).
**********************************
from scipy.optimize import least_squares
from matplotlib.pylab import plt
import numpy as np
from numpy import exp, linspace, random
from lmfit import Model


def gaussian(x, amp, cen, wid):
return amp * np.exp(-(x-cen)**2 / wid)
x = linspace(-10, 10, 101)
y = gaussian(x, 2.33, 0.21, 1.51) + random.normal(0, 0.2, len(x))
gmodel = Model(gaussian)
params = gmodel.make_params()
# print('parameter names: {}'.format(gmodel.param_names))
# print('independent variables: {}'.format(gmodel.independent_vars))
result = gmodel.fit(y, params, x=x, amp=5, cen=5, wid=1)
print(result.fit_report())
lst = []
for name, param in out.params.items():
lst.append(param.value)
lst.append(param.stderr)
*****************************************
I got this erroer:
NameError: name 'out' is not defined
****************************************
What should I do??

Julian Hochhaus

unread,
Jul 7, 2022, 3:18:59 AM7/7/22
to lmfit-py
Hi, 
You used result as variable name for your ModelResult.
Therefore you need to change 
out.params.items() to result.params.items()

By the way you may need to improve the initial guesses for  the params to be closer to your true values (e.g. amp~2.3; cen~1.5)
Kind regards

Zohreh Karimzadeh

unread,
Jul 7, 2022, 4:11:27 AM7/7/22
to lmfit-py
BIG help!
Really thanks

Zohreh Karimzadeh

unread,
Jul 7, 2022, 4:12:40 AM7/7/22
to lmfit-py
What about if one needs to save the parameters in a dictionary with their name keys?

Matt Newville

unread,
Jul 7, 2022, 1:05:29 PM7/7/22
to lmfit-py
On Thu, Jul 7, 2022 at 3:12 AM Zohreh Karimzadeh <z.kari...@gmail.com> wrote:
What about if one needs to save the parameters in a dictionary with their name keys?

I'm not sure I understand.  You mean like
   from lmfit import Parameters
  
   a_params = Parameters()
   a_parames.add('alpha', value=3.0)
   a_parames.add('beta', value=4.2)

   b_params = Parameters()
   b_parames.add('alpha', value=7002)
   b_parames.add('beta', value=900.)

   my_params = {'a': a_params, 'b': b_params}

That is how I read "parameters saved in a dictionary", but I sort of doubt that is what you mean.

A lmfit.Parameters object is a subclass of a Python dictionary, with keys being names, and each value being a single lmfit Parameter object.  
A Parameter has a name (we ensure that matches the key in the Parameters object) and a scalar value.  There are no exceptions.

It would be common to have something like

   c_params = Parameters()
   c_parames.add('c1_alpha', value=7002)
   c_parames.add('c1_beta', value=900.)
   c_parames.add('c2_alpha', value=5002)
   c_parames.add('c2_beta', value=200.)
   c_parames.add('c3_alpha', value=5002)
   c_parames.add('c3_beta', value=400.)

Models can take a "prefix" argument for just such uses.    

--Matt

Zohreh Karimzadeh

unread,
Jul 8, 2022, 12:50:01 AM7/8/22
to lmfi...@googlegroups.com
Thank you very much.
Zohreh Karimzadeh
Skype Name 49a52224a8b6b38b
Twitter Account @zohrehkarimzad1
+989102116325                                                        
((((((((((((((((Value Water)))))))))))))))


--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CA%2B7ESboy08Xm2huwC%2B4OD-aVEBUo2sNMab%2B5MF3NwRUUO3Q1qg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages