Time Series Forecasting with PyCaret Regression
Last updated
Was this helpful?
Last updated
Was this helpful?
PyCaret is an open-source, low-code machine learning library and end-to-end model management tool built-in Python for automating machine learning workflows. It is incredibly popular for its ease of use, simplicity, and ability to build and deploy end-to-end ML prototypes quickly and efficiently.
PyCaret is an alternate low-code library that can be used to replace hundreds of lines of code with few lines only. This makes the experiment cycle exponentially fast and efficient.
PyCaret is simple and easy to use. All the operations performed in PyCaret are sequentially stored in a Pipeline that is fully automated for **deployment. **Whether it's imputing missing values, one-hot-encoding, transforming categorical data, feature engineering, or even hyperparameter tuning, PyCaret automates all of it. To learn more about PyCaret, watch this 1-minute video.
This tutorial assumes that you have some prior knowledge and experience with PyCaret. If you haven’t used it before, no problem — you can get a quick headstart through these tutorials:
Installing PyCaret is very easy and takes only a few minutes. We strongly recommend using a virtual environment to avoid potential conflicts with other libraries.
PyCaret Regression Module is a supervised machine learning module used for estimating the relationships between a dependent variable (often called the ‘outcome variable’, or ‘target’) and one or more independent variables (often called ‘features’, or ‘predictors’).
Time series forecasting can broadly be categorized into the following categories:
Classical / Statistical Models — Moving Averages, Exponential smoothing, ARIMA, SARIMA, TBATS
**Machine Learning **— Linear Regression, XGBoost, Random Forest, or any ML model with reduction methods
**Deep Learning **— RNN, LSTM
This tutorial is focused on the second category i.e. Machine Learning.
PyCaret’s Regression module default settings are not ideal for time series data because it involves few data preparatory steps that are not valid for ordered data (data with a sequence such as time series data).
For example, the split of the dataset into train and test set is done randomly with shuffling. This wouldn’t make sense for time series data as you don’t want the recent dates to be included in the training set whereas historical dates are part of the test set.
Time-series data also requires a different kind of cross-validation since it needs to respect the order of dates. PyCaret regression module by default uses k-fold random cross-validation when evaluating models. The default cross-validation setting is not suitable for time-series data.
The following section in this tutorial will demonstrate how you can change default settings in PyCaret Regression Module easily to make it work for time series data.
Since algorithms cannot directly deal with dates, let’s extract some simple features from dates such as month and year, and drop the original date column.
I have manually split the dataset before initializing the setup . An alternate would be to pass the entire dataset to PyCaret and let it handle the split, in which case you will have to pass data_split_shuffle = False in the setup function to avoid shuffling the dataset before the split.
Now it’s time to initialize the setup function, where we will explicitly pass the training data, test data, and cross-validation strategy using the fold_strategy parameter.
The best model based on cross-validated MAE is **Least Angle Regression **(MAE: 22.3). Let’s check the score on the test set.
MAE on the test set is 12% higher than the cross-validated MAE. Not so good, but we will work with it. Let’s plot the actual and predicted lines to visualize the fit.
The grey backdrop towards the end is the test period (i.e. 1960). Now let’s finalize the model i.e. train the best model i.e. Least Angle Regression on the entire dataset (this time, including the test set).
Now that we have trained our model on the entire dataset (1949 to 1960), let’s predict five years out in the future through 1964. To use our final model to generate future predictions, we first need to create a dataset consisting of the Month, Year, Series column on the future dates.
Now, let’s use the future_df to score and generate predictions.
Let’s plot it.
Wasn’t that easy?
There is no limit to what you can achieve using this lightweight workflow automation library in Python. If you find this useful, please do not forget to give us ⭐️ on our GitHub repository.
Click on the links below to see the documentation and working examples.
PyCaret’s default installation is a slim version of pycaret which only installs hard dependencies that are .
When you install the full version of pycaret, all the optional dependencies as are also installed.
The objective of regression is to predict continuous values such as sales amount, quantity, temperature, number of customers, etc. All modules in PyCaret provide many features to prepare the data for modeling through the function. It has over 25 ready-to-use algorithms and to analyze the performance of trained models.
For the purpose of this tutorial, I have used the US airline passengers dataset. You can download the dataset from .
To hear more about PyCaret follow us on and .
Join us on our slack channel. Invite link .