Introduction to Time Series Forecasting with LSTMs Using PyTorch

Introduction to Time Series Forecasting with LSTMs

Welcome to the first lesson of the "Time Series Forecasting with LSTMs" course. In this lesson, we will explore the fundamentals of time series forecasting using Long Short-Term Memory (LSTM) networks. Time series data is crucial in various fields such as finance, weather forecasting, and stock market analysis. LSTMs are a special kind of Recurrent Neural Network (RNN) capable of learning long-term dependencies, making them particularly suitable for time series forecasting.

About the Dataset and Preprocessing

The Airline Passenger Traffic dataset is a well-known example of time series data, often used to demonstrate time series analysis techniques. It contains monthly totals of international airline passengers from 1949 to 1960. This dataset is valuable for illustrating trends, seasonality, and other time series characteristics. We also used this dataset in the first course of this learning path, making it a familiar and consistent example for exploring time series forecasting techniques.

For this course, we have stored the dataset in a CSV file named data.csv, which includes a 'Month' column representing the time intervals and a 'Passengers' column representing the number of passengers. This setup allows you to easily load and work with the dataset in your environment.

Data preprocessing is a critical step in time series forecasting. It involves preparing the data in a format suitable for training LSTM models. We will use Pandas to load and manipulate the data. First, we load the data from the CSV file and convert the 'Month' column to a datetime format. This allows us to set it as the index, which is essential for time series analysis. Next, we normalize the data using MinMaxScaler from Scikit-learn. Normalization scales the data to a range between 0 and 1, which helps improve the performance of the model.

Preprocessing Code Example

Below is a code example that demonstrates how to preprocess the time series data for LSTM training. Each step is explained in the comments:

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

def preprocess_data(filepath, seq_length=10):
    """Preprocess time series data for RNN training."""
    # Load the dataset from the CSV file
    data = pd.read_csv(filepath)
    # Convert the 'Month' column to datetime format
    data['Month'] = pd.to_datetime(data['Month'])
    # Set 'Month' as the index for time series analysis
    data.set_index('Month', inplace=True)   
    
    # Normalize the 'Passengers' column to the range [0, 1]
    scaler = MinMaxScaler()
    normalized_data = scaler.fit_transform(data)
    
    # Create sequences of length 'seq_length' for LSTM input
    X, y = [], []
    for i in range(len(normalized_data) - seq_length):
        # X contains sequences of 'seq_length' time steps
        X.append(normalized_data[i:i+seq_length])
        # y contains the value immediately following each sequence
        y.append(normalized_data[i+seq_length])
    
    X, y = np.array(X), np.array(y)
    # Reshape X to (samples, time steps, features) for LSTM input
    X = X.reshape((X.shape[0], X.shape[1], 1))
    
    return X, y, scaler

seq_length=10

X, y, scaler = preprocess_data('data.csv', seq_length)
  • Loading and Indexing: The data is loaded and the 'Month' column is converted to a datetime type and set as the index, which is important for time series operations.
  • Normalization: The MinMaxScaler scales the passenger numbers to a range between 0 and 1, which helps the LSTM model train more effectively.
  • Sequence Creation: The code creates input sequences (X) of a specified length (seq_length) and corresponding target values (y). This prepares the data in the format expected by LSTM models: (samples, time steps, features).
  • Reshaping: The input data is reshaped to ensure compatibility with PyTorch's LSTM layer, which expects a 3D input.
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