Preparing a time series for supervised learning
In this recipe, we turn our attention to machine learning approaches to forecasting. We start by describing the process of transforming a time series from a sequence of values into a format suitable for supervised learning.
Getting ready
Supervised learning involves a dataset with explanatory variables (input) and a target variable (output). A time series comprises a sequence of values with an associated timestamp. Therefore, we need to restructure the time series for supervised learning. A common approach to do this is using a sliding window. Each value of the series is based on the recent past values before it (also called lags).
To prepare for this section, you need to have your time series data available in a pandas
DataFrame and have the pandas
and NumPy libraries installed. If not, you can install them using pip
:
pip install -U pandas numpy
We also load the univariate time series into the Python session:
series...