
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:
- Building a Custom Model Pipeline in PyCaret: From Data Prep to Production
- Automated Feature Engineering in PyCaret
- 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.
import pandas as pd from pycaret.time_series import * Â # Load the dataset url = “https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv” data = pd.read_csv(url, header=0, index_col=0, parse_dates=True) Â # Display the first few rows print(data.head()) |
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.
# Initialize the PyCaret environment for time series forecasting ts_setup = setup(     data=data,     target=‘Passengers’,      session_id=123,      fold=3,     fh=12  ) |
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.
# Create a baseline ETS model model_baseline = create_model(‘ets’)Â Â # ETS stands for Exponential Smoothing |
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.
# Compare all models best_model = compare_models(sort=‘MASE’)Â Â # Sort by MASE (Mean Absolute Scaled Error) |
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 the best model tuned_model = tune_model(best_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.
# Make predictions for the next 12 months future_forecast = predict_model(tuned_model) print(future_forecast) |
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.
# Plot model diagnostics plot_model(tuned_model, plot=‘diagnostics’) |
Forecast Plots
PyCaret also lets you visualize the forecast. It shows the real values, predicted values, and future predictions.
# Plot forecast plot_model(tuned_model, plot=‘forecast’) |
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.
# Save the trained model save_model(tuned_model, ‘multi_step_forecast_model’) |
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.
# Load the saved model loaded_model = load_model(‘multi_step_forecast_model’) |
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.