简体   繁体   English

符号 model function 传递给 lmfit

[英]symbolic model function passing to lmfit

symbolic model function passing Hi, I stuck in my fitting due to my symbolic function, Please kindly let me know how this issue could be solved here is my code: I need to pass tow independent_var which are xCl and XI and a is parameter: symbolic model function passing Hi, I stuck in my fitting due to my symbolic function, Please kindly let me know how this issue could be solved here is my code: I need to pass tow independent_var which are xCl and XI and a is parameter:

import numpy as np
import lmfit
import sympy as sp    
anion = {'I': 1, 'Cl': 1}
xa = {'xI': 0.2, 'xCl': 0.3}
xCl = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
xI = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.1, 0.8, 0.11, 0.12]

def g(xa, anion, a):
    xzc = 0
    for k1, k2 in zip(anion, xa):
        xzc += sp.symbols(f"x{k1}") * sp.symbols(f"x{k2}") + a
    return xzc
obj = lmfit.Model(g, independent_vars=['xCl', 'xI'])
pars = obj.make_params(a=0.1)

result = mod.fit(ydat, pars, xCl, xI)
print(result.fit_report())

but I' ve found;但我找到了;

**************************************************
Traceback (most recent call last):
  File "F:\Zohreh\MainZohreh\postdoc-field\CSU\pythonProject\simple_Lmfit.py", line 36, in <module>
    obj = lmfit.Model(g, independent_vars=['xCl', 'xI'])
  File "C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\lmfit\model.py", line 277, in __init__
    self._parse_params()
  File "C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\lmfit\model.py", line 541, in _parse_params
    raise ValueError(self._invalid_ivar % (arg, fname))
ValueError: Invalid independent variable name ('xCl') for function g
*********************************************************

I found, it is necessary to use lambdify to change sympy equation to numpy one.我发现,有必要使用lambdify将sympy方程改为numpy之一。 this way it it will be granted that ndarray float are sent to lmfit model.这样,将允许将 ndarray 浮点数发送到 lmfit model。

The independent_vars passed to lmfit.Model must match the names of the function arguments of the model function. The independent_vars passed to lmfit.Model must match the names of the function arguments of the model function. In your case, the model function has a signature of g(xa, anion, a) so those would be xa , anion , and a .在您的情况下, model function 具有g(xa, anion, a)的签名,因此它们将是xaaniona Neither xCl nor xI makes sense as independent variables - they aren't variables to the model function at all. xClxI作为独立变量都没有意义 - 它们根本不是 model function 的变量。 That is what the exception那是什么异常

ValueError: Invalid independent variable name ('xCl') for function g

is expressing.正在表达。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM