【发布时间】:2017-06-06 21:25:55
【问题描述】:
您好,我现在正在学习线性回归。我想从我制作的数据中绘制线性回归图。
如果有如下所示的数据,
one_cycle = [(0, 401.92), (5, 103.62), (7, 62.8), (8, 28.26), (10, 10.55)]
我使用了statsmodels.api.OLS,得到了回归结果。
def basic_regression(one_cycle):
#one_cycle would be [(0,100),(1,75)...]
X, Y = [x[0] for x in one_cycle], [x[1] for x in one_cycle]
X = numpy.array(X).T
X = statsmodels.api.add_constant(X)
results = statsmodels.api.OLS(Y, X).fit()
return results
当我绘制结果图表时,
def draw(results):
fig, ax = plt.subplots()
fig = statsmodels.api.graphics.plot_fit(results, 0, ax=ax)
ax.ticklabel_format(useOffset=False)
plt.show()
该图表不是我所期望的,如下所示。
我期待的是这个图表,而不是那个图表:
(来源:sourceforge.net)
我怎么能像那样画 grah? 谢谢你有一个美好的一天。
【问题讨论】:
-
我认为你想要的是相对于 X 的第二列中的变量而不是第一列中的常量进行绘图。即使用 1 作为 x_var 索引
statsmodels.api.graphics.plot_fit(results, 1, ax=ax)
标签: python matplotlib linear-regression statsmodels