fitting array parameter

97 views
Skip to first unread message

Zohreh Karimzadeh

unread,
Jul 7, 2022, 8:24:37 AM7/7/22
to lmfit-py
Hi lmfit
I stuck in my fitting due to my parameters,
Please kindly let me know how this issue could be solved.
----------------------------------------------------
from lmfit import Model
# (y, params, independent_variables=['x1, 'x2']assign initial value for parameters)

def cationshortrange(xn,wnca, neutral = ['water'], cation = ['H'], anion = ['I'], zc = [1], za = [1], ea = [0.1] ):
term_wnMa = 0
for k in range(len(anion)):
for j in range(len(cation)):
if j < 1:
for i in range(len(neutral)):
term_wnMa += xn[i] * ea[k] * ((zc[j] + za[k]) / (za[k])) * wnca[i, j, k]
return print(term_wnMa)
# cationshortrange(wnca[water, H,I] ==+ 0.5, xn = 0.1 , neutral = ['water'], cation = ['H', 'K'], anion = ['I', 'Br'], zc = [1, 1], za = [1, 1], ea = [0.1] )
xn = [1, 2, 3, 4, 5]
y = [0.1, 0.2, 1.2, 1.6, 2.0]
gmodel = Model(cationshortrange)
params = gmodel.make_params()
print('parameter names: {}'.format(gmodel.param_names))
print('independent variables: {}'.format(gmodel.independent_vars))
result = gmodel.fit(y, params)
print(result.fit_report())
print(result.params.items())
-------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Zohreh\PycharmProjects\PSCPureFit\dicParamLmfit.py", line 23, in <module>
    result = gmodel.fit(y, params)
  File "C:\Users\Zohreh\PycharmProjects\PSCPureFit\venv\lib\site-packages\lmfit\model.py", line 1023, in fit
    if not np.isscalar(kwargs[var]):
KeyError: 'xn'
parameter names: ['wnca']
independent variables: ['xn']

Process finished with exit code 1
----------------------------------------------------------------
In fact based on cation and anion I will have different number of parameters in wnca array.

Matt Newville

unread,
Jul 7, 2022, 12:10:46 PM7/7/22
to lmfit-py
It looks to me like your model function `cationexchange` has one variable parameter: `wnca`.   All the other function arguments have default values that are lists and so would be treated as independent variables: those do not vary in the fit, and you will have to supply them to 
Model.fit().    It is possible that you intend some of `zc`, `za`, or `ea` to have values that vary in the fit, but that is not how lmfit work.  Each Parameter must have a unique name and a single floating-point (scalar) value.

Also, with
     params = gmodel.make_params()

or at some point before you use `params` in a fit,  you **must** provide initial values for all the variable parameters, in this case, `wnca`.  You cannot have initialized parameter values.


--
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/5be3db54-52fc-4858-ab5c-9f82310342f9n%40googlegroups.com.


--
--Matt Newville <newville at cars.uchicago.edu630-327-7411

Zohreh Karimzadeh

unread,
Jul 7, 2022, 12:14:00 PM7/7/22
to lmfi...@googlegroups.com
Thanks millions!
Zohreh Karimzadeh
Skype Name 49a52224a8b6b38b
Twitter Account @zohrehkarimzad1
+989102116325                                                        
((((((((((((((((Value Water)))))))))))))))


Reply all
Reply to author
Forward
0 new messages