PROJECT 011 · PYTHON SOURCE
Commuter and casual rider mix
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': 11, 'dataset': 'bikes', 'title': 'Commuter and casual rider mix', 'question': 'How does the registered-rider share differ by day type and selected hours?', 'method': 'Aggregate rider counts before calculating shares at six declared hours.', 'action': 'Use rider mix to frame service-planning questions without inferring individual traveler intent.', '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,12,17,18])]
t=d.groupby(['workingday','hr']).agg(registered=('registered','sum'),casual=('casual','sum'),total=('cnt','sum'),observed_hours=('cnt','size')).reset_index()
assert (t.registered+t.casual).equals(t.total)
t['registered_share']=t.registered/t.total;t['group']=t.workingday.astype(str)+' / hour '+t.hr.astype(str)
out=result(t,'group','registered_share','Registered share','Workingday 1 excludes weekends and source holidays. A registered rental is not necessarily a commute.')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)