Extending RNNs for Time Series Classification Tasks

Introduction

In this lesson, we will explore how to extend Recurrent Neural Networks (RNNs) for time series classification tasks. Time series classification involves predicting categorical labels based on sequential data. We will use a dataset containing monthly airline passenger numbers to demonstrate the process of loading and preparing data, building an RNN classification model, and evaluating its performance. By the end of this lesson, you will have a solid understanding of how to apply RNNs to classify time series data.

Loading and Preparing Data for Classification

To begin, we need to load our time series data and prepare it for classification tasks. We'll use a dataset containing monthly airline passenger numbers as an example. The first step is to load the data and preprocess it to create input sequences and corresponding labels.

import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler

# Load dataset
df = pd.read_csv('AirPassengers.csv')

# Ensure the column name matches
df.columns = ['Month', 'Passengers']

# Generate labels BEFORE scaling
df['Label'] = (df['Passengers'].diff().shift(-1) > 0).astype(int)  # Create binary labels: 1 if next month's passengers increase, else 0

# Normalize the data
scaler = MinMaxScaler(feature_range=(0, 1))
df['Passengers'] = scaler.fit_transform(df['Passengers'].values.reshape(-1, 1))

# Create input sequences
def create_sequences(data, labels, seq_length):
    X, y = [], []
    for i in range(len(data) - seq_length):
        X.append(data[i:i + seq_length])
        y.append(labels[i + seq_length])  # Correct alignment
    return np.array(X), np.array(y)

seq_length = 10
X, y = create_sequences(df['Passengers'].values, df['Label'].values, seq_length)

In this code, we load the dataset using pandas and ensure the column names match. We generate binary labels indicating whether the next value in the time series is higher or lower before scaling the data. We then normalize the passenger numbers to a range between 0 and 1 using MinMaxScaler. The function create_sequences generates input sequences of a specified length (seq_length) and their corresponding labels, returning the input sequences X and the target values y.

Data Preparation for Classification

Next, we prepare the data specifically for classification by converting the labels to a categorical format.

from tensorflow.keras.utils import to_categorical

# Convert labels to categorical format
y_classification = to_categorical(y)

Here, we convert the binary labels to a categorical format using to_categorical, which is necessary for training the classification model.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal