无法使用matplotlib绘制预测的时间序列值

Joe*_*y12 10 python datetime matplotlib statsmodels

我试图绘制我的实际时间序列值和预测值,但它给了我这个错误:

ValueError:视图限制最小值-36816.95989583333小于1并且是无效的Matplotlib日期值.如果将非日期时间值传递给具有日期时间单位的轴,则通常会发生这种情况

我正在使用statsmodels适合数据的arima模型.

这是我的数据样本:

datetime             value
2017-01-01 00:00:00  10.18
2017-01-01 00:15:00  10.2
2017-01-01 00:30:00  10.32
2017-01-01 00:45:00  10.16
2017-01-01 01:00:00  9.93
2017-01-01 01:15:00  9.77
2017-01-01 01:30:00  9.47
2017-01-01 01:45:00  9.08
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

mod = sm.tsa.statespace.SARIMAX(
    subset,
    order=(1, 1, 1),
    seasonal_order=(1, 1, 1, 12),
    enforce_stationarity=False,
    enforce_invertibility=False
)

results = mod.fit()
pred_uc = results.get_forecast(steps=500)
pred_ci = pred_uc.conf_int(alpha = 0.05)

# Plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(1, 1, 1)
ax.plot(subset,color = "blue")
ax.plot(pred_uc.predicted_mean, color="black", alpha=0.5, label='SARIMAX')
plt.show()
Run Code Online (Sandbox Code Playgroud)

知道如何解决这个问题吗?

小智 1

这应该是你如何提供数据的问题。

这些值必须是数据变量中datetime的索引,因此,以下内容有效。valuessubset

我在您提供的代码之前导入了数据,如下所示:

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import statsmodels.api as sm

subset = pd.Series(

[

    10.18, 10.2 , 10.32,
    10.16, 9.93, 9.77,
    9.47, 9.08

]

, index=pd.date_range(

    start='2017-01-01T00:00:00.000',

    end='2017-01-01T01:45:00.000',

    freq='15T'

)  )
Run Code Online (Sandbox Code Playgroud)

我相信,我得到了你想要的情节(已被剪辑):

绘图结果剪切

我在 Python 3 中使用了这些版本的库:

matplotlib。版本 “3.1.2”

麻木的。版本 “1.17.4”

熊猫。版本 “0.25.3”

统计模型。版本 “0.12.0”