【发布时间】:2021-08-16 15:09:56
【问题描述】:
我有一个示例时间序列数据框:
df = pd.DataFrame({'year':'1990','1991','1992','1993','1994','1995','1996',
'1997','1998','1999','2000'],
'count':[96,184,148,154,160,149,124,274,322,301,300]})
我想要在regression line 中带有confidence interval 乐队的linear regression 行。虽然我设法绘制了一条线性回归线。我发现很难在图中绘制置信区间带。这是我的线性回归图代码的 sn-p:
from matplotlib import ticker
from sklearn.linear_model import LinearRegression
X = df.date_ordinal.values.reshape(-1,1)
y = df['count'].values.reshape(-1, 1)
reg = LinearRegression()
reg.fit(X, y)
predictions = reg.predict(X.reshape(-1, 1))
fig, ax = plt.subplots()
plt.scatter(X, y, color ='blue',alpha=0.5)
plt.plot(X, predictions,alpha=0.5, color = 'black',label = r'$N$'+ '= {:.2f}t + {:.2e}\n'.format(reg.coef_[0][0],reg.intercept_[0]))
plt.ylabel('count($N$)');
plt.xlabel(r'Year(t)');
plt.legend()
formatter = ticker.ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
formatter.set_powerlimits((-1,1))
ax.yaxis.set_major_formatter(formatter)
plt.xticks(ticks = df.date_ordinal[::5], labels = df.index.year[::5])
plt.grid()
plt.show()
plt.clf()
这给了我一个很好的时间序列线性回归图。
问题和期望的输出
但是,regression line 也需要confidence interval,如:.
非常感谢您对此问题的帮助。
【问题讨论】:
标签: python matplotlib scikit-learn time-series linear-regression