Autoregressive Integrated Moving Average (ARIMA) is a powerful statistical model for time series forecasting. If you want to learn about using ARIMA and practically choosing the right values for the parameters, this article is for you. In this article, I’ll take you through a step-by-step practical guide to ARIMA for time series forecasting.
Practical Guide to ARIMA for Time Series Forecasting
This practical guide will take you through the entire process of using ARIMA, from data preprocessing to model training and forecasting future values.
Step 1: Understanding and Preparing the Dataset
The dataset we are using contains the following:
- Time Period: Dates from April 2013 to the latest available.
- Subscribers: Number of Netflix subscribers for each time period.
Let’s import the data and prepare it for analysis:
import pandas as pd
# load the dataset
df = pd.read_csv("Netflix-Subscriptions.csv")
df['Time Period'] = pd.to_datetime(df['Time Period'], format='%d/%m/%Y')
df.set_index('Time Period', inplace=True)
df = df.sort_index()
df.head()
Here, we converted the Time Period column into a datetime format (‘%d/%m/%Y’), set it as the index, and sorted the dataset in chronological order to ensure proper time series analysis.
Step 2: Visualizing the Time Series
The next step is to Plot the data to observe trends, seasonality, or stationarity:
import plotly.express as px
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.index, y=df["Subscribers"], mode='lines', name='Subscribers'))
fig.update_layout(
title="Netflix Subscribers Over Time",
xaxis_title="Year",
yaxis_title="Subscribers",
template="plotly_white",
width=900,
height=500
)
fig.show()
This graph shows an upward trend in Netflix subscriptions over time, indicating consistent growth in subscriber numbers from 2014 to 2023. The curve appears relatively smooth, with no visible sharp seasonal fluctuations, which suggests minimal seasonality in subscription growth.
Let’s check the stationary:
from statsmodels.tsa.stattools import adfuller
def check_stationarity(timeseries):
# compute rolling statistics
roll_mean = timeseries.rolling(window=12).mean()
roll_std = timeseries.rolling(window=12).std()
# create the figure
fig = go.Figure()
# add original time series
fig.add_trace(go.Scatter(x=timeseries.index, y=timeseries,
mode='lines', name='Original',
line=dict(color='blue')))
# add rolling mean
fig.add_trace(go.Scatter(x=roll_mean.index, y=roll_mean,
mode='lines', name='Rolling Mean',
line=dict(color='red', dash='dash')))
# add rolling std deviation
fig.add_trace(go.Scatter(x=roll_std.index, y=roll_std,
mode='lines', name='Rolling Std',
line=dict(color='green', dash='dot')))
fig.update_layout(
title="Stationarity Check - Rolling Mean & Std",
xaxis_title="Time",
yaxis_title="Value",
template="plotly_white",
width=900,
height=500
)
fig.show()
check_stationarity(df['Subscribers'])
The upward trend in the rolling mean (red dashed line) and the non-constant rolling standard deviation (green dotted line) indicate that the data is non-stationary. For a series to be stationary, its mean and variance should remain constant over time, which is not the case here due to the increasing trend and variability.
When the data is not stationary, we apply differencing (first or second order) until stationarity is achieved. Here’s how:
df['Subscribers_diff'] = df['Subscribers'].diff().dropna()
Step 3: Identifying ARIMA Parameters (p, d, q)
ARIMA models are represented as ARIMA(p, d, q), where:
- p (Auto-Regressive Order): Number of lag observations included.
- d (Differencing Order): Number of times differencing is applied.
- q (Moving Average Order): Size of the moving average window.
We applied differencing to make data stationary only once, so the value of d will be 1. And, here’s how to find the value of p and q using the autocorrelation and partial autocorrelation function plots:
from statsmodels.tsa.stattools import acf, pacf
def plot_acf_pacf(series, lags=20):
series = series.dropna()
acf_values = acf(series, nlags=lags)
pacf_values = pacf(series, nlags=lags)
fig_acf = go.Figure()
fig_acf.add_trace(go.Bar(x=list(range(len(acf_values))), y=acf_values, name='ACF'))
fig_acf.update_layout(title="Autocorrelation Function (ACF)", xaxis_title="Lags", yaxis_title="ACF Value", template="plotly_white")
fig_pacf = go.Figure()
fig_pacf.add_trace(go.Bar(x=list(range(len(pacf_values))), y=pacf_values, name='PACF'))
fig_pacf.update_layout(title="Partial Autocorrelation Function (PACF)", xaxis_title="Lags", yaxis_title="PACF Value", template="plotly_white")
fig_acf.show()
fig_pacf.show()
plot_acf_pacf(df['Subscribers_diff'], lags=20)

From the ACF plot, the significant autocorrelation at lag 1 suggests that q = 1 for the MA component. From the PACF plot, the significant partial autocorrelation at lag 1 and a sharp cutoff afterwards suggests that p = 1 for the AR component.
These values indicate that an ARIMA(1, 1, 1) model might be suitable for the data, with d depending on stationarity tests.
Step 4: Training the ARIMA Model
Now, we will use the statsmodels library to fit the ARIMA model. Here’s how to do that:
from statsmodels.tsa.arima.model import ARIMA # fit ARIMA model model = ARIMA(df['Subscribers'], order=(1,1,1)) model_fit = model.fit() print(model_fit.summary())
Here’s how to look at the forecasted values:
def plot_forecast(df, forecast, model_fit, steps=12, freq='Q'):
# generate future dates
future_dates = pd.date_range(start=df.index[-1], periods=steps + 1, freq=freq)[1:]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df.index, y=df['Subscribers'],
mode='lines', name='Actual',
line=dict(color='blue')
))
fig.add_trace(go.Scatter(
x=future_dates, y=forecast,
mode='lines', name='Forecast',
line=dict(color='red', dash='dash')
))
fig.update_layout(
title="Netflix Subscribers Forecast",
xaxis_title="Time",
yaxis_title="Subscribers",
template="plotly_white",
width=900,
height=500
)
fig.show()
forecast = model_fit.forecast(steps=12)
plot_forecast(df, forecast, model_fit, steps=12, freq='Q')
So, this is how to use the ARIMA model for time series forecasting. Here are some projects you should try to learn more about time series forecasting:
- Analyzing & Forecasting Rainfall Trends
- Website Traffic Analysis & Forecasting
- Ads CTR Forecasting
- Multivariate Time Series Forecasting
- Demand Forecasting & Inventory Optimization
Summary
So, ARIMA is a robust and widely used model for time series forecasting, particularly when the data exhibits trends or patterns without strong seasonality. This practical guide walked through the entire process, from understanding and preparing the dataset to visualizing trends, achieving stationarity, identifying parameters, and training the ARIMA model for accurate forecasting.
I hope you liked this article on a practical guide to ARIMA for time series forecasting. Feel free to ask valuable questions in the comments section below. You can follow me on Instagram for many more resources.





