Time Series Forecasting with PyCaret: Building Multi-Step Prediction Model


Time Series Forecasting with PyCaret: Building Multi-Step Prediction Model

Time Series Forecasting with PyCaret: Building Multi-Step Prediction Model
Image by Editor | Midjourney & Canva

Getting Familiar with Time Forecasting

Time series forecasting helps predict future data using past information, useful in areas like finance, weather, and inventory. Accurate time-related predictions help businesses make better choices.

Single-step forecasting is the process of predicting just the next value in a time series. This method focuses only on one future time point. Multi-step forecasting means predicting future values over multiple periods, such as weeks or months. There are two ways to do this:

  • Direct Forecasting: A new model is made for each future time step
  • Recursive Forecasting: The model uses past predictions to predict the next values

Multi-step forecasting is useful in areas like finance, supply chain, and weather forecasting.

What is PyCaret?

PyCaret is a Python tool that makes forecasting easy. It automates many steps in the machine learning workflow, like choosing models, engineering features, and finding the best performing models. PyCaret can help with the following:

  • Ease of Use: PyCaret makes setting up machine learning models easy with its simple interface
  • Comprehensive Model Selection: PyCaret offers many time series models like ARIMA, ETS, and Prophet
  • Automatic Feature Engineering: PyCaret creates useful features, like past data points and moving averages, to improve predictions
  • Model Tuning and Evaluation: PyCaret helps improve models by adjusting settings and checking their performance

In this article, we will show how to build a multi-step forecast. Multi-step means predicting more than just the next single value. PyCaret helps with data, models, and checking results. We’ll explain how to build and improve your forecasting model.

You may find the first 3 articles in this series helpful before moving ahead with this one:

  1. Building a Custom Model Pipeline in PyCaret: From Data Prep to Production
  2. Automated Feature Engineering in PyCaret
  3. Creating Powerful Ensemble Models with PyCaret

Before getting started, make sure you have PyCaret installed. You can do so with pip:

Preparing the Data

We will use the Airline Passenger dataset for this example. It shows the number of airline passengers each month.

Preparing the DataPreparing the Data

Here, the Month column is the time index. The Passengers column is the target variable we want to predict.

Initialize PyCaret

The setup() function in PyCaret prepares your data for modeling. It automatically handles tasks like detecting trends, filling missing values, and encoding categorical features. This step sets up the environment for time series forecasting.

Key parameters in setup include:

  • data: The time series data
  • target: The name of the column we want to predict (Passengers)
  • fh: Forecast horizon (how far ahead to predict); here, we predict the next 12 months

Creating a Baseline Model

A baseline model is a starting point for forecasting, which is useful in evaluating more complex models. PyCaret provides create_model() to easily build one. The ETS (exponential smoothing) model is a good baseline for time series data. It captures trends and seasonality in the data.

Creating a Baseline ModelCreating a Baseline Model

The ETS model is simple and effective for time series data.

Comparing Models

PyCaret has many forecasting models, and you can use compare_models() to test and compare them. This function ranks models based on their performance. You can sort models by MASE (mean absolute scaled error) to help find the best one.

Comparing ModelsComparing Models

PyCaret ranks models based on performance. With all other things being equal, choose the one with the lowest error for better predictions.

Tune the Model

Once you select a model, you can improve its accuracy by fine-tuning its settings. Use the tune_model() function to optimize hyperparameters.

tune_modeltune_model

PyCaret automatically adjusts the model’s parameters to get better results.

Making Multi-Step Forecasts

After training and tuning your model, the next step is to make multi-step forecasts. You can do this using the predict_model() function in PyCaret. It helps predict future values for a set period of time.

Making Multi-Step ForecastsMaking Multi-Step Forecasts

This will give a table with:

  • Historical Predictions: Predicted values for past data points
  • Future Forecasts: Predictions for the next 12 months (or the set horizon)

Visualizing the Results

Visualizations help understand model performance. PyCaret offers built-in plotting tools.

Model Diagnostics

PyCaret includes useful diagnostic plots to evaluate the performance of your model. One such plot is the residual diagnostics, showing model residuals.

Model DiagnosticsModel Diagnostics

Forecast Plots

PyCaret also lets you visualize the forecast. It shows the real values, predicted values, and future predictions.

Forecast PlotsForecast Plots

Exporting and Deploying the Model

After your model is ready, you can save it, allowing you to use it again later without re-training.

Saving the Model

Use the save_model() function to save the model. This stores the model so you can use it in the future.

Loading the Model

When you need the saved model, you can load it using the load_model() function. This will bring the model back into memory.

Conclusion

PyCaret makes time series forecasting simple and fast, helping you build multi-step prediction models with just a few lines of code. PyCaret automatically takes care of tasks like cleaning data and creating features, and it offers many forecasting models you can choose from for your needs. Multi-step forecasting helps you predict future values over time, which is useful for planning and decision-making. With PyCaret, even beginners can create accurate models quickly, and immediately get insights from your time series data.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here