Reader small image

You're reading from  Deep Learning for Time Series Cookbook

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781805129233
Edition1st Edition
Right arrow
Authors (2):
Vitor Cerqueira
Vitor Cerqueira
author image
Vitor Cerqueira

​Vitor Cerqueira is a time series researcher with an extensive background in machine learning. Vitor obtained his Ph.D. degree in Software Engineering from the University of Porto in 2019. He is currently a Post-Doctoral researcher in Dalhousie University, Halifax, developing machine learning methods for time series forecasting. Vitor has co-authored several scientific articles that have been published in multiple high-impact research venues.
Read more about Vitor Cerqueira

Luís Roque
Luís Roque
author image
Luís Roque

Luís Roque, is the Founder and Partner of ZAAI, a company focused on AI product development, consultancy, and investment in AI startups. He also serves as the Vice President of Data & AI at Marley Spoon, leading teams across data science, data analytics, data product, data engineering, machine learning operations, and platforms. In addition, he holds the position of AI Advisor at CableLabs, where he contributes to integrating the broadband industry with AI technologies. Luís is also a Ph.D. Researcher in AI at the University of Porto's AI&CS lab and oversees the Data Science Master's program at Nuclio Digital School in Barcelona. Previously, he co-founded HUUB, where he served as CEO until its acquisition by Maersk.
Read more about Luís Roque

View More author details
Right arrow

Deep Learning for Time Series Classification

In this chapter, we’ll tackle time series classification (TSC) problems using deep learning. As the name implies, TSC is a classification task involving time series data. The dataset contains several time series, and each of these has an associated categorical label. This problem is similar to a standard classification task, but the input explanatory variables are time series. We’ll explore how to approach this problem using different approaches. Besides using the K-nearest neighbors model to tackle this task, we’ll also develop different neural networks, such as a residual neural network (ResNet) and a convolutional neural network.

By the end of this chapter, you’ll be able to set up a TSC task using a PyTorch Lightning data module and solve it with different models. You’ll also learn how to use the sktime Python library to solve this problem.

This chapter contains the following recipes:

    ...

Technical requirements

We’ll focus on the PyTorch Lightning ecosystem to build deep learning models. Besides that, we’ll also use scikit-learn to create a baseline. Overall, the list of libraries used in the package is the following:

  • scikit-learn (1.3.2)
  • pandas (2.1.3)
  • NumPy (1.26.2)
  • Torch (2.1.1)
  • PyTorch Lightning (2.1.2)
  • sktime (0.24.1)
  • keras-self-attention (0.51.0)

As an example, we’ll use the Car dataset from the repository available at the following link: https://www.timeseriesclassification.com. You can learn more about the dataset in the following work:

Thakoor, Ninad, and Jean Gao. Shape classifier based on generalized probabilistic descent method with hidden Markov descriptor. Tenth IEEE International Conference on Computer Vision (ICCV’05) Volume 1. Vol. 1. IEEE, 2005.

The code and datasets used in this chapter can be found at the following GitHub URL: https://github.com/PacktPublishing/Deep-Learning...

Tackling TSC with K-nearest neighbors

In this recipe, we’ll show you how to tackle TSC tasks using a popular method called K-nearest neighbors. The goal of this recipe is to show you how standard machine-learning models can be used to solve this problem.

Getting ready

First, let’s start by loading the data using pandas:

import pandas as pd
data_directory = 'assets/datasets/Car'
train = pd.read_table(f'{data_directory}/Car_TRAIN.tsv', header=None)
test = pd.read_table(f'{data_directory}/Car_TEST.tsv', header=None)

The dataset is already split into a training and testing set, so we read them separately. Now, let’s see how to build a K-nearest neighbor model using this dataset.

How to do it…

Here, we describe the steps necessary for building a time series classifier using scikit-learn:

  1. Let’s start by splitting the target variable from the explanatory variables:
    y_train = train.iloc[:, 0]
    y_test =...

Building a DataModule class for TSC

In this recipe, we return to the PyTorch Lightning framework. We’ll build a DataModule class to encapsulate the data preprocessing and the passing of observations to models.

Getting ready

Let’s load the dataset from the previous recipe:

import pandas as pd
data_directory = 'assets/datasets/Car'
train = pd.read_table(f'{data_directory}/Car_TRAIN.tsv', header=None)
test = pd.read_table(f'{data_directory}/Car_TEST.tsv', header=None)

Next, we’ll build a DataModule class to handle this dataset.

How to do it…

In the previous chapters, we used TimeSeriesDataSet from PyTorch Forecasting to handle the data preparation for us. This class managed several steps. These include normalization and transformation of the data for supervised learning. However, in TSC, an observation uses the entire time series as input:

  1. We’ll start creating a simpler variant of TimeSeriesDataSet...

Convolutional neural networks for TSC

In this recipe, we’ll walk you through building a convolutional neural network to tackle TSC problems. We’ll use the DataModule class created in the previous recipe to do this.

Getting ready

We start again by importing the dataset used in the previous recipe:

import pandas as pd
data_directory = 'assets/datasets/Car'
train = pd.read_table(f'{data_directory}/Car_TRAIN.tsv', header=None)
test = pd.read_table(f'{data_directory}/Car_TEST.tsv', header=None)
datamodule = TSCDataModule(train_df=train,
                           test_df=test,
                           batch_size=8)

We also create an instance of the TSCDataModule data...

ResNets for TSC

This recipe shows you how to train a ResNet for TSC tasks. ResNets are a type of deep neural network architecture widely used in computer vision problems, such as image classification or object detection. Here, you’ll learn how to use them for modeling time series data.

Getting ready

We’ll continue with the same dataset and data module as in the previous recipe:

import pandas as pd
data_directory = 'assets/datasets/Car'
train = pd.read_table(f'{data_directory}/Car_TRAIN.tsv', header=None)
test = pd.read_table(f'{data_directory}/Car_TEST.tsv', header=None)
datamodule = TSCDataModule(train_df=train,
    test_df=test,
    batch_size=8)

Let’s see how to build a ResNet and train it with PyTorch Lightning.

How to do it…

In this section, we describe the process of creating a ResNet for TSC tasks:

  1. Let’s start by creating a ResNet using nn.Module...

Tackling TSC problems with sktime

In this recipe, we explore an alternative approach to PyTorch for TSC problems, which is sktime. sktime is a Python library devoted to time series modeling, which includes several neural network models for TSC.

Getting ready

You can install sktime using pip. You’ll also need the keras-self-attention library, which includes self-attention methods necessary for running some of the methods in sktime:

pip install 'sktime[dl]'
pip install keras-self-attention

The trailing dl tag in squared brackets when installing sktime means you want to include the optional deep learning models available in the library.

In this recipe, we’ll use an example dataset available in sktime. We’ll load it in the next section.

How to do it…

As the name implies, the sktime library follows a design pattern similar to scikit-learn. So, our approach to building a deep learning model using sktime will be similar to the workflow...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Deep Learning for Time Series Cookbook
Published in: Mar 2024Publisher: PacktISBN-13: 9781805129233
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.
undefined
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 $15.99/month. Cancel anytime

Authors (2)

author image
Vitor Cerqueira

​Vitor Cerqueira is a time series researcher with an extensive background in machine learning. Vitor obtained his Ph.D. degree in Software Engineering from the University of Porto in 2019. He is currently a Post-Doctoral researcher in Dalhousie University, Halifax, developing machine learning methods for time series forecasting. Vitor has co-authored several scientific articles that have been published in multiple high-impact research venues.
Read more about Vitor Cerqueira

author image
Luís Roque

Luís Roque, is the Founder and Partner of ZAAI, a company focused on AI product development, consultancy, and investment in AI startups. He also serves as the Vice President of Data & AI at Marley Spoon, leading teams across data science, data analytics, data product, data engineering, machine learning operations, and platforms. In addition, he holds the position of AI Advisor at CableLabs, where he contributes to integrating the broadband industry with AI technologies. Luís is also a Ph.D. Researcher in AI at the University of Porto's AI&CS lab and oversees the Data Science Master's program at Nuclio Digital School in Barcelona. Previously, he co-founded HUUB, where he served as CEO until its acquisition by Maersk.
Read more about Luís Roque