Multiple Time Series Forecasting with PyCaret

Multiple Time Series Forecasting with PyCaret

A step-by-step tutorial to forecast multiple time series with PyCaret

PyCaret — An open-source, low-code machine learning library in Python

PyCaret

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.

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:

RECAP

In my last tutorialarrow-up-right, I have demonstrated how you can use PyCaret to forecast time-series data using Machine Learning through PyCaret Regression Modulearrow-up-right. If you haven’t read that yet, you can read Time Series Forecasting with PyCaret Regression Modulearrow-up-right tutorial before continuing with this one, as this tutorial builds upon some important concepts covered in the last tutorial.

Installing PyCaret

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’s default installation is a slim version of pycaret which only installs hard dependencies that are listed herearrow-up-right.

When you install the full version of pycaret, all the optional dependencies as listed herearrow-up-right are also installed.

👉 PyCaret Regression Module

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’).

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 pre-processingarrow-up-right features to prepare the data for modeling through the setup arrow-up-rightfunction. It has over 25 ready-to-use algorithms and several plotsarrow-up-right to analyze the performance of trained models.

👉 Dataset

For this tutorial, I will show the end-to-end implementation of multiple time-series data forecasting, including both the training as well as predicting future values.

I have used the Store Item Demand Forecasting Challengearrow-up-right dataset from Kaggle. This dataset has 10 different stores and each store has 50 items, i.e. total of 500 daily level time series data for five years (2013–2017).

Sample Dataset

👉 Load and prepare the data

Samples rows from data

👉 Visualize time-series

store_1_item_1 time series and 30-day moving average
store_2_item_1 time series and 30-day moving average

👉 Start the training process

Now that we have the data ready, let’s start the training loop. Notice that verbose = False in all functions to avoid printing results on the console while training.

The code below is a loop around time_series column we created during the data preparatory step. There are a total of 150 time series (10 stores x 50 items).

Line 10 below is filtering the dataset for time_series variable. The first part inside the loop is initializing the setup function, followed by compare_models to find the best model. Line 24–26 captures the results and appends the performance metrics of the best model in a list called all_results . The last part of the code uses the finalize_model function to retrain the best model on the entire dataset including the 5% left in the test set and saves the entire pipeline including the model as a pickle file.

We can now create a data frame from all_results list. It will display the best model selected for each time series.

sample_rows from concat_results

Training Process 👇

Training process

👉 Generate predictions using trained models

Now that we have trained models, let’s use them to generate predictions, but first, we need to create the dataset for scoring (X variables).

sample rows from score_df dataset

Now let’s create a loop to load the trained pipelines and use the predict_model function to generate prediction labels.

samples rows from concat_df

We will now join the dataand concat_df .

sample rows from final_df

We can now create a loop to see all plots.

store_1_item_1 actual sales and predicted labels
store_2_item_1 actual sales and predicted labels

I hope that you will appreciate the ease of use and simplicity in PyCaret. In less than 50 lines of code and one hour of experimentation, I have trained over 10,000 models (25 estimators x 500 time series) and productionalized 500 best models to generate predictions.

Coming Soon!

Next week I will be writing a tutorial on unsupervised anomaly detection on time-series data using PyCaret Anomaly Detection Modulearrow-up-right. Please follow me on Mediumarrow-up-right, LinkedInarrow-up-right, and Twitterarrow-up-right to get more updates.

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.

To hear more about PyCaret follow us on LinkedInarrow-up-right and Youtubearrow-up-right.

Join us on our slack channel. Invite link herearrow-up-right.

You may also be interested in:

Build your own AutoML in Power BI using PyCaret 2.0arrow-up-right Deploy Machine Learning Pipeline on Azure using Dockerarrow-up-right Deploy Machine Learning Pipeline on Google Kubernetes Enginearrow-up-right Deploy Machine Learning Pipeline on AWS Fargatearrow-up-right Build and deploy your first machine learning web apparrow-up-right Deploy PyCaret and Streamlit app using AWS Fargate serverlessarrow-up-right Build and deploy machine learning web app using PyCaret and Streamlitarrow-up-right Deploy Machine Learning App built using Streamlit and PyCaret on GKEarrow-up-right

Documentationarrow-up-right Blogarrow-up-right GitHubarrow-up-right StackOverflowarrow-up-right Install PyCaret arrow-up-rightNotebook Tutorials arrow-up-rightContribute in PyCaretarrow-up-right

Want to learn about a specific module?

Click on the links below to see the documentation and working examples.

Classification arrow-up-rightRegressionarrow-up-right Clusteringarrow-up-right Anomaly Detectionarrow-up-right Natural Language Processing arrow-up-rightAssociation Rule Miningarrow-up-right

Last updated

Was this helpful?