Introduction to RNNs for Multivariate Time Series with PyTorch

Introduction to RNNs for Multivariate Time Series

Welcome to the next step in your journey of handling multivariate time series with Recurrent Neural Networks (RNNs). In the previous lessons, you learned how to preprocess the Air Quality dataset and prepare it for RNN input. Now, we will focus on building and training an RNN model to predict Temperature (T) using multiple features from this dataset. RNNs are particularly well-suited for time series forecasting due to their ability to capture temporal dependencies in sequential data. By the end of this lesson, you will have a solid understanding of how to construct and train an RNN model for multivariate time series forecasting.

Quick Recap of Preprocessing Steps

Before building the RNN model, it's essential to preprocess the Air Quality dataset to ensure it is suitable for input into the model. Here are the preprocessing steps you should have completed:

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

# Load the Air Quality dataset from the URL
url = "https://codesignal-staging-assets.s3.amazonaws.com/uploads/1742293523899/AirQualityUCI.csv"
df = pd.read_csv(url, sep=';', decimal=',')

# Replace -200 with NaN
df.replace(-200, np.nan, inplace=True)

# Combine 'Date' and 'Time' into a single 'DateTime' column
df['DateTime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'], format="%d/%m/%Y %H.%M.%S")

# Drop the original 'Date' and 'Time' columns
df.drop(columns=['Date', 'Time'], inplace=True)

# Drop unnecessary columns
df.drop(columns=['Unnamed: 15', 'Unnamed: 16'], inplace=True)

# Drop rows where essential features are missing
df.dropna(subset=['CO(GT)', 'NO2(GT)', 'T', 'RH'], inplace=True)

# Fill missing values using forward-fill & backward-fill
df.ffill(inplace=True)
df.bfill(inplace=True)

# Set DateTime as index
df.set_index('DateTime', inplace=True)

# Select relevant features for RNN
features = ['CO(GT)', 'NO2(GT)', 'PT08.S5(O3)', 'RH', 'T']
df_selected = df[features]

# Normalize the data
scaler = StandardScaler()
df_scaled = scaler.fit_transform(df_selected)

# Function to create sequences for multi-input RNN
def create_multivariate_sequences(data, seq_length=10):
    X, y = [], []
    for i in range(len(data) - seq_length):
        X.append(data[i:i+seq_length])  # Multi-feature input
        y.append(data[i+seq_length, -1])  # Predicting Temperature (T)
    return np.array(X), np.array(y)

# Define sequence length
sequence_length = 10

# Create sequences
X, y = create_multivariate_sequences(df_scaled, sequence_length)

# Reshape input for RNN
X = X.reshape((X.shape[0], sequence_length, len(features)))  # (samples, timesteps, features)
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