Search code examples
pythontime-seriesstatsmodelsforecastingarima

Having issues printing results from SARIMAX model


I have put together a SARIMA model to predict inventory needs for a specific product. I am able to get the time series visualized as an output of the results perfectly, but when trying to directly print the results directly (actual numbers, not graph) the code runs but does not provide a print of the values.

I have tried researching this but all I can seem to find online is examples of visualization of the time-series, which I already have. I have tried both .get_prediction and get_forecast.

my_order = (2, 1, 0)
my_seasonal_order = (3, 1, 0, 7)
bananas_model = SARIMAX(bananas_mfc_log, order=my_order, seasonal_order=my_seasonal_order, alpha=0.05, freq='D')
results = bananas_model.fit()

pred = results.get_prediction(start=pd.to_datetime('2019-07-01'), dynamic=False)
pred_ci = pred.conf_int()
ax = bananas_mfc_log.apply(np.exp).plot(label='observed')
pred.predicted_mean.apply(np.exp).plot(ax=ax, label='forecasted', alpha=.7, figsize=(14, 7))
ax.set_xlabel('Date')
ax.set_ylabel('Quantity Picked')
plt.ylim(-1000, 1500)
plt.legend()
plt.show()
pred2 = results.get_forecast(steps=15)
print(pred2)

The attempt to print above provides this:

<statsmodels.tsa.statespace.mlemodel.PredictionResultsWrapper object at 0x000002100EC02D30>
pred2 = results.get_prediction(start=pd.to_datetime('2019-07-26'), end=pd.to_datetime('2019-08-09'), dynamic=False)
print(pred2)

The second attempt to print results results in this:

<statsmodels.tsa.statespace.mlemodel.PredictionResultsWrapper object at 0x000002100059D710>

I would like to print a dataframe or an array of the forecast results. For example:

date         bananas
2019-07-26   57
2019-07-27   178
2019-07-28   198
2019-07-29   72
2019-07-30   54
2019-07-31   87

Solution

  • While waiting for a response I tried the following, which was successful:

    pred2 = []
    pred2 = results.get_prediction(start=pd.to_datetime('2019-07-01'), end=pd.to_datetime('2019-07-31'), dynamic=False)
    pred2 = pred2.predicted_mean.apply(np.exp)
    print(pred2)
    

    The apply(np.exp) was only necessary to backtransform from a log transformation completed for the model. This is a preview of the format of the results:

    2019-07-26    215.606736
    2019-07-27    353.112927
    2019-07-28     72.164655
    2019-07-29    318.258192
    2019-07-30     10.354032
    2019-07-31    301.233365
    Freq: D, dtype: float64
    

    Even though this post was short lived, I will leave it on here just in case it helps others who are also learning.