简体   繁体   English

使用 sympy 传递 mdel 时,lmfit 中的 numpy ndarray 错误

[英]numpy ndarray error in lmfit when mdel is passed using sympy

I got the following error:我收到以下错误:

<lambdifygenerated-1>:2: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. <lambdifygenerated-1>:2: VisibleDeprecationWarning: 从不规则的嵌套序列创建一个 ndarray(这是一个列表或元组的列表或元组或具有不同长度或形状的 ndarray)已被弃用。 If you meant to do this, you must specify 'dtype=object' when creating the ndarray.return numpy.array((A1 exp(-1/2 (x - xc1)**2/sigma1**2), 0, 0))如果您打算这样做,则必须在创建 ndarray 时指定 'dtype=object'。return numpy.array((A1 exp(-1/2 (x - xc1)**2/sigma1**2), 0, 0))

Here I have just one model but this code is written for model combination in fitting by the lmfit Please kindly let me know about it.在这里,我只有一个 model 但此代码是为 model 组合编写的,由 lmfit 安装请告诉我。

import matplotlib.pyplot as plt
import numpy as np
import sympy
from sympy.parsing import sympy_parser
import lmfit

gauss_peak1 = sympy_parser.parse_expr('A1*exp(-(x-xc1)**2/(2*sigma1**2))')
gauss_peak2 = 0
exp_back = 0

model_list = sympy.Array((gauss_peak1, gauss_peak2, exp_back))
model = sum(model_list)
print(model)



model_list_func = sympy.lambdify(list(model_list.free_symbols), model_list)
model_func = sympy.lambdify(list(model.free_symbols), model)


np.random.seed(1)
x = np.linspace(0, 10, 40)
param_values = dict(x=x, A1=2, sigma1=1, xc1=2)
y = model_func(**param_values)
yi = model_list_func(**param_values)
yn = y + np.random.randn(y.size)*0.4

plt.plot(x, yn, 'o')
plt.plot(x, y)


lm_mod = lmfit.Model(model_func, independent_vars=('x'))
res = lm_mod.fit(data=yn, **param_values)
res.plot_fit()
plt.plot(x, y, label='true')
plt.legend()
plt.show()

lmfit.Model takes a model function that is a Python function. lmfit.Model takes a model function that is a Python function. It parses the function arguments and expects those to be the Parameters for the model.它解析 function arguments 并期望这些是 model 的参数。

I don't think using sympy-created functions will do that.我不认为使用 sympy 创建的函数会做到这一点。 Do you need to use sympy here?你需要在这里使用 sympy 吗? I don't see why.我不明白为什么。 The usage here seems designed to make the code more complex than it needs to be.这里的用法似乎旨在使代码比需要的更复杂。 It seems you want to make a model with a Gaussian-like peak, and a constant(?) background.看来您想制作一个 model 具有类似高斯的峰值和恒定(?)背景。 If so, why not do如果是这样,为什么不这样做

from lmfit.Models import GaussianModel, ConstantModel

model = GaussianModel(prefix='p1_') + ConstantModel()
params = model.make_params(p1_amplitude=2, p1_center=2, p1_sigma=1, c=0)

That just seems way easier to me, and it is very easy to add a second Gaussian peak to that model.这对我来说似乎更容易,并且很容易向 model 添加第二个高斯峰。

But even if you have your own preferred mathematical expression, don't use that as a sympy string, use it as Python code:但即使您有自己喜欢的数学表达式,也不要将其用作 sympy 字符串,将其用作 Python 代码:

def myfunction(x, A1, xc1, sigma1):
    return A1*exp(-(x-xc1)**2/(2*sigma1**2))

and then接着

from lmfit import Model
mymodel = Model(myfunction)
params = mymodel.guess(A1=2, xc1=2, sigma1=1)

In short: sympy is an amazing tool, but lmfit does not use it.简而言之:sympy 是一个了不起的工具,但 lmfit 不使用它。

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

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