首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用自回归来绘制和可视化时间序列未来值的预测?

如何使用自回归来绘制和可视化时间序列未来值的预测?
EN

Stack Overflow用户
提问于 2019-02-13 18:22:26
回答 1查看 49关注 0票数 0

我想画出时间序列和对其未来价值的预测。我使用的是自回归模型。我不确定我使用的策略是否正确。但我想在未来估计15个时间戳的时间序列数据。我希望在不同的可能性之间有一个填充:

代码语言:javascript
复制
import numpy as np
from numpy import convolve
import matplotlib.pyplot as plt
plt.style.use('ggplot')

def moving_average(y, period):
    buffer = []
    for i in range(period, len(y)):
        buffer.append(y[i - period : i].mean())
    return buffer

def auto_regressive(y, p, d, q, future_count):
    """
    p = the order (number of time lags)
    d = degree of differencing
    q = the order of the moving-average
    """
    buffer = np.copy(y).tolist()
    for i in range(future_count):
        ma = moving_average(np.array(buffer[-p:]), q)
        forecast = buffer[-1]
        for n in range(0, len(ma), d):
            forecast -= buffer[-1 - n] - ma[n]
        buffer.append(forecast)
    return buffer



y=[60, 2, 0, 0, 1, 1, 0, -1, -2, 0, -2, 6, 0, 2, 0, 4, 0, 1, 3, 2, 1, 2, 1, 0, 2, 2, 0, 1, 0, 1, 3, -1, 0, 2, 2, 1, 3, 2, 4, 2, 3, 0, 0, 2, 2, 0, 3, 1, 0, 2]
x=[1549984749, 1549984751, 1549984755, 1549984761, 1549984768, 1549984769, 1549984770, 1549984774, 1549984780, 1549984783, 1549984786, 1549984787, 1549984788,
   1549984794, 1549984797, 1549984855, 1549984923, 1549984930, 1549984955, 1549985006, 1549985008, 1549985027, 1549985086, 1549985091, 1549985101, 1549985115,
   1549985116, 1549985118, 1549985130, 1549985130, 1549985139, 1549985141, 1549985146, 1549985154, 1549985178, 1549985192, 1549985203, 1549985217, 1549985245,
   1549985288, 1549985311, 1549985316, 1549985425, 1549985447, 1549985460, 1549985463, 1549985489, 1549985561, 1549985595, 1549985610]
x=np.array(x)
print(np.size(x))
y=np.array(y)
print(np.size(y))

future_count = 15
predicted_15 = auto_regressive(y,20,1,2,future_count)

plt.plot(x[len(x) - len(predicted_15):], predicted_15)
plt.plot(x, y, 'o-')
plt.show()

但是我得到了这个错误:

代码语言:javascript
复制
    "have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (15,) and (65,)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-13 18:37:07

您将收到一个错误,因为predicted_15包含y以及您的预测值(因此y的长度为65)。您只想绘制预测值(长度为15)。

代码语言:javascript
复制
plt.plot(x[len(x) - len(predicted_15):], predicted_15[len(x):])

话虽如此,您需要考虑这些预测的y值对应的x值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54667784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档