我正在尝试使用 lmfit 拟合曲线,但我正在使用的数据集不包含很多点,这使得生成的拟合看起来是锯齿状的而不是弯曲的。
我只是使用这条线:
out = mod.fit(SV, pars, x=VR)
VR 和 SV 是我要拟合的点的坐标。
我试过使用 scipy.interpolate.UnivariateSpline 并拟合得到的数据,但我想知道是否有内置或更快的方法来做到这一点。
谢谢
我正在尝试使用 lmfit 拟合曲线,但我正在使用的数据集不包含很多点,这使得生成的拟合看起来是锯齿状的而不是弯曲的。
我只是使用这条线:
out = mod.fit(SV, pars, x=VR)
VR 和 SV 是我要拟合的点的坐标。
我试过使用 scipy.interpolate.UnivariateSpline 并拟合得到的数据,但我想知道是否有内置或更快的方法来做到这一点。
谢谢
虽然您可能可以使用scipy.interpolate.UnivariateSpline完成这项工作,但您基本上会适合您已经完成的工作。
相反,您可以使用已从您的原始装配中提供给您的组件。一旦您知道如何操作,这将非常简单,但 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()
没有使用 lmfit 自动插值的内置方法。使用 lmfit 模型,您可以提供应评估模型的独立值的数组,以及与该模型进行比较的数据数组。
您可以自由地对数据进行插值或平滑处理或执行一些其他转换(我有时对数据和模型进行傅立叶变换以强调某些频率),但您必须将其作为模型的一部分。