Data and modeling
Machine learning for time series forecasting with Python
Time series forecasting with Python uses statistical models and machine learning to predict future values such as electricity load, sales or demand. Common approaches are exponential smoothing and ARIMA, gradient boosting on lag features, and global models trained across many related series.
How machine learning forecasting is used with Python
Business forecasts at scale
Companies forecast many metrics, such as visits, orders or capacity, often with analysts who are not forecasting specialists. Decomposable models with trend, seasonality and holiday terms give sensible results that people can adjust.
Real exampleProphet (Meta, open source)Meta describes Prophet as optimised for the business forecast tasks it encountered, typically hourly, daily or weekly data with strong seasonality and holidays.
Global models across many related series
Instead of one model per product or store, a single model is trained on thousands of related series and learns shared patterns. This helps series with short histories and produces probabilistic forecasts for stock planning.
Real exampleDeepAR (Amazon, 2017)The paper proposes training an autoregressive recurrent network on a large number of related time series to produce accurate probabilistic forecasts, with retail demand as a motivating example.
Deep learning forecasters
Neural architectures built for forecasting can work across domains without hand-made features. They are evaluated on public competition datasets so results can be compared.
Real exampleN-BEATS (Oreshkin et al., 2019)N-BEATS is a deep architecture of fully connected layers with residual links, tested on the M3, M4 and TOURISM competition datasets.
Pretrained forecasting models
Foundation models trained on large collections of time series can forecast a new series with no training, known as zero-shot forecasting. They are a quick baseline when history is short or many series need forecasts.
Real exampleChronos (Amazon, 2024)Chronos tokenises time series values and trains transformer language model architectures on them to create pretrained probabilistic forecasting models.
Forecasting with a foundation model from Google
Large pretrained forecasters are also offered as open models that can be run from Python. Teams compare them with tuned local models before relying on them.
Real exampleTimesFM (Google Research, ICML 2024)Google describes TimesFM as a forecasting model pretrained on 100 billion real-world time points that shows zero-shot performance on public benchmarks.
Can you learn time series forecasting with Python?
Yes. If you know basic Python, you can learn forecasting in a clear order: pandas for dates, simple baselines, statistical models, then machine learning on lag features and global models. The most important skill is honest backtesting.
- PythonWrite functions and loops confidently so you can run backtests over many series and cut-off dates.Learn it in Python
- pandas time seriesResample, shift and roll data by date, and plot seasonality to understand a series before modelling it.Learn it in Data Tools
- Baselines and statistical modelsFit naive, seasonal naive, exponential smoothing and ARIMA models with StatsForecast or statsmodels.
- Machine learning forecastingTrain LightGBM on lag and calendar features and evaluate with rolling-origin backtests against the baselines.Learn it in Machine Learning
- Neural and pretrained forecastersTry global neural models and pretrained forecasters with Darts or similar libraries, and check whether they beat your tuned baselines.Learn it in Deep Learning
Moving into forecasting roles
If you already work in the field
Demand planners, energy analysts and finance teams already understand the series they forecast and how forecasts are used. Add Python and a backtesting routine, then replace one spreadsheet forecast with a model that beats it across several past periods.
If you come from data science or ML
If you know ML, learn forecasting conventions: horizons, prediction intervals, hierarchical totals and metrics such as MASE. Read Hyndman and Athanasopoulos's 'Forecasting: Principles and Practice' and agree the forecast horizon with the people who use it.
Roles that do this workForecasting data scientistDemand plannerSupply chain analystEnergy analystMachine learning engineer
Tools you will use
| Tool | What you use it for |
|---|---|
| StatsForecast | Fit fast ARIMA, ETS and seasonal-naive baselines across many series. |
| LightGBM | Train gradient-boosted forecasters on lag and calendar features. |
| skforecast | Turn any scikit-learn regressor into a recursive forecaster with built-in backtesting. |
| Darts | Try classical, tree-based and neural forecasters through one interface. |
| Prophet | Fit trend and seasonality models with holiday effects for business series. |
Public data to practise on
- ElectricityLoadDiagrams20112014 (UCI): Electricity consumption of many Portuguese clients at fifteen-minute intervals, for load forecasting.
- Monash Time Series Forecasting Repository: A curated collection of forecasting datasets from energy, tourism, sales and other fields, for benchmarking.
Your first project
Download ElectricityLoadDiagrams20112014, forecast one client's next-day load with LightGBM on lag features, and compare it with a same-time-last-week baseline using mean absolute error across rolling forecast origins.
Common questions
Which Python library is best for time series forecasting?
It depends on the job. StatsForecast is fast for classical models, LightGBM with skforecast or MLForecast handles lag-feature models, and Darts and sktime give one interface across many methods.
Is LSTM good for time series forecasting?
It can be, especially with many related series, but on single series it often fails to beat well-tuned statistical models or gradient boosting. Always compare with simple baselines.
How do I evaluate a forecasting model?
Use rolling-origin backtesting: train up to a cut-off date, forecast the next horizon, move the cut-off and repeat. Report a scale-free error such as MASE alongside an error in business units.
How much history do I need to forecast?
At least two full seasonal cycles to estimate seasonality, for example two years for yearly patterns. With less, use simpler models, global models trained on related series, or pretrained forecasters.
Courses for these skills
- Python for AI and Machine LearningWrite functions and inspect exceptions while building a repeatable analysis.33 published lessons · all planned lessons open
- Data Tools: NumPy, pandas, Matplotlib and scikit-learnAlign observation dates, releases and feature tables.23 published lessons · all planned lessons open
- Practical Machine Learning in PythonLearn regression and validation for dated data.51 published lessons · more lessons in production
New to programming or maths? Start with Python, or follow the machine learning path in order. Published lessons are free to read; compare Free and Pro.