More about forecasting in cienciadedatos.net
- ARIMA and SARIMAX models with python
- Time series forecasting with machine learning
- Forecasting time series with gradient boosting: XGBoost, LightGBM and CatBoost
- Forecasting time series with XGBoost
- Global Forecasting Models: Multi-series forecasting
- Global Forecasting Models: Comparative Analysis of Single and Multi-Series Forecasting Modeling
- Probabilistic forecasting
- Forecasting with deep learning
- Forecasting energy demand with machine learning
- Forecasting web traffic with machine learning
- Intermittent demand forecasting
- Modelling time series trend with tree-based models
- Bitcoin price prediction with Python
- Stacking ensemble of machine learning models to improve forecasting
- Interpretable forecasting models
- Mitigating the Impact of Covid on forecasting Models
- Forecasting time series with missing values
Introduction¶
Intermittent demand forecasting is a statistical method used to predict the demand for products that have sporadic or irregular sales patterns. These types of products are characterized by periods of high demand followed by periods of little or no demand. Intermittent demand forecasting is used in the manufacturing, retail, and healthcare industries to manage inventory levels, optimize production schedules, and reduce stockouts and excess inventory costs.
Regular intermittent demand refers to predictable demand patterns with known intervals between demand periods. This type of intermittent demand occurs with a degree of regularity, such as a product that has seasonal demand or a product that is ordered every month on a certain date. Intermittent demand is traditionally forecast with statistical methods such as Croston's method, which models separately the size of the non-zero demands and the intervals between them. However, these methods do not take into account exogenous variables that can provide valuable information about when demand will occur, for example, a known calendar of opening hours. As a result, machine learning-based forecasting can be an appealing alternative, as it can incorporate these variables and improve accuracy.
On the other hand, irregular intermittent demand refers to unpredictable demand patterns, with no known intervals between demand periods. This type of intermittent demand is random and unpredictable, such as a product that is ordered only a few times a year and in varying quantities. Forecasting irregular intermittent demand is challenging because there is no predictable pattern to the demand. Traditional forecasting methods may not work effectively in these situations and alternative forecasting methods, such as bootstrapping or simulation, may be required.
In summary, regular intermittent demand has a predictable pattern whereas irregular intermittent demand does not. Forecasting regular intermittent demand is easier than forecasting irregular intermittent demand due to the predictability of the demand pattern.
This document demonstrates how the Python library skforecast can be used to forecast regular intermittent demand scenarios. The approach combines a custom metric, so that the model is evaluated and tuned only on the periods of interest, with features that inform the model of the periods in which there is no demand and of the position of each observation within the day and the week.
✎ Note
In the example used in this document, the zeros are structural: the store is closed at night and on weekends, so the periods without demand follow a schedule that is known in advance. This is different from irregular intermittent demand, where zeros occur at random and the main difficulty is to predict when the demand will occur.
Libraries¶
# Data processing
# ==============================================================================
import pandas as pd
# Plots
# ==============================================================================
import plotly.graph_objects as go
import plotly.express as px
import plotly.io as pio
import plotly.offline as poff
pio.templates.default = 'seaborn'
poff.init_notebook_mode(connected=True)
# Modeling
# ==============================================================================
import skforecast
from lightgbm import LGBMRegressor
from sklearn.metrics import mean_absolute_error
from skforecast.recursive import ForecasterRecursive
from skforecast.preprocessing import CalendarFeatures
from skforecast.model_selection import TimeSeriesFold
from skforecast.model_selection import grid_search_forecaster
from skforecast.model_selection import backtesting_forecaster
# Warnings configuration
# ==============================================================================
import warnings
warnings.filterwarnings('once')
color = '\033[1m\033[38;5;208m'
print(f'{color}Version skforecast: {skforecast.__version__}')
Version skforecast: 0.27.0
Data¶
The data used in this example represents the number of users who visited a store each hour. The store is open from Monday to Friday, from 7:00 to 21:00, so the last hourly record with visitors each day is the one at 20:00. Outside opening hours, the number of users is always zero. Therefore, any predictions outside this period are not useful and can either be ignored or set to 0.
The series has an hourly frequency and spans from 2011-01-01 to 2012-12-31 (17,544 observations). Around 58% of the observations are zero, all of them corresponding to hours when the store is closed.
# Downloading data
# ======================================================================================
url = ('https://raw.githubusercontent.com/JoaquinAmatRodrigo/Estadistica-machine'
'-learning-python/master/data/intermittent_demand.csv')
data = pd.read_csv(url, sep=',')
data['date_time'] = pd.to_datetime(data['date_time'], format='%Y-%m-%d %H:%M:%S')
data = data.set_index('date_time')
data = data.sort_index()
data = data.asfreq('h')
data.head(3)
| users | |
|---|---|
| date_time | |
| 2011-01-01 00:00:00 | 0.0 |
| 2011-01-01 01:00:00 | 0.0 |
| 2011-01-01 02:00:00 | 0.0 |
The data is divided into three partitions that respect the temporal order of the observations: a training set, used to fit the model; a validation set, used to select the lags and hyperparameters; and a test set, reserved to estimate the performance of the final model on data that has not been used at any earlier stage. In time series, the split must always be chronological. A random split would allow the model to learn from observations that are later than the ones it is asked to predict, which leads to overly optimistic results.
# Split train-val-test
# ======================================================================================
end_train = '2012-03-31 23:59:00'
end_validation = '2012-08-31 23:59:00'
data_train = data.loc[: end_train, :]
data_val = data.loc[end_train:end_validation, :]
data_test = data.loc[end_validation:, :]
print(
f'Dates train : {data_train.index.min()} --- {data_train.index.max()} '
f'(n={len(data_train)})'
)
print(
f'Dates validation : {data_val.index.min()} --- {data_val.index.max()} '
f'(n={len(data_val)})'
)
print(
f'Dates test : {data_test.index.min()} --- {data_test.index.max()} '
f'(n={len(data_test)})'
)
Dates train : 2011-01-01 00:00:00 --- 2012-03-31 23:00:00 (n=10944) Dates validation : 2012-04-01 00:00:00 --- 2012-08-31 23:00:00 (n=3672) Dates test : 2012-09-01 00:00:00 --- 2012-12-31 23:00:00 (n=2928)
# Plot time series
# ======================================================================================
fig = go.Figure()
trace1 = go.Scatter(
x=data_train.index, y=data_train['users'], name='train', mode='lines'
)
trace2 = go.Scatter(
x=data_val.index, y=data_val['users'], name='validation', mode='lines'
)
trace3 = go.Scatter(
x=data_test.index, y=data_test['users'], name='test', mode='lines'
)
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.add_trace(trace3)
fig.update_layout(
title='Time series of users',
xaxis_title='Date time',
yaxis_title='Users',
width = 750,
height = 350,
margin=dict(l=20, r=20, t=35, b=20),
legend=dict(orientation='h', yanchor='top', y=1.1, xanchor='left', x=0.001)
)
fig.show()
# Boxplot for weekly seasonality
# ==============================================================================
data['week_day'] = data.index.day_of_week + 1
fig = px.box(
data,
x='week_day',
y='users',
labels={'week_day': 'Day of week (1 = Monday)'},
title = 'Distribution of users per day of week',
width=600,
height=300
)
median_values = data.groupby('week_day')['users'].median()
fig.add_trace(
go.Scatter(
x=median_values.index,
y=median_values.values,
mode='lines+markers',
line=dict(color='blue', dash='dash'),
showlegend=False
)
)
fig.update_layout(margin=dict(l=20, r=20, t=35, b=20))
fig.show()
# Boxplot for daily seasonality
# ==============================================================================
data['hour_day'] = data.index.hour
fig = px.box(
data,
x='hour_day',
y='users',
labels={'hour_day': 'Hour of day'},
title = 'Distribution of users per hour of day',
width=600,
height=300
)
median_values = data.groupby('hour_day')['users'].median()
fig.add_trace(
go.Scatter(
x=median_values.index,
y=median_values.values,
mode='lines+markers',
line=dict(color='blue', dash='dash'),
showlegend=False
)
)
fig.update_layout(margin=dict(l=20, r=20, t=35, b=20))
fig.show()
The plots show the two characteristics that define this series:
The store is closed on Saturdays and Sundays (days 6 and 7) and, on weekdays, before 7:00 and from 21:00 onwards. In all these periods, the number of users is exactly zero.
During opening hours, demand follows a marked daily pattern with two peaks, one around 8:00 and another around 17:00-18:00.
A model that only uses the lags of the series has no explicit information about the opening schedule, so it has to infer it from the most recent values.
Custom metric to evaluate the model¶
To accurately evaluate the performance of the model, it is crucial to define a metric that closely reflects the business scenario in which the model will be used. Specifically, in this case, the model's performance should be optimized for the hours in which the store is open: weekdays from 7:00 to 21:00, that is, the hourly records from 7:00 to 20:00.
Fortunately, skforecast provides the flexibility to use custom functions as metrics for backtesting and model tuning. A custom metric is a function that receives the arguments y_true and y_pred and returns a single value.
def custom_metric(y_true, y_pred):
"""
Calculate the mean absolute error using only the predicted values for the
opening hours of the store: Monday to Friday, from 7:00 to 20:59.
"""
day_of_week = y_true.index.day_of_week
hour_of_day = y_true.index.hour
mask = day_of_week.isin([0, 1, 2, 3, 4]) & (hour_of_day >= 7) & (hour_of_day <= 20)
metric = mean_absolute_error(y_true[mask], y_pred[mask])
return metric
⚠ Warning
If the closed hours were included in the metric, more than half of the evaluated observations would be zeros that are trivial to predict, since the predictions for those hours are simply set to 0. The resulting error would be diluted and would look much better than the error the business actually experiences during opening hours. In addition, backtesting_forecaster calculates the metric with the predictions as they are returned by the model, before any post-processing, which is another reason to restrict the metric to the opening hours.
Forecasting¶
A first model is trained using only the past values of the series as predictors. A ForecasterRecursive is created with a LightGBM regressor and the last 24 lags, that is, the number of users in each of the previous 24 hours.
The model is evaluated with backtesting using the function backtesting_forecaster. The validation strategy is defined with a TimeSeriesFold: the model is trained once with the training and validation data (initial_train_size) and then used to predict the test period in consecutive folds of 36 hours (steps = 36), without being refitted. This simulates a scenario in which, every 36 hours, the forecast for the next 36 hours is generated with the data available up to that moment. Since the test set has 2,928 hours, the process is repeated in 82 folds.
# Create forecaster
# ==============================================================================
forecaster = ForecasterRecursive(
estimator = LGBMRegressor(
learning_rate = 0.1,
max_depth = 5,
n_estimators = 500,
random_state = 123,
verbose = -1
),
lags = 24
)
forecaster
ForecasterRecursive
General Information
- Estimator: LGBMRegressor
- Lags: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
- Window features: None
- Calendar features: None
- Window size: 24
- Series name: None
- Exogenous included: False
- Categorical features: auto
- Weight function included: False
- Differentiation order: None
- Drop NaN from series: False
- Creation date: 2026-11-11 21:45:45
- Last fit date: None
- Skforecast version: 0.27.0
- Python version: 3.13.18
- Forecaster id: None
Exogenous Variables
None
Data Transformations
- Transformer for y: None
- Transformer for exog: None
Training Information
- Training range: Not fitted
- Training index type: Not fitted
- Training index frequency: Not fitted
Estimator Parameters
-
{'boosting_type': 'gbdt', 'class_weight': None, 'colsample_bytree': 1.0, 'importance_type': 'split', 'learning_rate': 0.1, 'max_depth': 5, 'min_child_samples': 20, 'min_child_weight': 0.001, 'min_split_gain': 0.0, 'n_estimators': 500, 'n_jobs': None, 'num_leaves': 31, 'objective': None, 'random_state': 123, 'reg_alpha': 0.0, 'reg_lambda': 0.0, 'subsample': 1.0, 'subsample_for_bin': 200000, 'subsample_freq': 0, 'verbose': -1}
Fit Kwargs
-
{}
# Backtesting test period
# ==============================================================================
cv = TimeSeriesFold(steps = 36, initial_train_size = len(data.loc[:end_validation]))
metric_lags, predictions = backtesting_forecaster(
forecaster = forecaster,
y = data['users'],
cv = cv,
metric = custom_metric
)
metric_lags
| custom_metric | n_evaluated | |
|---|---|---|
| 0 | 198.521846 | 2928 |
# Set to zero predictions for closed hours
# ==============================================================================
hour = predictions.index.hour
day_of_week = predictions.index.day_of_week
closed_hours = (hour < 7) | (hour > 20)
closed_days = day_of_week.isin([5, 6])
is_closed = (closed_hours) | (closed_days)
predictions.loc[is_closed, 'pred'] = 0
# Plot predictions vs real value
# ======================================================================================
fig = go.Figure()
trace1 = go.Scatter(
x=data_test.index, y=data_test['users'], name='test', mode='lines'
)
trace2 = go.Scatter(
x=predictions.index, y=predictions['pred'], name='prediction', mode='lines'
)
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.update_layout(
title='Real value vs predicted in test data',
xaxis_title='Date time',
yaxis_title='Users',
width = 750,
height = 350,
margin=dict(l=20, r=20, t=35, b=20),
legend=dict(orientation='h', yanchor='top', y=1.1, xanchor='left', x=0.001)
)
fig.show()
When analyzing the predictions, it is clear that the model does not accurately reproduce the demand pattern of the opening hours: the predicted values fall well below the real ones. To better understand where the model fails, the residuals are analyzed by day of the week and hour of the day.
# Distribution of residuals by day of week and hour of day
# ==============================================================================
residuals = (predictions['pred'] - data_test['users']).to_frame('residuals')
residuals['week_day'] = residuals.index.day_of_week + 1
residuals['hour_day'] = residuals.index.hour
fig = px.box(
residuals,
x='week_day',
y='residuals',
labels={'week_day': 'Day of week (1 = Monday)'},
title = 'Distribution of residuals per day of week',
width=600,
height=300
)
fig.update_layout(margin=dict(l=20, r=20, t=35, b=20))
fig.show()
fig = px.box(
residuals,
x='hour_day',
y='residuals',
labels={'hour_day': 'Hour of day'},
title = 'Distribution of residuals per hour of day',
width=600,
height=300
)
fig.update_layout(margin=dict(l=20, r=20, t=35, b=20))
fig.show()
The residuals are calculated as the prediction minus the actual value, so negative values indicate that the model underestimates the number of users. The residuals of the hours in which the store is closed are exactly zero because those predictions were set to 0. Two patterns stand out:
The model underestimates the demand on every weekday, and the largest underestimation occurs on Mondays, the day after the two closing days. When Monday is predicted, the most recent lags correspond to the weekend, when the number of users is zero.
By hour of the day, the largest errors are concentrated in the two demand peaks: the morning peak shortly after the store opens (8:00) and the evening peak (17:00-18:00).
Inform the model when the store is closed¶
Exogenous variables can be used in a forecasting model to provide additional information and improve the model's ability to detect patterns. This approach offers the advantage of incorporating external factors that could influence the accuracy of the forecast, leading to a more reliable and accurate forecasting model. The exog argument makes it incredibly easy to integrate exogenous variables into the forecaster. In this case, a binary variable is_closed is created to indicate the hours in which the store is closed.
✎ Note
Exogenous variables must be known for the whole forecast horizon at the moment the prediction is made. The variable is_closed is derived exclusively from the calendar (day of the week and hour of the day), so its future values are known in advance and using it does not introduce data leakage.
# Create exogenous variable
# ==============================================================================
hour = data.index.hour
day_of_week = data.index.day_of_week
closed_hours = (hour < 7) | (hour > 20)
closed_days = day_of_week.isin([5, 6])
is_closed = (closed_hours) | (closed_days)
data['is_closed'] = is_closed.astype(int)
# Update the partitions so that they include the new variable
data_train = data.loc[: end_train, :]
data_val = data.loc[end_train:end_validation, :]
data_test = data.loc[end_validation:, :]
# Backtesting test period
# ==============================================================================
metric_exog, predictions = backtesting_forecaster(
forecaster = forecaster,
y = data['users'],
exog = data['is_closed'],
cv = cv,
metric = custom_metric
)
metric_exog
| custom_metric | n_evaluated | |
|---|---|---|
| 0 | 107.744754 | 2928 |
# Set to zero predictions for closed hours
# ==============================================================================
is_closed = data.loc[predictions.index, 'is_closed'] == 1
predictions.loc[is_closed, 'pred'] = 0
# Plot predictions vs real value
# ======================================================================================
fig = go.Figure()
trace1 = go.Scatter(
x=data_test.index, y=data_test['users'], name='test', mode='lines'
)
trace2 = go.Scatter(
x=predictions.index, y=predictions['pred'], name='prediction', mode='lines'
)
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.update_layout(
title='Real value vs predicted in test data',
xaxis_title='Date time',
yaxis_title='Users',
width = 750,
height = 350,
margin=dict(l=20, r=20, t=35, b=20),
legend=dict(orientation='h', yanchor='top', y=1.1, xanchor='left', x=0.001)
)
fig.show()
Incorporating the exogenous variable into the model leads to a significant improvement in forecasting accuracy: the mean absolute error during opening hours is reduced by almost half. This highlights the importance of using external factors to enhance the performance of the forecasting model.
Calendar features and weekly lag¶
The residual analysis of the first model showed that the largest errors occur at specific hours of the day (the two demand peaks) and on a specific day of the week (Monday). The variable is_closed tells the model when the store is closed, but not where each observation is located within the day or the week. Two additional sources of information are added to the model:
Calendar features: the day of the week and the hour of the day, extracted from the index of the series with
CalendarFeatures. Withencoding = 'cyclical', each feature is transformed into a sine-cosine pair, so that the model knows that, for example, hour 23 and hour 0 are consecutive. The transformer is passed to the forecaster with the argumentcalendar_features, and the features are created automatically during training and prediction. See calendar features for more details.Weekly lag: lag 168, that is, the number of users at the same hour of the same day of the previous week. With only the last 24 lags, when the store opens on a Monday, all the lags correspond to the weekend.
Like is_closed, the calendar features only depend on the date, so their future values are known in advance.
# Create forecaster with calendar features and weekly lag
# ==============================================================================
calendar_transformer = CalendarFeatures(
features = ['day_of_week', 'hour'],
encoding = 'cyclical',
keep_original_columns = False
)
# Lags 1 to 24 and lag 168 (same hour of the previous week)
forecaster = ForecasterRecursive(
estimator = LGBMRegressor(
learning_rate = 0.1,
max_depth = 5,
n_estimators = 500,
random_state = 123,
verbose = -1
),
lags = list(range(1, 25)) + [168],
calendar_features = calendar_transformer
)
# Backtesting test period
# ==============================================================================
metric_calendar, predictions = backtesting_forecaster(
forecaster = forecaster,
y = data['users'],
exog = data['is_closed'],
cv = cv,
metric = custom_metric
)
metric_calendar
| custom_metric | n_evaluated | |
|---|---|---|
| 0 | 87.129526 | 2928 |
# Set to zero predictions for closed hours
# ==============================================================================
is_closed = data.loc[predictions.index, 'is_closed'] == 1
predictions.loc[is_closed, 'pred'] = 0
# Plot predictions vs real value
# ======================================================================================
fig = go.Figure()
trace1 = go.Scatter(
x=data_test.index, y=data_test['users'], name='test', mode='lines'
)
trace2 = go.Scatter(
x=predictions.index, y=predictions['pred'], name='prediction', mode='lines'
)
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.update_layout(
title='Real value vs predicted in test data',
xaxis_title='Date time',
yaxis_title='Users',
width = 750,
height = 350,
margin=dict(l=20, r=20, t=35, b=20),
legend=dict(orientation='h', yanchor='top', y=1.1, xanchor='left', x=0.001)
)
fig.show()
With the calendar features and the weekly lag, the error in the test set is reduced again, by almost one fifth with respect to the previous model.
Model tuning¶
Hyperparameter tuning is a critical aspect of developing accurate and effective machine learning models. Hyperparameters are values that cannot be learned from data and must be set by the user before the model is trained. These hyperparameters can have a significant impact on the performance of the model, and careful tuning can improve its accuracy and generalization to new data. In the case of forecasting models, the lags included in the model can be considered as an additional hyperparameter.
Hyperparameter tuning involves systematically testing different values or combinations of hyperparameters (including lags) to find the optimal configuration that gives the best results. The skforecast library offers several hyperparameter tuning strategies, including grid search, random search, and Bayesian search, to identify the optimal combination of lags and hyperparameters that achieve the best prediction performance.
In this example, a grid search is performed with grid_search_forecaster. Each combination of lags and hyperparameters is evaluated by backtesting on the validation set with the same custom metric, so the test set is never used during the tuning process. By default (return_best = True), once the search has finished, the forecaster is refitted with the best configuration found. The search starts from the last model: the calendar features are kept and all the candidate sets of lags include the weekly lag.
# Grid search hyperparameters and lags
# ==============================================================================
forecaster = ForecasterRecursive(
estimator = LGBMRegressor(
learning_rate = 0.1,
max_depth = 5,
n_estimators = 500,
random_state = 123,
verbose = -1
),
lags = 24, # Placeholder, replaced in the grid search
calendar_features = calendar_transformer
)
# Lags: all the candidates include the weekly lag
lags_grid = {
'lags 1-24 + 168': list(range(1, 25)) + [168],
'lags 1-48 + 168': list(range(1, 49)) + [168],
'lags 1-72 + 168': list(range(1, 73)) + [168]
}
# Hyperparameters
param_grid = {
'n_estimators': [50, 100, 500],
'max_depth': [5, 10, 15],
'learning_rate': [0.01, 0.1],
}
# Folds
cv_search = TimeSeriesFold(steps = 36, initial_train_size = len(data.loc[:end_train]))
results_grid = grid_search_forecaster(
forecaster = forecaster,
y = data.loc[:end_validation, 'users'],
exog = data.loc[:end_validation, 'is_closed'],
cv = cv_search,
param_grid = param_grid,
lags_grid = lags_grid,
metric = custom_metric
)
# Search results
# ==============================================================================
columns = ['lags_label', 'learning_rate', 'max_depth', 'n_estimators', 'custom_metric']
results_grid[columns].head(4)
| lags_label | learning_rate | max_depth | n_estimators | custom_metric | |
|---|---|---|---|---|---|
| 0 | lags 1-72 + 168 | 0.1 | 10.0 | 100.0 | 78.803211 |
| 1 | lags 1-72 + 168 | 0.1 | 15.0 | 100.0 | 80.656150 |
| 2 | lags 1-72 + 168 | 0.1 | 5.0 | 100.0 | 80.832176 |
| 3 | lags 1-48 + 168 | 0.1 | 5.0 | 100.0 | 81.133973 |
# Backtesting test period
# ==============================================================================
metric_tuned, predictions = backtesting_forecaster(
forecaster = forecaster,
y = data['users'],
exog = data['is_closed'],
cv = cv,
metric = custom_metric
)
metric_tuned
| custom_metric | n_evaluated | |
|---|---|---|
| 0 | 83.167124 | 2928 |
# Set to zero predictions for closed hours
# ==============================================================================
is_closed = data.loc[predictions.index, 'is_closed'] == 1
predictions.loc[is_closed, 'pred'] = 0
# Plot predictions vs real value
# ======================================================================================
fig = go.Figure()
trace1 = go.Scatter(
x=data_test.index, y=data_test['users'], name='test', mode='lines'
)
trace2 = go.Scatter(
x=predictions.index, y=predictions['pred'], name='prediction', mode='lines'
)
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.update_layout(
title='Real value vs predicted in test data',
xaxis_title='Date time',
yaxis_title='Users',
width = 750,
height = 350,
margin=dict(l=20, r=20, t=35, b=20),
legend=dict(orientation='h', yanchor='top', y=1.1, xanchor='left', x=0.001)
)
fig.show()
The first row of the table of results shows the configuration selected by the grid search. With it, the error in the test set decreases again. In addition, the error in the test set is similar to the one obtained in the validation set during the search, which indicates that the selected configuration generalizes well to new data.
Summary of results¶
# Summary of the results in the test set
# ==============================================================================
results = pd.concat([metric_lags, metric_exog, metric_calendar, metric_tuned])
# Each model adds new elements to the previous one
results.index = [
'Lags only',
'+ is_closed',
'+ calendar features and weekly lag',
'+ tuning of lags and hyperparameters'
]
results = results.rename(columns={'custom_metric': 'MAE opening hours'})
results['Reduction vs previous model (%)'] = (
-100 * results['MAE opening hours'].pct_change()
)
results.round(1)
| MAE opening hours | n_evaluated | Reduction vs previous model (%) | |
|---|---|---|---|
| Lags only | 198.5 | 2928 | NaN |
| + is_closed | 107.7 | 2928 | 45.7 |
| + calendar features and weekly lag | 87.1 | 2928 | 19.1 |
| + tuning of lags and hyperparameters | 83.2 | 2928 | 4.5 |
Each step reduces the error with respect to the previous model. The largest improvement comes from informing the model of when the store is closed, and the error of the final model is less than half the error of the initial one.
✎ Note
In this document, the hours in which the store is closed are still part of the training data. An alternative is to exclude them from model training by giving them a weight of zero, or to reduce their influence with a lower weight, using the weight_func argument of the forecaster. See Weighted time series forecasting for more details.
Session information¶
import session_info
session_info.show(html=False)
----- lightgbm 4.7.0 pandas 2.3.3 plotly 7.1.0 session_info v1.0.1 skforecast 0.27.0 sklearn 1.9.1 ----- IPython 9.17.1 jupyter_client 8.10.0 jupyter_core 5.9.1 ----- Python 3.13.18 | packaged by conda-forge | (main, Oct 26 2026, 11:30:40) [MSC v.1944 64 bit (AMD64)] Windows-11-10.0.26200-SP0 ----- Session information updated at 2026-11-11 21:46
Citation¶
How to cite this document
If you use this document or any part of it, please acknowledge the source, thank you!
Intermittent demand forecasting with skforecast by Joaquín Amat Rodrigo and Javier Escobar Ortiz, available under Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0 DEED) at https://www.cienciadedatos.net/documentos/py48-intermittent-demand-forecasting.html
How to cite skforecast
If you use skforecast for a publication, we would appreciate it if you cite the published software.
Zenodo:
Amat Rodrigo, Joaquin, & Escobar Ortiz, Javier. (2026). skforecast (v0.27.0). Zenodo. https://doi.org/10.5281/zenodo.8382787
APA:
Amat Rodrigo, J., & Escobar Ortiz, J. (2026). skforecast (Version 0.27.0) [Computer software]. https://doi.org/10.5281/zenodo.8382787
BibTeX:
@software{skforecast, author = {Amat Rodrigo, Joaquin and Escobar Ortiz, Javier}, title = {skforecast}, version = {0.27.0}, month = {11}, year = {2026}, license = {BSD-3-Clause}, url = {https://skforecast.org/}, doi = {10.5281/zenodo.8382787} }
Did you like the article? Your support is important
Your contribution will help me to continue generating free educational content. Many thanks! 😊
This work by Joaquín Amat Rodrigo and Javier Escobar Ortiz is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International.
Allowed:
-
Share: copy and redistribute the material in any medium or format.
-
Adapt: remix, transform, and build upon the material.
Under the following terms:
-
Attribution: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
-
NonCommercial: You may not use the material for commercial purposes.
-
ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
