我有未知数量的参数字典(来自其他函数),我遍历字典以将其组件添加到 lmfit 模型中,如下所示:
from lmfit import Parameters
fit_params = Parameters()
for params_name in dict.keys():
current_param = dict[param_name]
fit_params.add(param_name)
我想为每个参数添加表达式
fit_params[param_name].set(expr = 'some_expression_in_function_of_x')
其中 x 是我的自变量,运行程序时出现此错误:
NameError
Traceback (most recent call last):
File "D:\Python\ThinPhy_File_By_File\test_code.py", line 52, in <module>
<_ast.Module object at 0x00000145F661C4E0>
^^^
name 'x' is not defined
File "C:\Python36\lib\site-packages\lmfit\parameter.py", line 548, in __repr__
sval = repr(self._getval())
File "C:\Python36\lib\site-packages\lmfit\parameter.py", line 639, in _getval
check_ast_errors(self._expr_eval)
File "C:\Python36\lib\site-packages\lmfit\parameter.py", line 21, in check_ast_errors
expr_eval.raise_exception(None)
File "C:\Python36\lib\site-packages\lmfit\asteval.py", line 167, in raise_exception
raise exc(self.error_msg)
NameError: name 'x' is not defined in expr='<_ast.Module object at 0x00000145F661C4E0>'
有没有办法定义一个包含模型自变量的表达式?或如何定义它。