You have 2 free member-only stories left this month.

Forecasting with Auto ARIMA in Python

ARIMA is a popular and powerful statistical time series forecasting method.

Photo by Jack B on Unsplash

Code Example

Library Imports

# Data Library
import numpy as np
# ARIMA library
import pmdarima as pm
# Visualization Library
import matplotlib.pyplot as plt

Load Dataset

# Loading some sample data using the pmdarima library
training_data = pm.datasets.load_wineind()
# Visualize the sample data
time = np.arange(training_data.shape[0])
plt.plot(time, training_data, c = 'black')
# Set axis labels
plt.xlabel('Time')
plt.ylabel('Value')
plt.show()
Fig. 1 (Plot of sample data)

Create an ARIMA model

# Fit the ARIMA model with auto generated parameters and use the seasonal component.
model = pm.auto_arima(training_data, seasonal=True, m=12)
# Set number of points to forecast as h
h = 50
# Generate a forecast
forecast = model.predict(h)

Visualize Forecast

# Visualize forecast with training data
time = np.arange(training_data.shape[0] + h)
# Slice the x-axis to only plot the training data as the color black
plt.plot(time[:training_data.shape[0]], training_data, c = 'black')
# Slice the x-axis to plot our forecast after the training data as the color red
plt.plot(time[training_data.shape[0]:], forecast, c = 'red')
# Set axis labels
plt.xlabel('Time')
plt.ylabel('Value')
plt.show()
Fig. 2 (Plot of training data with forecast)

Summary

Bio

Links

Data Scientist for Mercedes-Benz Research & Development. Member of the Matrix Profile Foundation. I write about tech and artificial intelligence.

Share your ideas with millions of readers.

Love podcasts or audiobooks? Learn on the go with our new app.