← Back to case study

PROJECT 012 · PYTHON SOURCE

Weather and commute-hour demand

Study-specific code. Shared modules, dependency versions, and reproduction instructions are included in all project files.

Download Python file ↓
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
import numpy as np
import pandas as pd
from scipy.stats import spearmanr
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from portfolio.data import load
from portfolio.constants import *
from portfolio.methods import result, sql, rates, associations, distribution, regression, classification, cluster
from portfolio.engine import execute

META = {'id': 12, 'dataset': 'bikes', 'title': 'Weather and commute-hour demand', 'question': 'How does observed commute-hour demand vary across weather categories and day types?', 'method': 'Restrict to declared commute hours and stratify by working-day status.', 'action': 'Consider weather-aware planning only after obtaining historical forecasts available at decision time.', 'limitations': 'Historical system rentals measure realized use, not unmet demand or station inventory. Missing hours are unknown. Weather associations do not establish causal effects. '}

def analyze():
    df = load('bikes').copy()
    d=df[df.hr.isin([7,8,9,16,17,18])]
    t=d.groupby(['workingday','weathersit']).cnt.agg(observed_hours='size',mean_rentals='mean',median_rentals='median').reset_index()
    t['group']=t.workingday.astype(str)+' / weather '+t.weathersit.astype(str)
    out=result(t,'group','mean_rentals','Rentals per observed hour','Source weather categories are kept as codes. Sample sizes vary; this is a contemporaneous association, not a forecast or causal weather effect.')
    return out

if __name__ == "__main__":
    execute(Path(__file__).parent, META, analyze)