← Back to case study

PROJECT 016 · PYTHON SOURCE

Year-over-year demand comparison

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': 16, 'dataset': 'bikes', 'title': 'Year-over-year demand comparison', 'question': 'How does observed hourly demand compare between corresponding months of the two years?', 'method': 'Month-matched means and observation counts, rather than unadjusted monthly totals.', 'action': 'Investigate system expansion and service availability before attributing observed growth to behavior.', '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()
    t=df.groupby(['mnth','yr']).cnt.agg(mean_rentals='mean',observed_hours='size').unstack('yr')
    t.columns=['mean_2011','mean_2012','hours_2011','hours_2012'];t=t.reset_index()
    t['relative_change']=t.mean_2012/t.mean_2011-1
    out=result(t,'mnth','relative_change','Relative change','Different weather, availability and missing-hour patterns remain confounders. Means use observed hours only.')
    return out

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