Persisting an ARIMA modelΒΆ

This example demonstrates how we can persist an ARIMA model to disk after fitting it. It can then be loaded back up and used to generate forecasts.


Out:

Predictions: array([21966.69715352, 25983.70920565, 30225.30978848, 35417.12496267,
       13011.3618805 , 19639.41027742, 21506.58176159, 23674.33998321,
       21685.76394174, 23669.77818948, 26955.35255979, 22755.2506162 ,
       19808.16131764, 23578.7818493 , 27845.86719673, 32923.89426476,
       10475.6877784 , 17024.74533391, 18831.64797186, 20929.546713  ,
       18876.02414315, 20792.57512611, 24011.97546913, 19745.03906623,
       16731.45362497, 20435.40470597, 24635.90938245, 29647.31030073,
        7132.50096185, 13614.94373048, 15355.2376951 , 17386.52463115,
       15266.3918605 , 17116.33182084, 20269.12156224, 15935.57434204,
       12855.37819397, 16492.71851157, 20626.61245363, 25571.40262264,
        2989.9825421 ,  9405.81456517, 11079.49778624, 13044.1739777 ,
       10857.430463  , 12640.75967901, 15726.93867622, 11326.78071176,
        8179.97381947, 11750.70339282, 15817.98659065])

print(__doc__)

# Author: Taylor Smith <taylor.smith@alkaline-ml.com>

import pmdarima as pm
from pmdarima.datasets import load_wineind
from sklearn.externals import joblib  # for persistence
import os

# #############################################################################
# Load the data and split it into separate pieces
y = load_wineind()
train, test = y[:125], y[125:]

# Fit an ARIMA
arima = pm.ARIMA(order=(1, 1, 2), seasonal_order=(0, 1, 1, 12))
arima.fit(y)

# #############################################################################
# Persist a model and create predictions after re-loading it
pickle_tgt = "arima.pkl"
try:
    # Pickle it
    joblib.dump(arima, pickle_tgt, compress=3)

    # Load the model up, create predictions
    arima_loaded = joblib.load(pickle_tgt)
    preds = arima_loaded.predict(n_periods=test.shape[0])
    print("Predictions: %r" % preds)

finally:
    # Remove the pickle file at the end of this example
    try:
        os.unlink(pickle_tgt)
    except OSError:
        pass

Total running time of the script: ( 0 minutes 1.407 seconds)

Gallery generated by Sphinx-Gallery