<jupyter_start><jupyter_text>Time Series Traffic Analysis<jupyter_code>%matplotlib notebook

import os
import shutil
import urllib3

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Data Loading<jupyter_code>URL = "https://archive.ics.uci.edu/ml/machine-learning-databases/00492/Metro_Interstate_Traffic_Volume.csv.gz"
PATH = "metro_interstate.csv.gz"


def fetch_data(url=URL, path=PATH):
    http = urllib3.PoolManager()
    with open(path, 'wb') as f:
        r = http.request('GET', url, preload_content=False)
        shutil.copyfileobj(r, f)
    

# Only need to run once!
fetch_data()
data = pd.read_csv(
    "metro_interstate.csv.gz",
    index_col="date_time",
    parse_dates=True
)
data.head()
def plot_numeric_features(df=data, cols=["temp", "rain_1h", "snow_1h", "clouds_all", "traffic_volume"]):
    fig, axes = plt.subplots(nrows=len(cols), sharex=True, figsize=(9,6))
    
    for col, ax in zip(cols, axes):
        df[col].plot(ax=ax)
        ax.set_title(col)
        
        if ax == axes[-1]:
            ax.set_xlabel("date")
    
    fig.tight_layout()
    return ax


# Plot features in date range
_ = plot_numeric_features(data.loc["2017-07-23":"2017-07-31"])<jupyter_output><empty_output><jupyter_text>Simple Machine LearningJust computing a regression on traffic volume for the numeric columns<jupyter_code>cols=["temp", "rain_1h", "snow_1h", "clouds_all"]

X = data[cols]
y = data["traffic_volume"]
from sklearn.linear_model import LinearRegression

model = LinearRegression(fit_intercept=True)
model.fit(X, y)
def describe_model(model):
    print(model.get_params())
    print()
    
    for attr in dir(model):
        if attr.endswith("_") and not attr.startswith("_"):
            print(f"{attr} {getattr(model, attr)}")

            
describe_model(model)
from sklearn.model_selection import train_test_split as tts 


X_train, X_test, y_train, y_test = tts(X, y, train_size=0.8)
lrm = LinearRegression(normalize=True).fit(X_train, y_train)
lrm.score(X_test, y_test)
from sklearn.ensemble import RandomForestRegressor

rfm = RandomForestRegressor(n_estimators=10, max_depth=3).fit(X_train, y_train)
describe_model(rfm)
rfm.score(X_test, y_test)
data["holiday_binary"] = [0 if not holiday else 1 for holiday in data["holiday"].values]
data["hour_of_day"] = data.index.hour
cols=["temp", "rain_1h", "snow_1h", "clouds_all", "holiday_binary", "hour_of_day"]

X = data[cols]
y = data["traffic_volume"]
X_train, X_test, y_train, y_test = tts(X, y, train_size=0.8)
lrm = LinearRegression(normalize=True).fit(X_train, y_train)
lrm.score(X_test, y_test)
rfm = RandomForestRegressor(n_estimators=10, max_depth=3).fit(X_train, y_train)
rfm.score(X_test, y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13276231263383298, 'fraction_numerical': 0.015703069236259814, 'mean_word_length': 5.067099567099567, 'pattern_counts': {'":': 1, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '164142', 'license': 'MIT', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1057, 'n_words': 206, 'path': 'demos/20201031.ipynb', 'repo_name': 'georgetown-analytics/machine-learning', 'size': '2802'}