python定义变量参数,在python lmfit中设置包含自变量的参数表达式

I have dictionary of parameters with unknown number of those parameters (comes from other function), I looped through the dictionary to add its components to an lmfit models as follows:

from lmfit import Parameters

fit_params = Parameters()

for params_name in dict.keys():

current_param = dict[param_name]

fit_params.add(param_name)

I wanted to add expression to each parameter with

fit_params[param_name].set(expr = 'some_expression_in_function_of_x')

where x is my independent variable, when running the program I have this error:

NameError

Traceback (most recent call last):

File "D:\Python\ThinPhy_File_By_File\test_code.py", line 52, in

^^^

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>'

is there any way to define an expression that contains the model independent variable? or how to define it.

解决方案

First, dict is a builtin and using it for a variable name can cause havoc.

To use your x in an expression, you would have to add your x to the symbol table used to evaluate the expression, as with

fit_params = Parameters()

fit_params._asteval.symtable['x'] = x

But: You should be careful doing this with ndarrays. It can work, but Parameters in lmfit are generally meant to be floats.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值