我试图用lmfit拟合一条曲线,但我正在处理的数据集不包含很多点,这使得最终的拟合看起来是锯齿状的,而不是曲线。
我只是简单地使用这句话:
out =mod.fit(服务,解析,x=VR)
我们的VR和SV是我想要拟合的点的坐标。
我试过使用scipy.interpolate.UnivariateSpline和拟合结果数据,但我想知道是否有内置的或更快的方法来做这件事。
谢谢
发布于 2017-03-11 11:06:02
没有一种内置的方法可以使用lmfit自动插值。对于lmfit模型,您需要提供一个独立值的数组来评估模型,并提供一个要与该模型进行比较的数据数组。
您可以自由地对数据进行插值或平滑处理,或者执行其他一些转换(我有时会对数据进行傅立叶变换并建立模型,以强调某些频率),但您必须将其作为模型的一部分。
发布于 2021-09-22 16:43:57
虽然您可能能够使用scipy.interpolate.UnivariateSpline完成这项工作,但您基本上可以适应您已经做过的工作。
取而代之的是,你可以使用你最初的fit中已经提供给你的组件。一旦你知道了怎么做,这是非常微不足道的,但是lmfit文档并没有提供一个清晰的案例。
import numpy as np
from lmfit.models import GaussianModel
import matplotlib.pyplot as plt
y, _ = np.histogram(np.random.normal(size=1000), bins=10, density=True)
x = np.linspace(0, 1, y.size)
# Replace with whatever model you are using (with the caveat that the above dataset is gaussian).
model = GaussianModel()
params = model.guess(y, x=x)
result = model.fit(y, params, x=x)
x_interp = np.linspace(0, 1, 100*y.size)
# The model is attached to the result, which makes it easier if you're sending it somewhere.
y_interp = result.model.func(x_interp, **result.best_values)
plt.plot(x, y, label='original')
plt.plot(x_interp, y_interp, label='interpolated')
plt.legend()
plt.show()https://stackoverflow.com/questions/42711002
复制相似问题