Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Modern Time Series Forecasting with Python

You're reading from   Modern Time Series Forecasting with Python Industry-ready machine learning and deep learning time series analysis with PyTorch and pandas

Arrow left icon
Product type Paperback
Published in Oct 2024
Publisher Packt
ISBN-13 9781835883181
Length 658 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Jeffrey Tackes Jeffrey Tackes
Author Profile Icon Jeffrey Tackes
Jeffrey Tackes
Manu Joseph Manu Joseph
Author Profile Icon Manu Joseph
Manu Joseph
Arrow right icon
View More author details
Toc

Table of Contents (26) Chapters Close

Preface 1. Part-1: Getting Familiar with Time Series FREE CHAPTER
2. Introducing Time Series 3. Acquiring and Processing Time Series Data 4. Analyzing and Visualizing Time Series Data 5. Setting a Strong Baseline Forecast 6. Part-2: Machine Learning for Time Series
7. Time Series Forecasting as Regression 8. Feature Engineering for Time Series Forecasting 9. Target Transformations for Time Series Forecasting 10. Forecasting Time Series with Machine Learning Models 11. Ensembling and Stacking 12. Global Forecasting Models 13. Part-3: Deep Learning for Time Series
14. Introduction to Deep Learning 15. Building Blocks of Deep Learning for Time Series 16. Common Modeling Patterns for Time Series 17. Attention and Transformers for Time Series 18. Strategies for Global Deep Learning Forecasting Models 19. Specialized Deep Learning Architectures for Forecasting 20. Probabilistic Forecasting and More 21. Part-4: Mechanics of Forecasting
22. Multi-Step Forecasting 23. Evaluating Forecast Errors—A Survey of Forecast Metrics 24. Evaluating Forecasts—Validation Strategies 25. Index

To get the most out of this book

The book has to be considered bundled with the notebooks and associated code base in GitHub and is best used when both are used together. There are three levels of learning that you can do with the book. The first one is enabled by the text in the book alone and it will take you through the theory, build you intuitions and go through basic codes to get something implemented fast. To solidify the learning, we recommend you take the next step and use the provided notebooks and experiment with them. In most notebooks, we show how to do one thing in a particular way. But there are many levers, like hyperparameters, that you can tweak and run to understand how the output changes with these changes. This level of learning will make you understand the code and have a deeper understanding of the concepts you learning from the book.

And lastly, we have abstracted some code into src folder in the repository which is used in the notebooks. Your last and final level of learning is to go through and understand those so that you know how it works under the hood. Most of the code is well commented so that its easier for you to understand what’s happening under the hood of the functions and classes you used in the notebooks. This will elevate your learning to a level where you can confidently apply these techniques to other use-cases like a boss.

You should have basic familiarity with Python programming, as the entire code that we use for the practical sections is in Python. Familiarity with major libraries in Python, such as pandas and scikit-learn, are not essential (because the book covers some basics) but will help you get through the book much faster. Familiarity with PyTorch, the framework the book uses for deep learning, is also not essential but would accelerate your learning. Any of the software requirements shouldn’t stop you because, in today’s internet-enabled world, the only thing that is standing between you and a world of knowledge is the search bar in your favorite search engine.

Setting up an environment

Setting up an environment, preferably a separate one, for the book is highly recommended. There are two main ways we suggest to create the environment—Anaconda/Mamba or a Python virtual environment.

Using Anaconda/Miniconda/Mamba

The easiest way to set up an environment is by using Anaconda, a distribution of Python for scientific computing. You can use Miniconda, a minimal installer for Conda, as well if you do not want the pre-installed packages that come with Anaconda. And you can also use Mamba, a reimplementation of the conda package manager in C++. It is much faster than conda and is a drop-in replacement for conda. Mamba is the recommended way because it has much less chances of getting stuck at the dreaded Resolving dependencies… screen in Anaconda. If you are using Anaconda version 23.10 or above, then you need not worry about Mamba that much because the fast and efficient package resolver is part of anaconda by default.

  1. Install Anaconda/Miniconda/Mamba/MicroMamba: Anaconda can be installed from https://www.anaconda.com/products/distribution. Depending on your operating system, choose the corresponding file and follow the instructions. Alternatively, you can install Miniconda from here: https://docs.anaconda.com/miniconda/. You can install Mamba and MicroMamba from here: https://mamba.readthedocs.io/en/latest/. If you are using Mamba, in all the instructions below replace “conda" with “mamba".
  2. Open conda prompt: To open Anaconda Prompt (or Terminal on Linux or macOS), do the following:
    • Windows: Open the Anaconda Prompt (Start | Anaconda Prompt)
    • macOS: Open Launchpad and then open Terminal. Type conda activate.
    • Linux: Open Terminal. Type conda activate.
  3. Create a new environment: Use the following command to create a new environment of your choice. For instance, to create an environment named modern_ts_2E with Python 3.10 (recommended to use 3.10 or above), use the following command:
    conda create -n modern_ts_2E python=3.10
    
  4. Activate the environment: Use the following command to activate the environment:
    conda activate modern_ts_2E
    
  5. Install PyTorch from the official website: PyTorch is best installed from the official website. Go to https://pytorch.org/get-started/locally/ and select the appropriate options for your system. You can replace conda with mamba if you want to use Mamba to install.
  6. Navigate to the downloaded code: Use operating systeg-specific commands to navigate to the folder where you have downloaded the code. For instance, in Windows, use cd.
  7. Install the required libraries: Use the provided anaconda_env.yml file to install all the required libraries. Use the following command:
    conda env update --file anaconda_env.yml
    

This will install all the required libraries in the environment. This can take a while.

  1. Checking the installation: We can check if all the libraries required for the book is installed properly by executing a script in the downloaded code folder, python test_installation.py. If the GPU is not showing up, install PyTorch again on top of the environment.
  2. Activating the environment and Running Notebooks: Every time you want to run the notebooks, first activate the environment using the conda activate modern_ts_2E command and then use Jupyter Notebook (jupyter notebook) or Jupyter Lab (jupyter lab) according to your preference.

Using Python Virtual environments and pip

If you prefer to stick to native Python for environment management, we have provided an alternate requirements.txt as well, which should help you with that:

  1. Install Python: You can download Python from https://www.python.org/downloads/. Recommended to use 3.10 or above.
  2. Create a virtual environment: Use the following command to create a virtual environment named modern_ts_2E:
    python -m venv modern_ts_2E
    
  3. Activate the environment: Use the following command to activate the environment:
    • Windows: modern_ts_2E\Scripts\activate
    • macOS/Linux: source modern_ts_2E/bin/activate
  4. Install PyTorch: PyTorch is best installed from the official website. Go to https://pytorch.org/get-started/locally/ and select the appropriate options for your system.
  5. Navigate to the downloaded code: Use operating-system-specific commands to navigate to the folder where you have downloaded the code. For instance, in Windows, use cd.
  6. Install the required libraries: Use the provided requirements.txt file to install all the required libraries. Use the following command:
    pip install -r requirements.txt
    

This will install all the required libraries in the environment. This can take a while.

  1. Checking the installation: We can check if all the libraries required for the book is installed properly by executing a script in the downloaded code folder
    python test_installation.py
    
  2. Activating the environment and Running Notebooks: Every time you want to run the notebooks, first activate the environment using the command modern_ts_2E\Scripts\activate (Windows) or source modern_ts_2E/bin/activate (macOS/Linux) and then use Jupyter Notebook (jupyter notebook) or Jupyter Lab (jupyter lab) according to your preference.

What to do when environment creation throws an error?

Considering the wide variety of computers around the world and ever-changing library dependencies, there is a very real chance that the environment setup we are providing with the book (which is tested and working as of Sept 2024) may not stand the test of time. In such cases, we recommend you open the requirements.txt and see which libraries we are installing and try to figure out if downgrading or upgrading versions will help you along.

Another way to tackle this will be to just install the packages that you need for a notebook when you are using those. Mostly conflicts occur when we try to install different libraries into a same environment. For instance, one of the libraries needs PyTorch < 1.0.0, but some other library needs PyTorch > 1.0.0. In such cases, it makes sense to separate the two libraries into two environments or find another version of the library that is compatible with others.

Your best friend in such situations is a Google search and subsequent trawling of GitHub comments that complain of the same issue. As a last resort, you can also raise an issue in the book repository, and we can try to help you out.

Download the data

You are going to be using a single dataset throughout the book. The book uses London Smart Meters dataset from Kaggle for this purpose. Many of the notebooks from early chapters are dependencies for some of the later chapters. As such, to remove this dependency if you want to run the notebooks out of order, we have included a data.zip file with all the required datasets.

To set up, follow these steps:

  1. Download the data from AWS: https://packt-modern-time-series-py.s3.eu-west-1.amazonaws.com/data.zip.
  2. Unzip the content.
  3. Copy over the data folder to the Modern-Time-Series-Forecasting-with-Python-2E folder you pull from GitHub.

That’s it! You are now ready to start running the code.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository. Doing so will help you avoid any potential errors related to the copying and pasting of code.

The code that is provided along with the book is in no way a library but more of a guide for you to start experimenting on. The amount of learning you can derive from the book and code is directly proportional to how much you experiment with the code and stray outside your comfort zone. So, go ahead and start experimenting and putting the skills you pick up in the book to good use.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Modern-Time-Series-Forecasting-with-Python-2E. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://packt.link/gbp/9781835883181.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “statsmodels.tsa.seasonal has a function called seasonal_decompose.”

A block of code is set as follows:

#Does not support missing values, so using imputed ts instead
res = seasonal_decompose(ts, period=7*48, model="additive", extrapolate_trend="freq")

Any command-line input or output is written as follows:

conda env create -f anaconda_env.yml

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “But if you look at the Time Elapsed column, it stands out.”

IMPORTANT NOTES Appear like this.

Tips Appear like this.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image