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.6971736 , 25983.70921864, 30225.30980326, 35417.124979  ,
       13011.36189291, 19639.41029709, 21506.58177915, 23674.34000393,
       21685.76396166, 23669.77821406, 26955.35258175, 22755.25063648,
       19808.16135114, 23578.78188153, 27845.86723055, 32923.89429994,
       10475.68781103, 17024.74537358, 18831.64801043, 20929.54675489,
       18876.02418493, 20792.57517292, 24011.9755139 , 19745.03910977,
       16731.45368225, 20435.40476246, 24635.90944103, 29647.31036116,
        7132.50102023, 13614.94379639, 15355.2377604 , 17386.52470027,
       15266.39193001, 17116.33189588, 20269.12163572, 15935.5744148 ,
       12855.37828096, 16492.71859826, 20626.61254291, 25571.40271427,
        2989.98263217,  9405.81466327, 11079.49788422, 13044.17408   ,
       10857.43056618, 12640.75978821, 15726.93878436, 11326.78081968,
        8179.97394211, 11750.70351566, 15817.98671658])

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 1.823 seconds)

Gallery generated by Sphinx-Gallery