You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a model function that contains a keyword argument that is not part of the fit parameters (but not a compulsory independent variable either). My expectation was that I can just pass it as a keyword argument to the model's fit() method when I need to set it. Here's an example with s being the argument in question.
importlmfitimportnumpyasnpfrommatplotlibimportpyplotasplt# Create a model.deffoo(x, a=3, b=5, s=1):
returna+s*b*x**2mdl=lmfit.Model(foo, param_names=['a', 'b'])
mdl.set_param_hint('a', value=3)
mdl.set_param_hint('b', value=5)
# Synthesize some noisy data.x=np.linspace(-5, 5, 101)
s=1+0.7*np.sin(x)
y=foo(x, 2.3, 1.7, s) +np.random.normal(0, 1, x.shape)
# Fit the data and report the resultfitres=mdl.fit(y, x=x, s=s)
print(fitres.fit_report())
fitres.plot()
plt.show()
# Manually check the result.plt.plot(x, y, '.', label='data')
plt.plot(x, fitres.eval(x=x), label='fit result eval()')
plt.plot(x, foo(x, a=fitres.params['a'], b=fitres.params['b'], s=s), label='manual check')
plt.legend()
plt.show()
Passing s to fit() like this results a warning.
C:\Users\wilde\miniconda3\lib\site-packages\lmfit\model.py:1053: UserWarning: The keyword argument s does not match any arguments of the model function. It will be ignored.
However, the fit still seems to perform as expected, i.e. the argument is passed on to the fitting function, the resulting parameters are correct. Here's the fit report (a = 2.3 and b = 1.7 were used during data synthesis).
Adding to the confusion, the fit result's plot() function draws the correct residuals and data but an incorrect best fit, i.e. it really does ignore s here.
The eval() method on the other hand seems to respect s.
Version information
Python: 3.9.18 | packaged by conda-forge | (main, Aug 30 2023, 03:40:31) [MSC v.1929 64 bit (AMD64)]
lmfit: 1.2.2, scipy: 1.11.3, numpy: 1.26.0,asteval: 0.9.31, uncertainties: 3.1.7
The text was updated successfully, but these errors were encountered:
The plotting feature is a very rudimentary convenience function. You presumably need to pass the independent variable to the plot function as well. I can take a closer look at this, but it doesn’t necessarily appear as a real GitHub Issue. If you read the instructions you would have realized that, and at least provided all the information requested in the template.
lmfit
locked and limited conversation to collaborators
Nov 7, 2023
Description
I have a model function that contains a keyword argument that is not part of the fit parameters (but not a compulsory independent variable either). My expectation was that I can just pass it as a keyword argument to the model's
fit()
method when I need to set it. Here's an example withs
being the argument in question.Passing
s
tofit()
like this results a warning.However, the fit still seems to perform as expected, i.e. the argument is passed on to the fitting function, the resulting parameters are correct. Here's the fit report (
a = 2.3
andb = 1.7
were used during data synthesis).Adding to the confusion, the fit result's
plot()
function draws the correct residuals and data but an incorrect best fit, i.e. it really does ignores
here.The
eval()
method on the other hand seems to respects
.Version information
The text was updated successfully, but these errors were encountered: