I am trying to evaluate an ARIMA model. To evaluate the model, I want to use 5 days into the future as test data.
forecast_data = model_fit.forecast(steps=5)
# Last 5 days of forecast data
pred_arima_y = forecast_data[0].tolist()
# 5 days of actual data
test_y = appl_test_df.price.values
# Minimum forecast data for the last 5 days
pred_y_lower = []
# Maximum forecast data for the last 5 days
pred_y_upper = []
for lower_upper in forecast_data[2]:
lower = lower_upper[0]
upper = lower_upper[1]
pred_y_lower.append(lower)
pred_y_upper.append(upper)
And I get the following error
TypeError Traceback (most recent call last)
<ipython-input-44-309f07141e72> in <cell line: 16>()
14 pred_y_upper = []
15
---> 16 for sub_array in forecast_data[2]:
17 for lower_upper in sub_array:
18 lower = lower_upper[0]
TypeError: 'numpy.float64' object is not iterable
To fix the code, I tried using a nested loop to iterate over the entire 2D array forecast_data[2], or tried other methods, but they all failed.
I added some more code to the previous one. The preceding partial code looks like this. ARIMA was not available, so we switched to SARIMAX halfway through.
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.tsa.statespace.sarimax import SARIMAX
import statsmodels.api as sm
# model = ARIMA(appl_train_df.price.values, order = (2,1,2))
# model_fit = model.fit(trend = 'c', full_output = True, disp = True)
model = SARIMAX(endog = appl_train_df.price.values, order = (2, 1, 2), seasonal_order = (1, 1, 1, 12))
model_fit = model.fit(trend = 'c', full_output = True, disp = True)
# model_fit = model.fit(disp=0)
print(model_fit.summary())
SARIMAX Results
============================================================================================
Dep. Variable: y No. Observations: 515
Model: SARIMAX(2, 1, 2)x(1, 1, [1], 12) Log Likelihood -3919.268
Date: Tue, 02 Apr 2024 AIC 7852.536
Time: 01:36:55 BIC 7882.066
Sample: 0 HQIC 7864.122
- 515
Covariance Type: opg
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
ar.L1 1.8483 0.012 154.720 0.000 1.825 1.872
ar.L2 -0.9506 0.011 -82.778 0.000 -0.973 -0.928
ma.L1 -1.8954 0.010 -187.159 0.000 -1.915 -1.876
ma.L2 0.9839 0.010 98.924 0.000 0.964 1.003
ar.S.L12 0.1830 0.018 10.002 0.000 0.147 0.219
ma.S.L12 -0.9623 0.032 -30.038 0.000 -1.025 -0.900
sigma2 2.977e+05 1.44e+04 20.616 0.000 2.69e+05 3.26e+05
===================================================================================
Ljung-Box (L1) (Q): 1.85 Jarque-Bera (JB): 665.47
Prob(Q): 0.17 Prob(JB): 0.00
Heteroskedasticity (H): 0.35 Skew: 0.05
Prob(H) (two-sided): 0.00 Kurtosis: 8.64
===================================================================================
And I checked the value with
print(forecast_data[2].shape)
, but the result is
( )
, It is very strange.
forecast_data.shape, and add it to the question?forecast_data[2]is ONE number, and thusforecast_datais a 1d array. Did you examineforecast_data? Itsshape,dtypeetc. Review the docs formodel_fit.forecastto make sure you understand what it's supposed to produce?numpy.float64object will have()shape, like a 0d single item array.