Skip to main content
 首页 » 编程设计

python之LMFIT: ' ValueError: The input contains nan values' 当数据输入全是 float

2024年11月24日9三少

我正在尝试对我从 Integration 获得的数据集进行曲线拟合,lmfit 说它包含 NaN 值。

同样的数据用 scipy_curvefit 拟合,那是 super 糟糕的。我正在试用这个库以获得更好的结果。

我试图将我的 yData 和 xData 更改为一些简单的数组(您将在下面看到)并得到了同样的错误!

from lmfit import Model 
def poly(x, a1,a2,a3,a4,a5): 
    return a1+a2*x**a3+a4*x**a5 
x=s 
y=vrr 
x=[0.,1.,2.,3.,5.,6.] 
y=[4.,5.,6.,12.,3.,5.] 
gmodel = Model(poly) 
gmodel_parameters= gmodel.make_params() 
gmodel_parameters['a1'].set(value=10) 
gmodel_parameters['a2'].set(value=10) 
gmodel_parameters['a3'].set(value=7) 
gmodel_parameters['a4'].set(value=10) 
gmodel_parameters['a5'].set(value=7) 
result=gmodel.fit(x=x,data=y,params=gmodel_parameters) 
 
plt.plot(x, y, 'k--') 
plt.plot(x, result.best_fit, 'r-') 
plt.show() 

请您参考如下方法:

拟合指数时要小心。如果程序将此变量设置为小于零,则可能会出现 NaN 错误。阅读文档并尝试将 min=0 添加到 a3 和 a5 作为优化选项。

https://lmfit.github.io/lmfit-py/model.html#the-model-class

祝你好运!