New Time Series Forecasting with PyCaret! | Towards Data Science

IntroductionSetting Up, Comparing, Tuning, and Blending ModelsProductionalize ModelSummaryReferences
Introduction
Setting Up, Comparing, Tuning, and Blending Models
Productionalize Model
Summary
References
To be frank, even though it seems simple, time-series algorithms are usually difficult to deal with. PyCaret [2] alleviates a lot of monotonous and confusing work by automating the setup and comparison of models processes of time series. You can take it a step even further by productionalizing your chosen model as well with this Python library. You can still, however, have own your input and autonomy within the library by adjusting different parameters. With that being said, let’s take a deeper dive into a use case to illustrate how simple forecasting is with this unique tool and library below.
PyCaret has what seems countless models to choose from, as well as compare against. Some, I honestly have not even heard of, as well as some of the more popular and reliable ones like ARIMA (Auto-Regressive Integrated Moving Average) model.
Here are some of the time series model names you can try, and to reiterate, there are even more:
Seasonal Naive Forecaster
Exponential Smoothing
ARIMA
Auto ARIMA
Polynomial Trend Forecaster
K Neighbors w/ Cond. Deseasonalize & Detrending
Linear w/ Cond. Deseasonalize & Detrending
Elastic Net w/ Cond. Deseasonalize & Detrending
Ride w/ Cond. Deseasonalize & Detrending
Lasso Net w/ Cond. Deseasonalize & Detrending
Extreme Gradient Boosting w/ Cond. Deseasonalize & Detrending
As you can see, there are a lot of models you can use and compare so you do not have to do any tedious work like importing a long list of different libraries. You might also find some new ones that you have not tried yet.
Here is a simple tutorial to follow for easy time-series forecasting with PyCaret:
# Code is edited and reorganized from example source:https://nbviewer.org/github/pycaret/pycaret/blob/time_series_beta/time_series_101.ipynb [4]ex = TimeSeriesExperiment()ex.setup(data=y, fh=fh, fold=fold, session_id=100)y_train = ex.get_config("y_train")y_test = ex.get_config("y_test")best_baseline_models = ex.compare_models(fold=fold, sort='MAE', n_select=10)compare_metrics = ex.pull()best_tuned_models = [ex.tune_model(model) for model in best_baseline_models]mean_blender = ex.blend_models(best_tuned_models, method='mean')y_predict = ex.predict_model(mean_blender)ex.plot_model(estimator=mean_blender)
In this code from above, we can see that in just a few lines of code, you can compare virtually all time-series models, tune them, and blend them for the best results. You could even be pretty accurate without tuning or blending if you wanted to save some time. Another great feature is pulling metrics from the various models and viewing them side-by-side.
Overall, this small chunk of code could normally take up much more of your notebook space, so it is a benefit to be able to focus on the nice visualizations from PyCaret, rather than the smaller or less important parts of code that you will no longer need to address.
Now that you have tested several models and compared their metrics like MAE in this example (or any other metrics like RMSE, MAPE, SMAPE, and R2, as well as the time it takes to run the models), you can start to save and load your model for future use.
First, you will save whatever model you have chosen is the best for whatever reason. Then, you will load the model in a new session, and predict new data.
Saving your final model is easy. Simply use the save_model function, and name it so that you can load that same name or version for future predictions/forecasting.
ex.save_model(final_model, "my_final_model")
Loading your model is also easy, and you can apply the predict method to see how your predictions are along with your index or first column.
ex_load = TimeSeriesExperiment()loaded_model = ex_load.load_model("my_final_model")
With even less code, you can productionalize your model by saving and loading your model for new, unseen data. There are also further methods where you can utilize prediction intervals with custom alphas. Other features include:
Window splitting
Expanding window
Rolling window
Error handling
It is best to jump in with your dataset and apply this code to your specific use case so you can start predicting or forecasting with time series algorithms.
PyCaret is always updating so make sure to be on the lookout for new functionalities daily or weekly. Time-series can be the less glamourous data science problem, but it might be the most requested from stakeholders at a company. It really depends on you, if you are struggling with time series, then this article showed you how, in just a few lines of code, you can compare, analyze, and use models for forecasting.
To summarize, here is what we learned:
* Setting Up, Comparing, Tuning, and Blending Models* Productionalize Model
I hope you found my article both interesting and useful. Please feel free to comment down below if you agree or disagree with this method of learning and implementing time series forecasting. Why or why not? What other libraries or tools do you think you could use? This library can certainly be clarified even further, but I hope I was able to shed some light on difficult time series problems. Thank you for reading!
I am not affiliated with any of these companies.
Please feel free to check out my profile, Matt Przybyla, and other articles, as well as subscribe to receive email notifications for my blogs by following the link below, or by clicking on the subscribe icon on the top of the screen by the follow icon, and reach out to me on LinkedIn if you have any questions or comments.
Subscribe link: https://datascience2.medium.com/subscribe
[1] Photo by Icons8 Team on Unsplash, (2018)
[2] Moez Ali, PyCaret Time Series, (2021)
[3] Photo by Glenn Carstens-Peters on Unsplash, (2019)
[4] PyCaret, Time Series Jupyter Notebook Example, (2021)
[5] Photo by Bagus Hernawan on Unsplash, (2017)