lmfit模型拟合然后预测
lmfit model fitting and then prediction
我正在采用 lmfit
进行曲线拟合并使用该拟合模型进行预测。但是,下面的代码并没有达到我想要的效果。能否请你帮忙?谢谢
import numpy as np
from lmfit import Model
def linearModel(x, a0, a1):
return a0+a1*x
#main code begin here
X=[1,2,4] # data for fitting
y=[2,4,6] # data for fitting
gmodel = Model(linearModel) #select model
params = gmodel.make_params(a0=1, a1=1) # initial params
result = gmodel.fit(y, params, x=X) # curve fitting
x1=[1, 2, 3] # input for prediction
a=result.eval(x) # prediction
包含您实际 运行 的代码、您得到的结果以及您期望的结果总是一个好主意。
这里主要是你的语法错误。其次,你应该使用 numpy 数组,而不是列表。第三,如文档所示(参见 https://lmfit.github.io/lmfit-py/model.html#lmfit.model.ModelResult.eval)result.eval()
会将 params
作为第一个参数,而不是自变量。简而言之,您想将最后两行替换为
x1 = np.array([1, 2, 3]) # input for prediction
a = result.eval(x=x1) # prediction
这应该会按预期工作。
而且:当然,您不需要 lmfit
进行线性回归。 ;).
我正在采用 lmfit
进行曲线拟合并使用该拟合模型进行预测。但是,下面的代码并没有达到我想要的效果。能否请你帮忙?谢谢
import numpy as np
from lmfit import Model
def linearModel(x, a0, a1):
return a0+a1*x
#main code begin here
X=[1,2,4] # data for fitting
y=[2,4,6] # data for fitting
gmodel = Model(linearModel) #select model
params = gmodel.make_params(a0=1, a1=1) # initial params
result = gmodel.fit(y, params, x=X) # curve fitting
x1=[1, 2, 3] # input for prediction
a=result.eval(x) # prediction
包含您实际 运行 的代码、您得到的结果以及您期望的结果总是一个好主意。
这里主要是你的语法错误。其次,你应该使用 numpy 数组,而不是列表。第三,如文档所示(参见 https://lmfit.github.io/lmfit-py/model.html#lmfit.model.ModelResult.eval)result.eval()
会将 params
作为第一个参数,而不是自变量。简而言之,您想将最后两行替换为
x1 = np.array([1, 2, 3]) # input for prediction
a = result.eval(x=x1) # prediction
这应该会按预期工作。
而且:当然,您不需要 lmfit
进行线性回归。 ;).