Featured resource
2026 Tech Forecast
2026 Tech Forecast

1,500+ tech insiders, business leaders, and Pluralsight Authors share their predictions on what’s shifting fastest and how to stay ahead.

Download the forecast
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • AI
Labs

Time Series Modeling with XGBoost

In this guided lab, you’ll help a retailer investigate whether time series modeling can improve its next-day sales forecasts. Working in a Jupyter notebook with two years of daily sales data, you’ll inspect the data, create time-based features, and prepare a dataset for supervised learning. You’ll train an XGBoost model, compare its predictions with baseline forecasts using visualizations and error metrics, and use your findings to recommend a forecasting approach. By the end, you’ll have practical experience preparing time-series data and evaluating forecasts in a business context.

Lab platform
Lab Info
Level
Beginner
Last updated
Dec 03, 2026
Duration
1h 0m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use, and consent to receive marketing emails from Pluralsight.
Table of Contents
  1. Challenge

    1. Inspect the data

    You are a data analyst helping a retailer plan inventory for its top-selling product. The retailer estimates tomorrow's demand with today's sales. You will investigate whether models that use historical sales patterns can forecast next-day sales more accurately than that estimate.

    The data is in input_data/daily_sales.csv: daily units sold over two years, from November 2024 through October 2026.

    The forecast target is units_sold, the number of units sold on the day you're predicting. The timestamp is date, which tells you which day each observation describes.

    How the forecast works

    Each day, once that day's sales are final, you forecast tomorrow's sales. Tomorrow's actual sales then become available and feed the next day's forecast. This is a one-step-ahead forecast. It looks one day ahead and does not forecast several days at once.

    Working in the notebooks

    Each step has its own notebook, starting with 1-inspect-data.ipynb. Complete the indicated TODO cells, then run the cells in each task from top to bottom. Each task saves a file that Validate checks.

    Check the data before modeling

    Sales exports often contain problems that quietly distort a model. Look for three problem types:

    • Duplicate rows: exact copies of an earlier row
    • Missing values: blank units_sold
    • Missing dates: calendar days between the first and last date that have no row

    Confirm that the rows are sorted by date. Lag and rolling features depend on that order.

    Start by opening 1-inspect-data.ipynb. ### Before you continue

    • Which patterns do you see over time and by day of week?

      Sales ran higher in July and August 2025 than in December 2025 and February 2026. In the weekday chart, Saturday has the highest average and Tuesday has the lowest. These patterns suggest that both recent sales and the day of the week could help forecast tomorrow's sales.

    • Which values would be known on the day you forecast tomorrow's sales?

      Once today's sales are final, you know sales through today and calendar facts about tomorrow, such as its day of the week. Tomorrow's actual sales become available after the forecast.

    Move on to Step 2 to turn past sales and calendar dates into features for forecasting tomorrow's demand.

  2. Challenge

    2. Engineer features

    A model can't see "yesterday" or "last Saturday" in a column of sales. You create those inputs as features. In this step, each row of the table represents a forecast date, and its features use information available before that date.

    • Lag features copy earlier sales onto the current row. lag_7 is sales from seven days earlier, which is the same weekday in the previous week.
    • Rolling statistics summarize a recent window, such as the mean and standard deviation of the last 7 or 28 days. They smooth out single-day noise and show whether sales are running high or low.
    • Calendar features describe the forecast date itself. They are safe to use because the calendar is known in advance.

    Avoid leaking the answer

    A rolling window over units_sold includes the current day's sales, which is the value you are trying to forecast. Shift units_sold by one day first, then compute the rolling statistics on the shifted values. Each row then uses prior days only.

    Lags and rolling windows also need history. A 28-day window has no value for the first days of the series, so those rows will have missing features. You remove them in the next step.

    To continue, select File > Open and open 2-engineer-features.ipynb. Complete the indicated TODO cells, then run the cells in each task from top to bottom. Tasks 2.1–2.3 add columns to features and save the updated table to training_data/features.csv. Task 2.4 saves a separate report. ### Before you continue

    Review the counts from Task 2.4. The 28-day window needs 28 prior days of sales, so the first 28 rows are incomplete.

    • Why remove those rows? Those dates lack enough recorded sales to calculate a full 28-day window. Filling the features would substitute estimates for that missing history. Using later sales to fill them would also leak information that was unavailable at forecast time.

    Continue to Step 3 to remove incomplete rows and split the remaining data into training, validation, and test periods.

  3. Challenge

    3. Create datasets

    Each row of the table represents one forecasting example. The features are the lag, rolling, and calendar columns. The target is units_sold on the row's date, which is the value to forecast. The sales features describe earlier days, and the calendar features describe the forecast date. Both are available before you make the forecast.

    Remove incomplete rows

    Early rows without a full history have missing features. Models can't learn from empty inputs, and filling them would invent information. Drop these rows so the model trains on complete inputs.

    Split by time, not at random

    Forecasting predicts the future from the past. A random split lets the model train on days after the ones it is tested on, which makes the results look better than they would be in use. Split chronologically into three datasets:

    | Dataset | Dates | Use | | :--- | :--- | :--- | | Training | through April 2026 | Fit models | | Validation | May to July 2026 | Compare models and choose one | | Test | August to October 2026 | Evaluate the chosen model once, at the end |

    To continue, select File > Open and open 3-create-datasets.ipynb. Complete the TODO cells, then run the cells in each task from top to bottom. ### Before you continue

    • Why would choosing a model based on test results make the final score unreliable?

      You might pick a model that happened to do well on those dates. Choose the model using its performance on the validation period, so the test period can provide an independent estimate of how well it performs on new data.

    • Why does a fair comparison need identical validation days?

      Some days are harder to forecast than others. Scoring every model, including the naive forecast, against the same actual sales on the same dates means each model faces the same mix of easy and difficult days.

    Move on to Step 4 to train the models and measure their errors on the validation period.

  4. Challenge

    4. Train models

    You will create a naive forecast, train three supervised models, and measure their errors on the same validation days, using the same target and metric.

    | Model | What it does | | :--- | :--- | | Naive | Forecasts tomorrow with today's sales, as the retailer does now | | Linear regression | Fits one straight-line weight to each feature | | Calendar-only XGBoost | Gradient-boosted decision trees that see only the date | | Full-feature XGBoost | Gradient-boosted decision trees that see lags, rolling statistics, and the calendar |

    XGBoost builds many small decision trees, each one correcting the errors of the trees before it. Trees can learn rules such as "on weekends, when the last 28 days were busy, sales spike," which a single straight-line weight per feature can't express. Comparing the two XGBoost models shows what the engineered history adds beyond the calendar.

    Mean absolute error

    Mean absolute error (MAE) is the average size of the forecast miss, in the same units as the target. An MAE of 40 means the forecast is off by 40 units sold on average. Lower is better.

    Provided settings

    The notebook defines TARGET as "units_sold".

    Both XGBoost models use the xgb_params defined at the top of the notebook. Tuning is not the focus of this lab, so leave them unchanged.

    To continue, select File > Open and open 4-train-models.ipynb. Run the first code cell before starting Task 4.1. It loads the training and validation datasets as training_model_data and validation and defines TARGET, all_features, calendar_features, and xgb_params.

    Complete the indicated TODO cells. After each task, run the provided save cell. Use the paths listed in that task to confirm the files were saved. ### Before you continue

    Run the final code cell in 4-train-models.ipynb to print and compare the four validation MAEs from Tasks 4.1–4.4.

    • Which models beat the naive forecast?

      In the reference run, linear regression and full-feature XGBoost both had lower validation MAE than the naive forecast. Calendar-only XGBoost had a higher MAE.

    • What does the XGBoost comparison tell you about the engineered features?

      Both models use the same settings and calendar features. The full-feature model also uses lags and rolling statistics. Its lower validation MAE suggests that past sales and recent sales patterns helped on these dates.

    Continue to Step 5 to compare the forecasts, evaluate your chosen model on the test period, and recommend an approach for inventory planning.

  5. Challenge

    5. Recommend a forecasting approach

    You will compare validation errors and prediction plots, choose a model, and evaluate it on the held-out test period. After that, you will use the results and feature importance to recommend an approach for the retailer's inventory planning. Any model may come out ahead, so base your choice on the measured results.

    • Use the validation results to choose the model.
    • Use the test period once to estimate how the chosen model would perform on new days.

    Each day's forecast uses the actual sales of earlier days, including the test period's earlier days. This matches how the retailer would use the forecast: one day ahead, with yesterday's actual sales available.

    Reading the results

    The validation_mae dictionary maps each model name to its validation MAE. A negative percentage improvement means the model had a higher validation MAE than the naive forecast.

    MAE is an average. A model with a low MAE can still miss badly on unusual days, so consider the typical miss alongside what the retailer does with the forecast, such as setting a safety stock buffer.

    Feature importance shows which inputs the XGBoost model relied on most. It describes the model's behavior and does not prove that a feature causes sales to change.

    After running the feature importance cell, observe which engineered features the full-feature XGBoost model relied on most. Use those observations in your recommendation.

    To continue, select File > Open and open 5-recommend-approach.ipynb. Complete the TODO cells, then run the cells in each task from top to bottom. ### Summary

    You inspected and cleaned a daily sales series, built lag and rolling features from prior sales and calendar features from the forecast date, built chronological training, validation, and test datasets, and compared a naive forecast, linear regression, and two XGBoost models on the same days using MAE. You then used validation results to choose a model and confirmed it once on the test period.

    The comparison between the two XGBoost models shows how much of the forecasting signal came from engineered history rather than the calendar. No single approach wins everywhere, so a recommendation should rest on a fair comparison, as yours does, and be checked again as new sales arrive.

    To finish, review your saved recommendation in output_data/recommendation.md and check that it connects the forecast errors to the retailer's inventory decisions.

About the author

Michael Jenkins has more than 20 years of experience working with automation technologies and is well versed with AWS, GCP, and Azure clouds.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight