在更多数据点评估 lm 拟合结果
我使用 lm 拟合将函数(高斯曲线)拟合到一些数据点。
# prepare data
data = pd.read_csv("data.csv")
X = np.array(data.iloc[:,0])
Y = np.array(data.iloc[:,1])
def Gauss(x,y0,a,mu,sigma):
return y0+a/(sigma*np.sqrt(2*np.pi))*np.exp(-(x-mu)**2/(2*sigma**2))
gmodel = Model(Gauss)
result = gmodel.fit(Y, x=X, y0=0, a=1, mu=1, sigma=1)
conf = result.eval_uncertainty(sigma=1)
print(result.fit_report())
到目前为止,一切都很好。现在我想绘制数据和模型
x = np.linspace(min(X), max(X))
plt.figure(
num=1,
figsize=(10,6),
facecolor='white')
plt.plot(X, Y, 'bo', label='Data')
# This does not work
plt.plot(x, result.init_fit, 'k--', label='Initial guess')
plt.plot(x, result.best_fit, 'r-', label='Best fit')
plt.fill_between(x, result.best_fit-conf, result.best_fit+conf, color='black', label='Confidence band')
plt.legend(loc=2)
但这不起作用,因为 result.init_fit
和 result.best_fit
以及最重要的 conf
仅评估我已拟合的数据点的结果,而不是 x
中的所有数据点。那么我如何评估 x 中所有数据点的这些值呢?
I fit a function (gauss curve) to some datapoints using lm fit.
# prepare data
data = pd.read_csv("data.csv")
X = np.array(data.iloc[:,0])
Y = np.array(data.iloc[:,1])
def Gauss(x,y0,a,mu,sigma):
return y0+a/(sigma*np.sqrt(2*np.pi))*np.exp(-(x-mu)**2/(2*sigma**2))
gmodel = Model(Gauss)
result = gmodel.fit(Y, x=X, y0=0, a=1, mu=1, sigma=1)
conf = result.eval_uncertainty(sigma=1)
print(result.fit_report())
So far so good. Now I would like to plot the data and the model
x = np.linspace(min(X), max(X))
plt.figure(
num=1,
figsize=(10,6),
facecolor='white')
plt.plot(X, Y, 'bo', label='Data')
# This does not work
plt.plot(x, result.init_fit, 'k--', label='Initial guess')
plt.plot(x, result.best_fit, 'r-', label='Best fit')
plt.fill_between(x, result.best_fit-conf, result.best_fit+conf, color='black', label='Confidence band')
plt.legend(loc=2)
However this does not work, since result.init_fit
and result.best_fit
and most importantly conf
are evaluating the results only at the datapoints I have fitted them to, not all datapoints in x
. So how do I evaluate these for all datapoints in x
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用新的
x
值重新评估您的模型。要评估结果,不使用最佳拟合参数(上面将默认执行此操作),而是使用初始参数,使用
You can just re-evaluate your model with the new
x
values withTo evaluate the result not using the best-fit parameters (which the above will do by default), but with the initial parameters, use