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.


Predictions: array([21966.69715327, 25983.70919042, 30225.30977757, 35417.12495208,
       13011.3618736 , 19639.41026476, 21506.58175114, 23674.33997107,
       21685.76393112, 23669.77817456, 26955.35254916, 22755.25060906,
       19808.16130991, 23578.78183011, 27845.86718064, 32923.89424952,
       10475.68776683, 17024.74531668, 18831.64795696, 20929.54669646,
       18876.02412824, 20792.57510697, 24011.97545439, 19745.03905506,
       16731.45361331, 20435.40468293, 24635.9093626 , 29647.31028183,
        7132.5009467 , 13614.94370975, 15355.2376768 , 17386.52461129,
       15266.39184237, 17116.33179857, 20269.12154445, 15935.57432792,
       12855.37817945, 16492.71848576, 20626.6124311 , 25571.40260115,
        2989.98252446,  9405.81454204, 11079.49776562, 13044.17395562,
       10857.43044273, 12640.75965469, 15726.93865647, 11326.78069577,
        8179.97380317, 11750.70336532, 15817.98656653])

print(__doc__)

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

import pmdarima as pm
from pmdarima import model_selection
import joblib  # for persistence
import os

# #############################################################################
# Load the data and split it into separate pieces
y = pm.datasets.load_wineind()
train, test = model_selection.train_test_split(y, train_size=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 2.234 seconds)

Gallery generated by Sphinx-Gallery