Skip to content

Traffic data fluctuates constantly or is affected by time. Predicting it can be challenging, but this task will help sharpen your time-series skills. With deep learning, you can use abstract patterns in data that can help boost predictability.

Your task is to build a system that can be applied to help you predict traffic volume or the number of vehicles passing at a specific point and time. Determining this can help reduce road congestion, support new designs for roads or intersections, improve safety, and more! Or, you can use to help plan your commute to avoid traffic!

The dataset provided contains the hourly traffic volume on an interstate highway in Minnesota, USA. It also includes weather features and holidays, which often impact traffic volume.

Time to predict some traffic!

The data:

The dataset is collected and maintained by UCI Machine Learning Repository. The target variable is traffic_volume. The dataset contains the following and has already been normalized and saved into training and test sets:

train_scaled.csv, test_scaled.csv

ColumnTypeDescription
tempNumericAverage temp in kelvin
rain_1hNumericAmount in mm of rain that occurred in the hour
snow_1hNumericAmount in mm of snow that occurred in the hour
clouds_allNumericPercentage of cloud cover
date_timeDateTimeHour of the data collected in local CST time
holiday_ (11 columns)CategoricalUS National holidays plus regional holiday, Minnesota State Fair
weather_main_ (11 columns)CategoricalShort textual description of the current weather
weather_description_ (35 columns)CategoricalLonger textual description of the current weather
traffic_volumeNumericHourly I-94 ATR 301 reported westbound traffic volume
hour_of_dayNumericThe hour of the day
day_of_weekNumericThe day of the week (0=Monday, Sunday=6)
day_of_monthNumericThe day of the month
monthNumericThe number of the month
traffic_volumeNumericHourly I-94 ATR 301 reported westbound traffic volume
# Import the relevant libraries
import numpy as np
import pandas as pd

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader
# Read the traffic data from the CSV training and test files
train_scaled_df = pd.read_csv('train_scaled.csv')
test_scaled_df = pd.read_csv('test_scaled.csv')

# Convert the DataFrame to NumPy arrays
train_scaled = train_scaled_df.to_numpy()
test_scaled = test_scaled_df.to_numpy()
train_scaled[len(train_scaled)-10,train_scaled.shape[1]-1]
seq_length = 15*7 #train by week
train_scaled_df.iloc[0:seq_length,:train_scaled_df.shape[1]-1]
seq_length = int(np.floor(train_scaled_df.groupby(['month','day_of_month']).agg(daily_traffic_records =("traffic_volume","count")).mean()[0]+1))
seq_length
def create_sequence(df, seq_length=seq_length):
    xs = []
    ys = []
    for i in range(df.shape[0] - seq_length):
        x = df.iloc[i:i+seq_length, :df.shape[1]-1]
        y = df.iloc[i+seq_length, df.shape[1]-1]
        xs.append(x)
        ys.append(y)
    return np.array(xs), np.array(ys)

X_train, y_train = create_sequence(train_scaled_df)
train_dataset = TensorDataset(torch.from_numpy(X_train).float(),torch.from_numpy(y_train).float())
train_data_loader = DataLoader(train_dataset,batch_size=64,shuffle=True)

LSTM

class LSTM_MODEL(nn.Module):
    def __init__(self):
        super().__init__()
        self.lstm = nn.LSTM(
            input_size=65,
            hidden_size=16,
            num_layers=2,
            batch_first=True
        )
        self.fc = nn.Linear(16,1)

    def forward(self,x):
        h0 =torch.zeros(self.lstm.num_layers,x.size(0),16)
        c0 =torch.zeros(self.lstm.num_layers,x.size(0),16)
        out,_ = self.lstm(x,(h0,c0))
        out = self.fc(out[:,-1,:])
        return out
# --- FIXED TRAINING CODE ---

traffic_model = LSTM_MODEL()
criterion = nn.HuberLoss()
optimizer = optim.Adam(traffic_model.parameters(), lr=0.05)
num_epochs = 4

for i in range(num_epochs):
    running_loss=0
    losses = []
    for seqs, labels in train_data_loader:
        # Infer the last dimension automatically to avoid shape mismatch
        seqs = seqs.view(seqs.size(0), seqs.size(1), -1)
        outputs = traffic_model(seqs)
        loss = criterion(outputs, labels)
        running_loss += loss.item()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    losses.append(running_loss)
    print(f"Epoch: {i+1}   Running Loss: {running_loss}")
final_training_loss = losses[-1]
X_test, y_test = create_sequence(test_scaled_df)
test_dataset = TensorDataset(torch.from_numpy(X_test).float(),torch.from_numpy(y_test).float())
test_loader = DataLoader(test_dataset,batch_size=64,shuffle=False)