Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Machine Learning for Time Series with Python
  • Table Of Contents Toc
Machine Learning for Time Series with Python

Machine Learning for Time Series with Python - Second Edition

By : Ben Auffarth
close
close
Machine Learning for Time Series with Python

Machine Learning for Time Series with Python

By: Ben Auffarth

Overview of this book

The Python ecosystem offers a wide range of tools for time series analysis and time series forecasting. Machine Learning for Time Series, Second Edition provides a practical guide to building forecasting systems while developing a solid understanding of modern predictive modeling techniques. Starting with the fundamentals of time series data, you'll learn how to prepare datasets, perform feature engineering, and build forecasting pipelines. The book covers traditional methods such as ARIMA, SARIMA, and GARCH, alongside machine learning approaches including gradient boosting, recurrent neural networks, and deep learning models. Through practical examples and clear explanations, you'll learn how to choose the right model for the right problem and improve forecasting accuracy across multiple applications. Updated content includes forecasting and signal extraction for financial markets, plus case studies from operations management, digital marketing, healthcare, and financial forecasting. By the end of this book, you'll be able to confidently perform time series analysis and build effective forecasting systems using Python.
Table of Contents (7 chapters)
close
close

When do smart models fail?

Our forecasting engines had never been more advanced. Yet that very complexity created a dangerous paradox. In March 2020, as COVID‑19 swept the globe, these state‑of‑the‑art systems at major retailers began producing wildly inaccurate predictions.

At Target, machine‑learning models trained on years of stable history couldn't explain an 845 percent surge in toilet‑paper demand or an 89 percent collapse in apparel sales. At Amazon, inventory algorithms that had expertly balanced millions of SKUs suddenly flooded warehouses with home‑gym and office‑gear nobody needed—while critical cleaning and health products vanished from shelves. These failures weren't the result of simple spreadsheet errors but of forecasting pipelines—some pure ML, others decomposition‑based—that either omitted an explicit seasonality‑and‑anomaly step or relied on static, pre‑pandemic settings. Instead of flagging the March–April 2020 spikes as one‑off shocks, they folded them into their trend and seasonal baselines, yielding persistent over‑ and under‑forecasts until the models were hurriedly re‑tuned.

It's important to remember that these weren't simple statistical models running on outdated spreadsheets, but ML systems built by world-class data science teams using state-of-the-art methods. Yet, within weeks, billions of dollars in forecasting infrastructure were rendered nearly useless. Walmart's demand‑planning engines—which normally allocate stock across 4,700 stores with surgical precision—began issuing orders that bore no relation to real buying patterns. CVS Health's prescription models, built on years of consistent patient behavior, missed the surge in anxiety‑drug scripts and failed to anticipate the drop in routine refills as people skipped doctor visits. Across retail and healthcare, systems once lauded for their high validation scores and rigorous A/B tests became sources of systematic error instead of competitive advantage.

In the next section, we'll examine the technical sophistication that made these failures so surprising and instructive.

These failures reveal why time series requires different thinking. The sophisticated feature engineering that works well for cross-sectional data can become actively harmful when underlying patterns change faster than models can adapt. These organizations were deploying methods that represented the pinnacle of data science best practices circa 2020:

  • Advanced feature engineering: Target's models incorporated over 200 engineered features, including rolling averages, seasonal decompositions, vacation effects, weather impacts, and complex interaction terms between promotional activities and product categories. These feature engineering techniques, properly implemented, are covered systematically in Chapter 5.
  • Ensemble methods: Amazon's systems combined Random Forest, XGBoost, and neural network predictions using sophisticated stacking approaches validated through extensive backtesting and A/B tests. Chapter 5 demonstrates how to implement these ensemble approaches using modern Python frameworks designed for production reliability.
  • Real-time processing: Walmart's demand forecasting updated hourly, ingesting point-of-sale data, weather forecasts, social media sentiment, and economic indicators through streaming analytics pipelines. The monitoring and real-time adaptation strategies that could have detected these failures early are covered in Chapter 10.
  • Rigorous validation: These weren't models hastily deployed without testing. They had been validated using time series cross-validation, stress-tested against historical anomalies like Black Friday and hurricane seasons, and deployed with appropriate prediction intervals and monitoring systems. However, they used validation approaches designed for stable environments rather than the dynamic validation frameworks that we'll cover starting with Chapter 3.

Yet when consumer behavior shifted faster than their training pipelines could adapt, even the most advanced machine learning became a liability. The sophisticated feature engineering that captured nuanced seasonal patterns became actively misleading when seasonality disappeared overnight. The ensemble methods that provided robust predictions during normal times amplified errors when all component models failed simultaneously.

Understanding temporal dependencies is crucial because they create both opportunities and challenges. While past values can provide powerful predictive information, they also mean that patterns can change over time: a phenomenon called concept drift, where the statistical relationship between inputs and the target shifts, so a model trained on yesterday's distribution gradually becomes wrong on tomorrow's. The same dependencies that make forecasting possible also make validation harder: the data-generating process itself moves with the calendar, so a holdout score collected last quarter cannot certify how the model will behave this quarter. A validation framework for time series therefore has to score both how well a model fits the past and how quickly that fit decays as the world drifts.

Before we can build systems that work reliably when patterns change, we need to establish what makes time series fundamentally different from the static datasets most machine learning methods assume.

The key insight is that time series data violates a fundamental assumption of most machine learning methods: that observations are independent and identically distributed. When we treat temporal observations as independent data points that just happen to be ordered chronologically, we miss the crucial dependencies between past and future values. This is why techniques that work well for cross-sectional data—like random train/test splits or standard feature engineering—can fail with time series.

Unlock full access

Continue reading for free

A Packt free trial gives you instant online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon