簡體   English   中英

使用 statsmodels.tsa.arima.model import ARIMA 進行時間序列預測

[英]Time Series Prediction with statsmodels.tsa.arima.model import ARIMA

我正在嘗試對我的 ARIMA Model 進行預測,但我停留在一點

from statsmodels.tsa.arima.model import ARIMA
train2 = trainData1["meantemp"][:1170]
test2 = trainData1["meantemp"][1170:]
# p,d,q ARIMA Model
model = ARIMA(train2, order=(1,1,50))
model_fit = model.fit()
print(model_fit.summary())

在這里,trainData1 以日期作為索引(如您猜測的那樣包括 train2 和 test2)並使用 train2 數據訓練 model,之后我嘗試對 test2 數據進行如下預測;

# make predictions
predictions = model_fit.predict(test2)
rmse = mean_squared_error(test2.values, predictions)
rmse

但它給了我以下錯誤;

TypeError: Cannot convert input [date
2016-03-17    2.375000
2016-03-18   -0.125000
2016-03-19    0.598214
2016-03-20    0.347619
2016-03-21   -0.508333
                ...   
2016-12-28    0.367391
2016-12-29   -1.979296
2016-12-30   -1.142857
2016-12-31    0.957393
2017-01-01   -5.052632
Name: meantemp, Length: 291, dtype: float64] of type <class 'pandas.core.series.Series'> to Timestamp

預測 function 的內部應該添加什么作為數據?

訓練2如下;

2013-01-02   -2.600000
2013-01-03   -0.233333
2013-01-04    1.500000
2013-01-05   -2.666667
2013-01-06    1.000000
                ...   
2016-03-12   -0.504167
2016-03-13   -0.312500
2016-03-14   -1.875000
2016-03-15    1.691667
2016-03-16   -0.129167
Name: meantemp, Length: 1170, dtype: float64

測試2如下;

date
2016-03-17    2.375000
2016-03-18   -0.125000
2016-03-19    0.598214
2016-03-20    0.347619
2016-03-21   -0.508333
                ...   
2016-12-28    0.367391
2016-12-29   -1.979296
2016-12-30   -1.142857
2016-12-31    0.957393
2017-01-01   -5.052632
Name: meantemp, Length: 291, dtype: float64

我按如下方式解決了自己的問題;

# make predictions
predictions = model_fit.forecast(291)
print(f'ARIMA Model Test Data MSE: {np.mean((predictions.values - test2.values)**2):.3f}')

預測 function,model 預測接下來的 291 步,這里是一天,因為它是每日時間序列,並使用預測和實際測試值使用 MSE 指標進行評估。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM