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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "4e333c36",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# DBbun Davis — Machine Learning Demo\n",
|
| 9 |
+
"This notebook shows how to build ML models using the synthetic **Davis Square** dataset.\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"We’ll do three examples:\n",
|
| 12 |
+
"1. **Classification:** predict whether a public safety incident is **high severity** using incident, weather, and street features.\n",
|
| 13 |
+
"2. **Regression:** predict **noise (dB)** from weather and traffic context.\n",
|
| 14 |
+
"3. **Clustering:** group streets by **mobility signature** (mode mix) using K-Means.\n",
|
| 15 |
+
"\n",
|
| 16 |
+
"All **figures, tables, and models** are saved locally.\n"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"execution_count": 29,
|
| 22 |
+
"id": "4f2d9fff",
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"outputs": [],
|
| 25 |
+
"source": [
|
| 26 |
+
"# --- User config ---\n",
|
| 27 |
+
"#DATA_DIR = \"./out_dbbun_davis_medium\" # Set to your dataset folder\n",
|
| 28 |
+
"DATA_DIR = \"./\"\n",
|
| 29 |
+
"FIGS_DIR = \"./ml_figs\"\n",
|
| 30 |
+
"TABLES_DIR = \"./ml_tables\"\n",
|
| 31 |
+
"MODELS_DIR = \"./ml_models\"\n",
|
| 32 |
+
"\n",
|
| 33 |
+
"# --- Imports ---\n",
|
| 34 |
+
"import os, json, pickle, re\n",
|
| 35 |
+
"import numpy as np\n",
|
| 36 |
+
"import pandas as pd\n",
|
| 37 |
+
"import matplotlib.pyplot as plt\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"from sklearn.model_selection import train_test_split, cross_val_score\n",
|
| 40 |
+
"from sklearn.preprocessing import OneHotEncoder, StandardScaler\n",
|
| 41 |
+
"from sklearn.compose import ColumnTransformer\n",
|
| 42 |
+
"from sklearn.pipeline import Pipeline\n",
|
| 43 |
+
"from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, RocCurveDisplay, mean_absolute_error, r2_score\n",
|
| 44 |
+
"from sklearn.linear_model import LogisticRegression, LinearRegression\n",
|
| 45 |
+
"from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor\n",
|
| 46 |
+
"from sklearn.cluster import KMeans\n",
|
| 47 |
+
"\n",
|
| 48 |
+
"os.makedirs(FIGS_DIR, exist_ok=True)\n",
|
| 49 |
+
"os.makedirs(TABLES_DIR, exist_ok=True)\n",
|
| 50 |
+
"os.makedirs(MODELS_DIR, exist_ok=True)\n",
|
| 51 |
+
"\n",
|
| 52 |
+
"def savefig(name):\n",
|
| 53 |
+
" path = os.path.join(FIGS_DIR, name)\n",
|
| 54 |
+
" plt.savefig(path, bbox_inches=\"tight\", dpi=144)\n",
|
| 55 |
+
" plt.close()\n",
|
| 56 |
+
" print(f\"Saved figure: {path}\")\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"def savetab(df, name):\n",
|
| 59 |
+
" path = os.path.join(TABLES_DIR, name)\n",
|
| 60 |
+
" df.to_csv(path, index=False)\n",
|
| 61 |
+
" print(f\"Saved table: {path}\")\n",
|
| 62 |
+
"\n",
|
| 63 |
+
"def savemodel(model, name):\n",
|
| 64 |
+
" path = os.path.join(MODELS_DIR, name)\n",
|
| 65 |
+
" with open(path, \"wb\") as f:\n",
|
| 66 |
+
" pickle.dump(model, f)\n",
|
| 67 |
+
" print(f\"Saved model: {path}\")"
|
| 68 |
+
]
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"cell_type": "code",
|
| 72 |
+
"execution_count": 30,
|
| 73 |
+
"id": "fce74eac-3c7b-4f5b-85f5-2e7b0a9fbdb3",
|
| 74 |
+
"metadata": {},
|
| 75 |
+
"outputs": [],
|
| 76 |
+
"source": [
|
| 77 |
+
"import os\n",
|
| 78 |
+
"os.environ[\"OMP_NUM_THREADS\"] = \"1\""
|
| 79 |
+
]
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"cell_type": "code",
|
| 83 |
+
"execution_count": 31,
|
| 84 |
+
"id": "0fd4f78b-44e7-424d-8f1c-3bbc29468cba",
|
| 85 |
+
"metadata": {},
|
| 86 |
+
"outputs": [],
|
| 87 |
+
"source": [
|
| 88 |
+
"import warnings\n",
|
| 89 |
+
"warnings.filterwarnings(\"ignore\", category=FutureWarning, module=\"pandas\")"
|
| 90 |
+
]
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
"cell_type": "code",
|
| 94 |
+
"execution_count": 32,
|
| 95 |
+
"id": "2a2efd9d-75b0-43f4-894b-f7347e3febdb",
|
| 96 |
+
"metadata": {},
|
| 97 |
+
"outputs": [],
|
| 98 |
+
"source": [
|
| 99 |
+
"pd.set_option('future.no_silent_downcasting', True)"
|
| 100 |
+
]
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"cell_type": "code",
|
| 104 |
+
"execution_count": 33,
|
| 105 |
+
"id": "0ac6980b",
|
| 106 |
+
"metadata": {},
|
| 107 |
+
"outputs": [
|
| 108 |
+
{
|
| 109 |
+
"name": "stdout",
|
| 110 |
+
"output_type": "stream",
|
| 111 |
+
"text": [
|
| 112 |
+
"Loaded tables:\n",
|
| 113 |
+
" streets: (15, 7) | safety: (12000, 8) | obs: (35000, 7) | weather: (101136, 7)\n"
|
| 114 |
+
]
|
| 115 |
+
}
|
| 116 |
+
],
|
| 117 |
+
"source": [
|
| 118 |
+
"# Load data\n",
|
| 119 |
+
"def load_csv(name):\n",
|
| 120 |
+
" return pd.read_csv(os.path.join(DATA_DIR, name))\n",
|
| 121 |
+
"\n",
|
| 122 |
+
"# Helper for WKT point -> (lon,lat)\n",
|
| 123 |
+
"def wkt_point_to_lonlat(wkt):\n",
|
| 124 |
+
" m = re.match(r\"POINT\\(([-0-9\\.]+)\\s+([-0-9\\.]+)\\)\", str(wkt))\n",
|
| 125 |
+
" if not m:\n",
|
| 126 |
+
" return np.nan, np.nan\n",
|
| 127 |
+
" lon, lat = float(m.group(1)), float(m.group(2))\n",
|
| 128 |
+
" return lon, lat\n",
|
| 129 |
+
"\n",
|
| 130 |
+
"# Tables\n",
|
| 131 |
+
"streets = load_csv(\"geo_streets.csv\")\n",
|
| 132 |
+
"parks = load_csv(\"geo_parks.csv\")\n",
|
| 133 |
+
"poi = load_csv(\"poi_generic.csv\")\n",
|
| 134 |
+
"households = load_csv(\"households.csv\")\n",
|
| 135 |
+
"pets = load_csv(\"pets_registry.csv\")\n",
|
| 136 |
+
"pet_inc = load_csv(\"pet_incidents.csv\")\n",
|
| 137 |
+
"trips = load_csv(\"mobility_trips.csv\")\n",
|
| 138 |
+
"safety = load_csv(\"public_safety.csv\")\n",
|
| 139 |
+
"events = load_csv(\"events_civic.csv\")\n",
|
| 140 |
+
"obs = load_csv(\"observations.csv\")\n",
|
| 141 |
+
"lines = load_csv(\"transit_lines.csv\")\n",
|
| 142 |
+
"stops = load_csv(\"transit_stops.csv\")\n",
|
| 143 |
+
"ridership = load_csv(\"transit_ridership_daily.csv\")\n",
|
| 144 |
+
"bike = load_csv(\"bike_infra.csv\")\n",
|
| 145 |
+
"traffic = load_csv(\"traffic_counts.csv\")\n",
|
| 146 |
+
"prices = load_csv(\"prices_index.csv\")\n",
|
| 147 |
+
"weather = load_csv(\"weather_daily.csv\")\n",
|
| 148 |
+
"trees = load_csv(\"trees_inventory.csv\")\n",
|
| 149 |
+
"infra = load_csv(\"infrastructure_events.csv\")\n",
|
| 150 |
+
"bissues = load_csv(\"building_issues.csv\")\n",
|
| 151 |
+
"\n",
|
| 152 |
+
"with open(os.path.join(DATA_DIR, \"DATA_DICTIONARY.json\"), \"r\", encoding=\"utf-8\") as f:\n",
|
| 153 |
+
" dd = json.load(f)\n",
|
| 154 |
+
"\n",
|
| 155 |
+
"# Basic sanity\n",
|
| 156 |
+
"print(\"Loaded tables:\")\n",
|
| 157 |
+
"print(\" streets:\", streets.shape, \"| safety:\", safety.shape, \"| obs:\", obs.shape, \"| weather:\", weather.shape)"
|
| 158 |
+
]
|
| 159 |
+
},
|
| 160 |
+
{
|
| 161 |
+
"cell_type": "code",
|
| 162 |
+
"execution_count": 34,
|
| 163 |
+
"id": "2a457810",
|
| 164 |
+
"metadata": {},
|
| 165 |
+
"outputs": [],
|
| 166 |
+
"source": [
|
| 167 |
+
"def nearest_year_join(left_df, left_year_col, right_df, right_year_col, on_key_col):\n",
|
| 168 |
+
" # For each (on_key_col, year) in left_df, find the right_df row with smallest |year diff|\n",
|
| 169 |
+
" # This is a simple but effective approach for decimated time series.\n",
|
| 170 |
+
" right_sorted = right_df.sort_values([on_key_col, right_year_col]).copy()\n",
|
| 171 |
+
" out_rows = []\n",
|
| 172 |
+
" for (k, y), block in left_df.groupby([on_key_col, left_year_col]):\n",
|
| 173 |
+
" cand = right_sorted[right_sorted[on_key_col]==k]\n",
|
| 174 |
+
" if cand.empty:\n",
|
| 175 |
+
" # no match for this key\n",
|
| 176 |
+
" tmp = block.copy()\n",
|
| 177 |
+
" tmp[\"__ry\"] = np.nan\n",
|
| 178 |
+
" tmp[\"__ridx\"] = -1\n",
|
| 179 |
+
" out_rows.append(tmp)\n",
|
| 180 |
+
" continue\n",
|
| 181 |
+
" # pick nearest by year\n",
|
| 182 |
+
" ridx = int((cand[right_year_col] - y).abs().idxmin())\n",
|
| 183 |
+
" tmp = block.copy()\n",
|
| 184 |
+
" tmp[\"__ry\"] = cand.loc[ridx, right_year_col]\n",
|
| 185 |
+
" tmp[\"__ridx\"] = ridx\n",
|
| 186 |
+
" out_rows.append(tmp)\n",
|
| 187 |
+
" joined_left = pd.concat(out_rows, axis=0)\n",
|
| 188 |
+
"\n",
|
| 189 |
+
" # Attach right_df columns by __ridx where available\n",
|
| 190 |
+
" joined = joined_left.merge(right_df.add_prefix(\"r__\"), left_on=\"__ridx\", right_index=True, how=\"left\")\n",
|
| 191 |
+
" joined = joined.drop(columns=[\"__ridx\"])\n",
|
| 192 |
+
" return joined"
|
| 193 |
+
]
|
| 194 |
+
},
|
| 195 |
+
{
|
| 196 |
+
"cell_type": "markdown",
|
| 197 |
+
"id": "ff73a6ff",
|
| 198 |
+
"metadata": {},
|
| 199 |
+
"source": [
|
| 200 |
+
"## 1) Classification: Predict high-severity public safety incidents\n",
|
| 201 |
+
"**Target:** `severity` mapped to binary: *high* vs *not-high* \n",
|
| 202 |
+
"**Features:** incident `category`, `reported_by`, street attributes (bike infra presence), daily weather (`temp_c`, `precip_mm`, `snow_cm`, `wind_kph`, `heatwave`)."
|
| 203 |
+
]
|
| 204 |
+
},
|
| 205 |
+
{
|
| 206 |
+
"cell_type": "code",
|
| 207 |
+
"execution_count": 35,
|
| 208 |
+
"id": "0a9bb0f2",
|
| 209 |
+
"metadata": {},
|
| 210 |
+
"outputs": [
|
| 211 |
+
{
|
| 212 |
+
"name": "stdout",
|
| 213 |
+
"output_type": "stream",
|
| 214 |
+
"text": [
|
| 215 |
+
" precision recall f1-score support\n",
|
| 216 |
+
"\n",
|
| 217 |
+
" 0 0.67 0.82 0.74 2026\n",
|
| 218 |
+
" 1 0.30 0.16 0.21 974\n",
|
| 219 |
+
"\n",
|
| 220 |
+
" accuracy 0.61 3000\n",
|
| 221 |
+
" macro avg 0.49 0.49 0.47 3000\n",
|
| 222 |
+
"weighted avg 0.55 0.61 0.57 3000\n",
|
| 223 |
+
"\n",
|
| 224 |
+
"Confusion matrix:\n",
|
| 225 |
+
" [[1671 355]\n",
|
| 226 |
+
" [ 820 154]]\n",
|
| 227 |
+
"ROC AUC: 0.4951799603106231\n",
|
| 228 |
+
"Saved figure: ./ml_figs\\clf_roc_curve.png\n",
|
| 229 |
+
"Saved model: ./ml_models\\incident_severity_rf.pkl\n",
|
| 230 |
+
"Saved table: ./ml_tables\\clf_metrics_incident_severity.csv\n"
|
| 231 |
+
]
|
| 232 |
+
}
|
| 233 |
+
],
|
| 234 |
+
"source": [
|
| 235 |
+
"# Prepare safety data\n",
|
| 236 |
+
"safety['date'] = pd.to_datetime(safety['date'])\n",
|
| 237 |
+
"safety['year'] = safety['date'].dt.year\n",
|
| 238 |
+
"safety['month'] = safety['date'].dt.month\n",
|
| 239 |
+
"safety['day'] = safety['date'].dt.day\n",
|
| 240 |
+
"\n",
|
| 241 |
+
"# Target\n",
|
| 242 |
+
"y = (safety['severity'] == 'high').astype(int)\n",
|
| 243 |
+
"\n",
|
| 244 |
+
"# Merge weather on date\n",
|
| 245 |
+
"weather['date'] = pd.to_datetime(weather['date'])\n",
|
| 246 |
+
"s_w = safety.merge(weather[['date','temp_c','precip_mm','snow_cm','wind_kph','heatwave']], on='date', how='left')\n",
|
| 247 |
+
"\n",
|
| 248 |
+
"# Street-level bike infra presence (binary, ever installed before safety year)\n",
|
| 249 |
+
"bike_any = bike.copy()\n",
|
| 250 |
+
"bike_any['has_bike_infra'] = True\n",
|
| 251 |
+
"s_w = s_w.merge(\n",
|
| 252 |
+
" bike_any[['street_id','has_bike_infra']].drop_duplicates('street_id'),\n",
|
| 253 |
+
" left_on='location_street_id', right_on='street_id', how='left'\n",
|
| 254 |
+
")\n",
|
| 255 |
+
"s_w['has_bike_infra'] = s_w['has_bike_infra'].fillna(False).infer_objects(copy=False)\n",
|
| 256 |
+
"\n",
|
| 257 |
+
"# Assemble features\n",
|
| 258 |
+
"X = s_w[['category','reported_by','has_bike_infra','temp_c','precip_mm','snow_cm','wind_kph','heatwave','month']].copy()\n",
|
| 259 |
+
"\n",
|
| 260 |
+
"# Split numeric/categorical\n",
|
| 261 |
+
"num_cols = ['temp_c','precip_mm','snow_cm','wind_kph','month']\n",
|
| 262 |
+
"cat_cols = ['category','reported_by','heatwave','has_bike_infra']\n",
|
| 263 |
+
"\n",
|
| 264 |
+
"pre = ColumnTransformer([\n",
|
| 265 |
+
" (\"num\", StandardScaler(), num_cols),\n",
|
| 266 |
+
" (\"cat\", OneHotEncoder(handle_unknown=\"ignore\"), cat_cols)\n",
|
| 267 |
+
"])\n",
|
| 268 |
+
"\n",
|
| 269 |
+
"clf = Pipeline([\n",
|
| 270 |
+
" (\"pre\", pre),\n",
|
| 271 |
+
" (\"rf\", RandomForestClassifier(n_estimators=200, random_state=42, class_weight=\"balanced\"))\n",
|
| 272 |
+
"])\n",
|
| 273 |
+
"\n",
|
| 274 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)\n",
|
| 275 |
+
"clf.fit(X_train, y_train)\n",
|
| 276 |
+
"\n",
|
| 277 |
+
"# Evaluate\n",
|
| 278 |
+
"pred = clf.predict(X_test)\n",
|
| 279 |
+
"proba = clf.predict_proba(X_test)[:,1]\n",
|
| 280 |
+
"\n",
|
| 281 |
+
"print(classification_report(y_test, pred))\n",
|
| 282 |
+
"cm = confusion_matrix(y_test, pred)\n",
|
| 283 |
+
"print(\"Confusion matrix:\\n\", cm)\n",
|
| 284 |
+
"\n",
|
| 285 |
+
"# ROC AUC & curve\n",
|
| 286 |
+
"auc = roc_auc_score(y_test, proba)\n",
|
| 287 |
+
"print(\"ROC AUC:\", auc)\n",
|
| 288 |
+
"\n",
|
| 289 |
+
"disp = RocCurveDisplay.from_predictions(y_test, proba)\n",
|
| 290 |
+
"plt.title(\"ROC Curve — High Severity Incident Classifier\")\n",
|
| 291 |
+
"savefig(\"clf_roc_curve.png\")\n",
|
| 292 |
+
"\n",
|
| 293 |
+
"# Save model\n",
|
| 294 |
+
"savemodel(clf, \"incident_severity_rf.pkl\")\n",
|
| 295 |
+
"\n",
|
| 296 |
+
"# Save metrics as table\n",
|
| 297 |
+
"metrics_tbl = pd.DataFrame({\n",
|
| 298 |
+
" \"roc_auc\":[auc],\n",
|
| 299 |
+
" \"accuracy\":[(pred==y_test).mean()],\n",
|
| 300 |
+
" \"n_test\":[len(y_test)]\n",
|
| 301 |
+
"})\n",
|
| 302 |
+
"savetab(metrics_tbl, \"clf_metrics_incident_severity.csv\")"
|
| 303 |
+
]
|
| 304 |
+
},
|
| 305 |
+
{
|
| 306 |
+
"cell_type": "markdown",
|
| 307 |
+
"id": "78159761",
|
| 308 |
+
"metadata": {},
|
| 309 |
+
"source": [
|
| 310 |
+
"## 2) Regression: Predict street noise (dB)\n",
|
| 311 |
+
"**Target:** `observations` where `metric == \"noise_db\"` → `value` (dB). \n",
|
| 312 |
+
"**Features:** weather (temp, wind, rain, snow, heatwave), month, traffic volumes (nearest-year by street).\n"
|
| 313 |
+
]
|
| 314 |
+
},
|
| 315 |
+
{
|
| 316 |
+
"cell_type": "code",
|
| 317 |
+
"execution_count": 36,
|
| 318 |
+
"id": "bb03cb92",
|
| 319 |
+
"metadata": {},
|
| 320 |
+
"outputs": [
|
| 321 |
+
{
|
| 322 |
+
"name": "stdout",
|
| 323 |
+
"output_type": "stream",
|
| 324 |
+
"text": [
|
| 325 |
+
"MAE: 5.521102874867018 R^2: 0.3084211030160505\n",
|
| 326 |
+
"Saved figure: ./ml_figs\\regr_noise_pred_vs_actual.png\n",
|
| 327 |
+
"Saved model: ./ml_models\\noise_rf.pkl\n",
|
| 328 |
+
"Saved table: ./ml_tables\\regr_noise_metrics.csv\n"
|
| 329 |
+
]
|
| 330 |
+
}
|
| 331 |
+
],
|
| 332 |
+
"source": [
|
| 333 |
+
"# Filter observations to noise\n",
|
| 334 |
+
"noise = obs[obs['metric']=='noise_db'].copy()\n",
|
| 335 |
+
"noise['date'] = pd.to_datetime(noise['date'])\n",
|
| 336 |
+
"noise['year'] = noise['date'].dt.year\n",
|
| 337 |
+
"noise['month'] = noise['date'].dt.month\n",
|
| 338 |
+
"\n",
|
| 339 |
+
"# Merge weather\n",
|
| 340 |
+
"weather['date'] = pd.to_datetime(weather['date'])\n",
|
| 341 |
+
"nz = noise.merge(weather[['date','temp_c','precip_mm','snow_cm','wind_kph','heatwave']], on='date', how='left')\n",
|
| 342 |
+
"\n",
|
| 343 |
+
"# Traffic is every ~5 years; map to nearest year by street\n",
|
| 344 |
+
"traffic['year'] = pd.to_datetime(traffic['date']).dt.year\n",
|
| 345 |
+
"nz_key = nz[['location_street_id','year']].copy()\n",
|
| 346 |
+
"nz_key = nz_key.rename(columns={'location_street_id':'street_id'})\n",
|
| 347 |
+
"traffic_near = nearest_year_join(nz_key, 'year', traffic[['street_id','year','vehicles_per_day','bikes_per_day','peds_per_day']], 'year', 'street_id')\n",
|
| 348 |
+
"\n",
|
| 349 |
+
"# Merge traffic back by index alignment\n",
|
| 350 |
+
"nz = nz.join(traffic_near[['r__street_id','r__year','r__vehicles_per_day','r__bikes_per_day','r__peds_per_day']].reset_index(drop=True))\n",
|
| 351 |
+
"# If missing traffic (e.g., older years), fill with medians\n",
|
| 352 |
+
"for c in ['r__vehicles_per_day','r__bikes_per_day','r__peds_per_day']:\n",
|
| 353 |
+
" nz[c] = nz[c].fillna(nz[c].median())\n",
|
| 354 |
+
"\n",
|
| 355 |
+
"# Prepare features/target\n",
|
| 356 |
+
"y = nz['value'].astype(float) # noise dB\n",
|
| 357 |
+
"X = nz[['temp_c','precip_mm','snow_cm','wind_kph','heatwave','month','r__vehicles_per_day','r__bikes_per_day','r__peds_per_day']].copy()\n",
|
| 358 |
+
"\n",
|
| 359 |
+
"num_cols = ['temp_c','precip_mm','snow_cm','wind_kph','month','r__vehicles_per_day','r__bikes_per_day','r__peds_per_day']\n",
|
| 360 |
+
"cat_cols = ['heatwave']\n",
|
| 361 |
+
"\n",
|
| 362 |
+
"pre = ColumnTransformer([\n",
|
| 363 |
+
" (\"num\", StandardScaler(), num_cols),\n",
|
| 364 |
+
" (\"cat\", OneHotEncoder(handle_unknown=\"ignore\"), cat_cols)\n",
|
| 365 |
+
"])\n",
|
| 366 |
+
"\n",
|
| 367 |
+
"regr = Pipeline([\n",
|
| 368 |
+
" (\"pre\", pre),\n",
|
| 369 |
+
" (\"rf\", RandomForestRegressor(n_estimators=200, random_state=42))\n",
|
| 370 |
+
"])\n",
|
| 371 |
+
"\n",
|
| 372 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n",
|
| 373 |
+
"regr.fit(X_train, y_train)\n",
|
| 374 |
+
"pred = regr.predict(X_test)\n",
|
| 375 |
+
"\n",
|
| 376 |
+
"mae = mean_absolute_error(y_test, pred)\n",
|
| 377 |
+
"r2 = r2_score(y_test, pred)\n",
|
| 378 |
+
"print(\"MAE:\", mae, \" R^2:\", r2)\n",
|
| 379 |
+
"\n",
|
| 380 |
+
"# Scatter: predicted vs actual\n",
|
| 381 |
+
"plt.figure(figsize=(6,6))\n",
|
| 382 |
+
"plt.scatter(y_test, pred, alpha=0.3)\n",
|
| 383 |
+
"plt.xlabel(\"Actual dB\"); plt.ylabel(\"Predicted dB\")\n",
|
| 384 |
+
"plt.title(\"Noise Regression — Predicted vs Actual\")\n",
|
| 385 |
+
"savefig(\"regr_noise_pred_vs_actual.png\")\n",
|
| 386 |
+
"\n",
|
| 387 |
+
"# Save model & metrics\n",
|
| 388 |
+
"savemodel(regr, \"noise_rf.pkl\")\n",
|
| 389 |
+
"savetab(pd.DataFrame({\"MAE\":[mae], \"R2\":[r2], \"n_test\":[len(y_test)]}), \"regr_noise_metrics.csv\")"
|
| 390 |
+
]
|
| 391 |
+
},
|
| 392 |
+
{
|
| 393 |
+
"cell_type": "markdown",
|
| 394 |
+
"id": "11a13962",
|
| 395 |
+
"metadata": {},
|
| 396 |
+
"source": [
|
| 397 |
+
"## 3) Clustering: Streets by mobility signature\n",
|
| 398 |
+
"We compute, for each street, a **mode share vector** based on trips that originate on that street. \n",
|
| 399 |
+
"Then we cluster streets using **K-Means** and visualize cluster sizes."
|
| 400 |
+
]
|
| 401 |
+
},
|
| 402 |
+
{
|
| 403 |
+
"cell_type": "code",
|
| 404 |
+
"execution_count": 37,
|
| 405 |
+
"id": "8b77b4d6",
|
| 406 |
+
"metadata": {},
|
| 407 |
+
"outputs": [
|
| 408 |
+
{
|
| 409 |
+
"name": "stderr",
|
| 410 |
+
"output_type": "stream",
|
| 411 |
+
"text": [
|
| 412 |
+
"C:\\Users\\karto\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1429: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n",
|
| 413 |
+
" warnings.warn(\n",
|
| 414 |
+
"C:\\Users\\karto\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1429: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n",
|
| 415 |
+
" warnings.warn(\n",
|
| 416 |
+
"C:\\Users\\karto\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1429: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n",
|
| 417 |
+
" warnings.warn(\n",
|
| 418 |
+
"C:\\Users\\karto\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1429: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n",
|
| 419 |
+
" warnings.warn(\n",
|
| 420 |
+
"C:\\Users\\karto\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1429: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n",
|
| 421 |
+
" warnings.warn(\n",
|
| 422 |
+
"C:\\Users\\karto\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1429: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n",
|
| 423 |
+
" warnings.warn(\n",
|
| 424 |
+
"C:\\Users\\karto\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1429: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n",
|
| 425 |
+
" warnings.warn(\n"
|
| 426 |
+
]
|
| 427 |
+
},
|
| 428 |
+
{
|
| 429 |
+
"name": "stdout",
|
| 430 |
+
"output_type": "stream",
|
| 431 |
+
"text": [
|
| 432 |
+
"Saved figure: ./ml_figs\\cluster_elbow.png\n",
|
| 433 |
+
"Saved table: ./ml_tables\\cluster_sizes.csv\n"
|
| 434 |
+
]
|
| 435 |
+
},
|
| 436 |
+
{
|
| 437 |
+
"name": "stderr",
|
| 438 |
+
"output_type": "stream",
|
| 439 |
+
"text": [
|
| 440 |
+
"C:\\Users\\karto\\anaconda3\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1429: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n",
|
| 441 |
+
" warnings.warn(\n"
|
| 442 |
+
]
|
| 443 |
+
},
|
| 444 |
+
{
|
| 445 |
+
"data": {
|
| 446 |
+
"text/html": [
|
| 447 |
+
"<div>\n",
|
| 448 |
+
"<style scoped>\n",
|
| 449 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 450 |
+
" vertical-align: middle;\n",
|
| 451 |
+
" }\n",
|
| 452 |
+
"\n",
|
| 453 |
+
" .dataframe tbody tr th {\n",
|
| 454 |
+
" vertical-align: top;\n",
|
| 455 |
+
" }\n",
|
| 456 |
+
"\n",
|
| 457 |
+
" .dataframe thead th {\n",
|
| 458 |
+
" text-align: right;\n",
|
| 459 |
+
" }\n",
|
| 460 |
+
"</style>\n",
|
| 461 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 462 |
+
" <thead>\n",
|
| 463 |
+
" <tr style=\"text-align: right;\">\n",
|
| 464 |
+
" <th></th>\n",
|
| 465 |
+
" <th>cluster</th>\n",
|
| 466 |
+
" <th>n_streets</th>\n",
|
| 467 |
+
" </tr>\n",
|
| 468 |
+
" </thead>\n",
|
| 469 |
+
" <tbody>\n",
|
| 470 |
+
" <tr>\n",
|
| 471 |
+
" <th>0</th>\n",
|
| 472 |
+
" <td>0</td>\n",
|
| 473 |
+
" <td>3</td>\n",
|
| 474 |
+
" </tr>\n",
|
| 475 |
+
" <tr>\n",
|
| 476 |
+
" <th>1</th>\n",
|
| 477 |
+
" <td>1</td>\n",
|
| 478 |
+
" <td>2</td>\n",
|
| 479 |
+
" </tr>\n",
|
| 480 |
+
" <tr>\n",
|
| 481 |
+
" <th>2</th>\n",
|
| 482 |
+
" <td>2</td>\n",
|
| 483 |
+
" <td>5</td>\n",
|
| 484 |
+
" </tr>\n",
|
| 485 |
+
" <tr>\n",
|
| 486 |
+
" <th>3</th>\n",
|
| 487 |
+
" <td>3</td>\n",
|
| 488 |
+
" <td>5</td>\n",
|
| 489 |
+
" </tr>\n",
|
| 490 |
+
" </tbody>\n",
|
| 491 |
+
"</table>\n",
|
| 492 |
+
"</div>"
|
| 493 |
+
],
|
| 494 |
+
"text/plain": [
|
| 495 |
+
" cluster n_streets\n",
|
| 496 |
+
"0 0 3\n",
|
| 497 |
+
"1 1 2\n",
|
| 498 |
+
"2 2 5\n",
|
| 499 |
+
"3 3 5"
|
| 500 |
+
]
|
| 501 |
+
},
|
| 502 |
+
"metadata": {},
|
| 503 |
+
"output_type": "display_data"
|
| 504 |
+
},
|
| 505 |
+
{
|
| 506 |
+
"name": "stdout",
|
| 507 |
+
"output_type": "stream",
|
| 508 |
+
"text": [
|
| 509 |
+
"Saved figure: ./ml_figs\\cluster_sizes_bar.png\n",
|
| 510 |
+
"Saved table: ./ml_tables\\cluster_assignments.csv\n"
|
| 511 |
+
]
|
| 512 |
+
}
|
| 513 |
+
],
|
| 514 |
+
"source": [
|
| 515 |
+
"# Build mode share per street (origin)\n",
|
| 516 |
+
"mode_pvt = trips.pivot_table(index='origin_street_id', columns='mode', values='trip_id', aggfunc='count', fill_value=0)\n",
|
| 517 |
+
"mode_share = mode_pvt.div(mode_pvt.sum(axis=1).replace(0,1), axis=0)\n",
|
| 518 |
+
"\n",
|
| 519 |
+
"# Try several k and pick a reasonable one (elbow heuristic)\n",
|
| 520 |
+
"inertias = []\n",
|
| 521 |
+
"ks = list(range(2, 9))\n",
|
| 522 |
+
"for k in ks:\n",
|
| 523 |
+
" km = KMeans(n_clusters=k, random_state=42, n_init=10)\n",
|
| 524 |
+
" km.fit(mode_share)\n",
|
| 525 |
+
" inertias.append(km.inertia_)\n",
|
| 526 |
+
"\n",
|
| 527 |
+
"plt.figure(figsize=(6,4))\n",
|
| 528 |
+
"plt.plot(ks, inertias, marker=\"o\")\n",
|
| 529 |
+
"plt.xlabel(\"k\"); plt.ylabel(\"Inertia\")\n",
|
| 530 |
+
"plt.title(\"K-Means Elbow — Mobility Mode Share by Street\")\n",
|
| 531 |
+
"savefig(\"cluster_elbow.png\")\n",
|
| 532 |
+
"\n",
|
| 533 |
+
"# Choose k=4 as a reasonable choice for demonstration\n",
|
| 534 |
+
"k = 4\n",
|
| 535 |
+
"km = KMeans(n_clusters=k, random_state=42, n_init=10)\n",
|
| 536 |
+
"labels = km.fit_predict(mode_share)\n",
|
| 537 |
+
"mode_share_clusters = mode_share.copy()\n",
|
| 538 |
+
"mode_share_clusters['cluster'] = labels\n",
|
| 539 |
+
"\n",
|
| 540 |
+
"# Summarize clusters\n",
|
| 541 |
+
"cluster_sizes = mode_share_clusters['cluster'].value_counts().sort_index().reset_index()\n",
|
| 542 |
+
"cluster_sizes.columns = ['cluster','n_streets']\n",
|
| 543 |
+
"savetab(cluster_sizes, \"cluster_sizes.csv\")\n",
|
| 544 |
+
"display(cluster_sizes)\n",
|
| 545 |
+
"\n",
|
| 546 |
+
"plt.figure(figsize=(6,4))\n",
|
| 547 |
+
"plt.bar(cluster_sizes['cluster'], cluster_sizes['n_streets'])\n",
|
| 548 |
+
"plt.xlabel(\"Cluster\"); plt.ylabel(\"Number of streets\")\n",
|
| 549 |
+
"plt.title(\"Street Clusters by Mobility Signature\")\n",
|
| 550 |
+
"savefig(\"cluster_sizes_bar.png\")\n",
|
| 551 |
+
"\n",
|
| 552 |
+
"# Save cluster assignments\n",
|
| 553 |
+
"assignments = mode_share_clusters.reset_index()[['origin_street_id','cluster']]\n",
|
| 554 |
+
"savetab(assignments, \"cluster_assignments.csv\")"
|
| 555 |
+
]
|
| 556 |
+
},
|
| 557 |
+
{
|
| 558 |
+
"cell_type": "markdown",
|
| 559 |
+
"id": "9dc1e2af",
|
| 560 |
+
"metadata": {},
|
| 561 |
+
"source": [
|
| 562 |
+
"### Bonus: Interpreting models\n",
|
| 563 |
+
"As a quick interpretability pass, we can look at feature importance from the random forests (MDI)."
|
| 564 |
+
]
|
| 565 |
+
},
|
| 566 |
+
{
|
| 567 |
+
"cell_type": "code",
|
| 568 |
+
"execution_count": 38,
|
| 569 |
+
"id": "73f43632",
|
| 570 |
+
"metadata": {},
|
| 571 |
+
"outputs": [
|
| 572 |
+
{
|
| 573 |
+
"data": {
|
| 574 |
+
"text/html": [
|
| 575 |
+
"<div>\n",
|
| 576 |
+
"<style scoped>\n",
|
| 577 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 578 |
+
" vertical-align: middle;\n",
|
| 579 |
+
" }\n",
|
| 580 |
+
"\n",
|
| 581 |
+
" .dataframe tbody tr th {\n",
|
| 582 |
+
" vertical-align: top;\n",
|
| 583 |
+
" }\n",
|
| 584 |
+
"\n",
|
| 585 |
+
" .dataframe thead th {\n",
|
| 586 |
+
" text-align: right;\n",
|
| 587 |
+
" }\n",
|
| 588 |
+
"</style>\n",
|
| 589 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 590 |
+
" <thead>\n",
|
| 591 |
+
" <tr style=\"text-align: right;\">\n",
|
| 592 |
+
" <th></th>\n",
|
| 593 |
+
" <th>feature</th>\n",
|
| 594 |
+
" <th>importance</th>\n",
|
| 595 |
+
" </tr>\n",
|
| 596 |
+
" </thead>\n",
|
| 597 |
+
" <tbody>\n",
|
| 598 |
+
" <tr>\n",
|
| 599 |
+
" <th>0</th>\n",
|
| 600 |
+
" <td>temp_c</td>\n",
|
| 601 |
+
" <td>0.302091</td>\n",
|
| 602 |
+
" </tr>\n",
|
| 603 |
+
" <tr>\n",
|
| 604 |
+
" <th>1</th>\n",
|
| 605 |
+
" <td>wind_kph</td>\n",
|
| 606 |
+
" <td>0.219432</td>\n",
|
| 607 |
+
" </tr>\n",
|
| 608 |
+
" <tr>\n",
|
| 609 |
+
" <th>2</th>\n",
|
| 610 |
+
" <td>month</td>\n",
|
| 611 |
+
" <td>0.188248</td>\n",
|
| 612 |
+
" </tr>\n",
|
| 613 |
+
" <tr>\n",
|
| 614 |
+
" <th>3</th>\n",
|
| 615 |
+
" <td>precip_mm</td>\n",
|
| 616 |
+
" <td>0.082657</td>\n",
|
| 617 |
+
" </tr>\n",
|
| 618 |
+
" <tr>\n",
|
| 619 |
+
" <th>4</th>\n",
|
| 620 |
+
" <td>snow_cm</td>\n",
|
| 621 |
+
" <td>0.044882</td>\n",
|
| 622 |
+
" </tr>\n",
|
| 623 |
+
" <tr>\n",
|
| 624 |
+
" <th>5</th>\n",
|
| 625 |
+
" <td>reported_by_sensor</td>\n",
|
| 626 |
+
" <td>0.017501</td>\n",
|
| 627 |
+
" </tr>\n",
|
| 628 |
+
" <tr>\n",
|
| 629 |
+
" <th>6</th>\n",
|
| 630 |
+
" <td>reported_by_officer</td>\n",
|
| 631 |
+
" <td>0.016649</td>\n",
|
| 632 |
+
" </tr>\n",
|
| 633 |
+
" <tr>\n",
|
| 634 |
+
" <th>7</th>\n",
|
| 635 |
+
" <td>reported_by_citizen</td>\n",
|
| 636 |
+
" <td>0.016428</td>\n",
|
| 637 |
+
" </tr>\n",
|
| 638 |
+
" <tr>\n",
|
| 639 |
+
" <th>8</th>\n",
|
| 640 |
+
" <td>category_animal</td>\n",
|
| 641 |
+
" <td>0.012986</td>\n",
|
| 642 |
+
" </tr>\n",
|
| 643 |
+
" <tr>\n",
|
| 644 |
+
" <th>9</th>\n",
|
| 645 |
+
" <td>category_theft</td>\n",
|
| 646 |
+
" <td>0.012825</td>\n",
|
| 647 |
+
" </tr>\n",
|
| 648 |
+
" <tr>\n",
|
| 649 |
+
" <th>10</th>\n",
|
| 650 |
+
" <td>category_disturbance</td>\n",
|
| 651 |
+
" <td>0.012002</td>\n",
|
| 652 |
+
" </tr>\n",
|
| 653 |
+
" <tr>\n",
|
| 654 |
+
" <th>11</th>\n",
|
| 655 |
+
" <td>category_noise</td>\n",
|
| 656 |
+
" <td>0.011956</td>\n",
|
| 657 |
+
" </tr>\n",
|
| 658 |
+
" <tr>\n",
|
| 659 |
+
" <th>12</th>\n",
|
| 660 |
+
" <td>category_other</td>\n",
|
| 661 |
+
" <td>0.011694</td>\n",
|
| 662 |
+
" </tr>\n",
|
| 663 |
+
" <tr>\n",
|
| 664 |
+
" <th>13</th>\n",
|
| 665 |
+
" <td>category_vandalism</td>\n",
|
| 666 |
+
" <td>0.010977</td>\n",
|
| 667 |
+
" </tr>\n",
|
| 668 |
+
" <tr>\n",
|
| 669 |
+
" <th>14</th>\n",
|
| 670 |
+
" <td>category_traffic</td>\n",
|
| 671 |
+
" <td>0.010530</td>\n",
|
| 672 |
+
" </tr>\n",
|
| 673 |
+
" <tr>\n",
|
| 674 |
+
" <th>15</th>\n",
|
| 675 |
+
" <td>has_bike_infra_True</td>\n",
|
| 676 |
+
" <td>0.010157</td>\n",
|
| 677 |
+
" </tr>\n",
|
| 678 |
+
" <tr>\n",
|
| 679 |
+
" <th>16</th>\n",
|
| 680 |
+
" <td>has_bike_infra_False</td>\n",
|
| 681 |
+
" <td>0.009653</td>\n",
|
| 682 |
+
" </tr>\n",
|
| 683 |
+
" <tr>\n",
|
| 684 |
+
" <th>17</th>\n",
|
| 685 |
+
" <td>heatwave_False</td>\n",
|
| 686 |
+
" <td>0.004153</td>\n",
|
| 687 |
+
" </tr>\n",
|
| 688 |
+
" <tr>\n",
|
| 689 |
+
" <th>18</th>\n",
|
| 690 |
+
" <td>heatwave_nan</td>\n",
|
| 691 |
+
" <td>0.002672</td>\n",
|
| 692 |
+
" </tr>\n",
|
| 693 |
+
" <tr>\n",
|
| 694 |
+
" <th>19</th>\n",
|
| 695 |
+
" <td>heatwave_True</td>\n",
|
| 696 |
+
" <td>0.002508</td>\n",
|
| 697 |
+
" </tr>\n",
|
| 698 |
+
" </tbody>\n",
|
| 699 |
+
"</table>\n",
|
| 700 |
+
"</div>"
|
| 701 |
+
],
|
| 702 |
+
"text/plain": [
|
| 703 |
+
" feature importance\n",
|
| 704 |
+
"0 temp_c 0.302091\n",
|
| 705 |
+
"1 wind_kph 0.219432\n",
|
| 706 |
+
"2 month 0.188248\n",
|
| 707 |
+
"3 precip_mm 0.082657\n",
|
| 708 |
+
"4 snow_cm 0.044882\n",
|
| 709 |
+
"5 reported_by_sensor 0.017501\n",
|
| 710 |
+
"6 reported_by_officer 0.016649\n",
|
| 711 |
+
"7 reported_by_citizen 0.016428\n",
|
| 712 |
+
"8 category_animal 0.012986\n",
|
| 713 |
+
"9 category_theft 0.012825\n",
|
| 714 |
+
"10 category_disturbance 0.012002\n",
|
| 715 |
+
"11 category_noise 0.011956\n",
|
| 716 |
+
"12 category_other 0.011694\n",
|
| 717 |
+
"13 category_vandalism 0.010977\n",
|
| 718 |
+
"14 category_traffic 0.010530\n",
|
| 719 |
+
"15 has_bike_infra_True 0.010157\n",
|
| 720 |
+
"16 has_bike_infra_False 0.009653\n",
|
| 721 |
+
"17 heatwave_False 0.004153\n",
|
| 722 |
+
"18 heatwave_nan 0.002672\n",
|
| 723 |
+
"19 heatwave_True 0.002508"
|
| 724 |
+
]
|
| 725 |
+
},
|
| 726 |
+
"metadata": {},
|
| 727 |
+
"output_type": "display_data"
|
| 728 |
+
},
|
| 729 |
+
{
|
| 730 |
+
"name": "stdout",
|
| 731 |
+
"output_type": "stream",
|
| 732 |
+
"text": [
|
| 733 |
+
"Saved table: ./ml_tables\\clf_top_feature_importances.csv\n",
|
| 734 |
+
"Saved figure: ./ml_figs\\clf_feature_importances.png\n"
|
| 735 |
+
]
|
| 736 |
+
},
|
| 737 |
+
{
|
| 738 |
+
"data": {
|
| 739 |
+
"text/html": [
|
| 740 |
+
"<div>\n",
|
| 741 |
+
"<style scoped>\n",
|
| 742 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 743 |
+
" vertical-align: middle;\n",
|
| 744 |
+
" }\n",
|
| 745 |
+
"\n",
|
| 746 |
+
" .dataframe tbody tr th {\n",
|
| 747 |
+
" vertical-align: top;\n",
|
| 748 |
+
" }\n",
|
| 749 |
+
"\n",
|
| 750 |
+
" .dataframe thead th {\n",
|
| 751 |
+
" text-align: right;\n",
|
| 752 |
+
" }\n",
|
| 753 |
+
"</style>\n",
|
| 754 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 755 |
+
" <thead>\n",
|
| 756 |
+
" <tr style=\"text-align: right;\">\n",
|
| 757 |
+
" <th></th>\n",
|
| 758 |
+
" <th>feature</th>\n",
|
| 759 |
+
" <th>importance</th>\n",
|
| 760 |
+
" </tr>\n",
|
| 761 |
+
" </thead>\n",
|
| 762 |
+
" <tbody>\n",
|
| 763 |
+
" <tr>\n",
|
| 764 |
+
" <th>0</th>\n",
|
| 765 |
+
" <td>precip_mm</td>\n",
|
| 766 |
+
" <td>0.231075</td>\n",
|
| 767 |
+
" </tr>\n",
|
| 768 |
+
" <tr>\n",
|
| 769 |
+
" <th>1</th>\n",
|
| 770 |
+
" <td>temp_c</td>\n",
|
| 771 |
+
" <td>0.223292</td>\n",
|
| 772 |
+
" </tr>\n",
|
| 773 |
+
" <tr>\n",
|
| 774 |
+
" <th>2</th>\n",
|
| 775 |
+
" <td>snow_cm</td>\n",
|
| 776 |
+
" <td>0.115154</td>\n",
|
| 777 |
+
" </tr>\n",
|
| 778 |
+
" <tr>\n",
|
| 779 |
+
" <th>3</th>\n",
|
| 780 |
+
" <td>wind_kph</td>\n",
|
| 781 |
+
" <td>0.098303</td>\n",
|
| 782 |
+
" </tr>\n",
|
| 783 |
+
" <tr>\n",
|
| 784 |
+
" <th>4</th>\n",
|
| 785 |
+
" <td>month</td>\n",
|
| 786 |
+
" <td>0.075172</td>\n",
|
| 787 |
+
" </tr>\n",
|
| 788 |
+
" <tr>\n",
|
| 789 |
+
" <th>5</th>\n",
|
| 790 |
+
" <td>r__vehicles_per_day</td>\n",
|
| 791 |
+
" <td>0.069528</td>\n",
|
| 792 |
+
" </tr>\n",
|
| 793 |
+
" <tr>\n",
|
| 794 |
+
" <th>6</th>\n",
|
| 795 |
+
" <td>r__bikes_per_day</td>\n",
|
| 796 |
+
" <td>0.068437</td>\n",
|
| 797 |
+
" </tr>\n",
|
| 798 |
+
" <tr>\n",
|
| 799 |
+
" <th>7</th>\n",
|
| 800 |
+
" <td>r__peds_per_day</td>\n",
|
| 801 |
+
" <td>0.066777</td>\n",
|
| 802 |
+
" </tr>\n",
|
| 803 |
+
" <tr>\n",
|
| 804 |
+
" <th>8</th>\n",
|
| 805 |
+
" <td>heatwave_nan</td>\n",
|
| 806 |
+
" <td>0.048992</td>\n",
|
| 807 |
+
" </tr>\n",
|
| 808 |
+
" <tr>\n",
|
| 809 |
+
" <th>9</th>\n",
|
| 810 |
+
" <td>heatwave_False</td>\n",
|
| 811 |
+
" <td>0.001682</td>\n",
|
| 812 |
+
" </tr>\n",
|
| 813 |
+
" <tr>\n",
|
| 814 |
+
" <th>10</th>\n",
|
| 815 |
+
" <td>heatwave_True</td>\n",
|
| 816 |
+
" <td>0.001590</td>\n",
|
| 817 |
+
" </tr>\n",
|
| 818 |
+
" </tbody>\n",
|
| 819 |
+
"</table>\n",
|
| 820 |
+
"</div>"
|
| 821 |
+
],
|
| 822 |
+
"text/plain": [
|
| 823 |
+
" feature importance\n",
|
| 824 |
+
"0 precip_mm 0.231075\n",
|
| 825 |
+
"1 temp_c 0.223292\n",
|
| 826 |
+
"2 snow_cm 0.115154\n",
|
| 827 |
+
"3 wind_kph 0.098303\n",
|
| 828 |
+
"4 month 0.075172\n",
|
| 829 |
+
"5 r__vehicles_per_day 0.069528\n",
|
| 830 |
+
"6 r__bikes_per_day 0.068437\n",
|
| 831 |
+
"7 r__peds_per_day 0.066777\n",
|
| 832 |
+
"8 heatwave_nan 0.048992\n",
|
| 833 |
+
"9 heatwave_False 0.001682\n",
|
| 834 |
+
"10 heatwave_True 0.001590"
|
| 835 |
+
]
|
| 836 |
+
},
|
| 837 |
+
"metadata": {},
|
| 838 |
+
"output_type": "display_data"
|
| 839 |
+
},
|
| 840 |
+
{
|
| 841 |
+
"name": "stdout",
|
| 842 |
+
"output_type": "stream",
|
| 843 |
+
"text": [
|
| 844 |
+
"Saved table: ./ml_tables\\regr_top_feature_importances.csv\n",
|
| 845 |
+
"Saved figure: ./ml_figs\\regr_feature_importances.png\n"
|
| 846 |
+
]
|
| 847 |
+
}
|
| 848 |
+
],
|
| 849 |
+
"source": [
|
| 850 |
+
"# Extract MDI importances from pipelines (requires access to transformed feature names)\n",
|
| 851 |
+
"def get_feature_names(preprocessor, num_cols, cat_cols, X):\n",
|
| 852 |
+
" num_feats = num_cols\n",
|
| 853 |
+
" # OneHot names\n",
|
| 854 |
+
" oh = preprocessor.named_transformers_['cat']\n",
|
| 855 |
+
" cat_names = list(oh.get_feature_names_out(cat_cols))\n",
|
| 856 |
+
" return list(num_feats) + cat_names\n",
|
| 857 |
+
"\n",
|
| 858 |
+
"# Classification importances\n",
|
| 859 |
+
"try:\n",
|
| 860 |
+
" clf_model = clf.named_steps['rf']\n",
|
| 861 |
+
" feat_names = get_feature_names(clf.named_steps['pre'], \n",
|
| 862 |
+
" ['temp_c','precip_mm','snow_cm','wind_kph','month'],\n",
|
| 863 |
+
" ['category','reported_by','heatwave','has_bike_infra'],\n",
|
| 864 |
+
" X_train)\n",
|
| 865 |
+
" imp = pd.Series(clf_model.feature_importances_, index=feat_names).sort_values(ascending=False)\n",
|
| 866 |
+
" top_imp = imp.head(20).reset_index()\n",
|
| 867 |
+
" top_imp.columns = ['feature','importance']\n",
|
| 868 |
+
" display(top_imp)\n",
|
| 869 |
+
" savetab(top_imp, \"clf_top_feature_importances.csv\")\n",
|
| 870 |
+
"\n",
|
| 871 |
+
" plt.figure(figsize=(8,5))\n",
|
| 872 |
+
" plt.barh(top_imp['feature'][::-1], top_imp['importance'][::-1])\n",
|
| 873 |
+
" plt.title(\"Top Feature Importances — Incident Severity RF\")\n",
|
| 874 |
+
" plt.xlabel(\"Importance\")\n",
|
| 875 |
+
" savefig(\"clf_feature_importances.png\")\n",
|
| 876 |
+
"except Exception as e:\n",
|
| 877 |
+
" print(\"Could not compute classification importances:\", e)\n",
|
| 878 |
+
"\n",
|
| 879 |
+
"# Regression importances\n",
|
| 880 |
+
"try:\n",
|
| 881 |
+
" regr_model = regr.named_steps['rf']\n",
|
| 882 |
+
" feat_names = get_feature_names(regr.named_steps['pre'],\n",
|
| 883 |
+
" ['temp_c','precip_mm','snow_cm','wind_kph','month','r__vehicles_per_day','r__bikes_per_day','r__peds_per_day'],\n",
|
| 884 |
+
" ['heatwave'],\n",
|
| 885 |
+
" X_train)\n",
|
| 886 |
+
" imp = pd.Series(regr_model.feature_importances_, index=feat_names).sort_values(ascending=False)\n",
|
| 887 |
+
" top_imp = imp.head(20).reset_index()\n",
|
| 888 |
+
" top_imp.columns = ['feature','importance']\n",
|
| 889 |
+
" display(top_imp)\n",
|
| 890 |
+
" savetab(top_imp, \"regr_top_feature_importances.csv\")\n",
|
| 891 |
+
"\n",
|
| 892 |
+
" plt.figure(figsize=(8,5))\n",
|
| 893 |
+
" plt.barh(top_imp['feature'][::-1], top_imp['importance'][::-1])\n",
|
| 894 |
+
" plt.title(\"Top Feature Importances — Noise RF\")\n",
|
| 895 |
+
" plt.xlabel(\"Importance\")\n",
|
| 896 |
+
" savefig(\"regr_feature_importances.png\")\n",
|
| 897 |
+
"except Exception as e:\n",
|
| 898 |
+
" print(\"Could not compute regression importances:\", e)"
|
| 899 |
+
]
|
| 900 |
+
},
|
| 901 |
+
{
|
| 902 |
+
"cell_type": "markdown",
|
| 903 |
+
"id": "0963228d",
|
| 904 |
+
"metadata": {},
|
| 905 |
+
"source": [
|
| 906 |
+
"### Next ideas\n",
|
| 907 |
+
"- Use **time-based splits** (train on early years, test on later) for more realistic forecasting.\n",
|
| 908 |
+
"- Try **Gradient Boosting** or **XGBoost/LightGBM** for stronger baselines.\n",
|
| 909 |
+
"- Add **spatial features** (distance to parks, density of POIs) for richer models.\n",
|
| 910 |
+
"- Package results into an **HTML report** or a small **Streamlit** app."
|
| 911 |
+
]
|
| 912 |
+
}
|
| 913 |
+
],
|
| 914 |
+
"metadata": {
|
| 915 |
+
"kernelspec": {
|
| 916 |
+
"display_name": "Python 3 (ipykernel)",
|
| 917 |
+
"language": "python",
|
| 918 |
+
"name": "python3"
|
| 919 |
+
},
|
| 920 |
+
"language_info": {
|
| 921 |
+
"codemirror_mode": {
|
| 922 |
+
"name": "ipython",
|
| 923 |
+
"version": 3
|
| 924 |
+
},
|
| 925 |
+
"file_extension": ".py",
|
| 926 |
+
"mimetype": "text/x-python",
|
| 927 |
+
"name": "python",
|
| 928 |
+
"nbconvert_exporter": "python",
|
| 929 |
+
"pygments_lexer": "ipython3",
|
| 930 |
+
"version": "3.12.7"
|
| 931 |
+
}
|
| 932 |
+
},
|
| 933 |
+
"nbformat": 4,
|
| 934 |
+
"nbformat_minor": 5
|
| 935 |
+
}
|
DBbun_Davis_analytics_demo.ipynb
ADDED
|
@@ -0,0 +1,1591 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "b72504e7",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# DBbun Davis — Medium Demo Notebook\n",
|
| 9 |
+
"This notebook loads the synthetic **Davis Square** dataset and produces attractive, saved-to-disk figures and tables.\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"## How to use\n",
|
| 12 |
+
"1. Generate the dataset first (recommended **medium** size): \n",
|
| 13 |
+
" ```bash\n",
|
| 14 |
+
" python make_davis_square.py --size medium --seed 42 --out ./out_dbbun_davis_medium\n",
|
| 15 |
+
" ```\n",
|
| 16 |
+
"2. Update `DATA_DIR` below to point to your output folder.\n",
|
| 17 |
+
"3. Run all cells. All figures will be saved to `FIGS_DIR`, and summary tables to `TABLES_DIR`.\n"
|
| 18 |
+
]
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"cell_type": "code",
|
| 22 |
+
"execution_count": 9,
|
| 23 |
+
"id": "2ba10144",
|
| 24 |
+
"metadata": {},
|
| 25 |
+
"outputs": [
|
| 26 |
+
{
|
| 27 |
+
"name": "stdout",
|
| 28 |
+
"output_type": "stream",
|
| 29 |
+
"text": [
|
| 30 |
+
"All expected files found.\n"
|
| 31 |
+
]
|
| 32 |
+
}
|
| 33 |
+
],
|
| 34 |
+
"source": [
|
| 35 |
+
"# --- User config ---\n",
|
| 36 |
+
"#DATA_DIR = \"./out_dbbun_davis_medium\" # <-- change if needed\n",
|
| 37 |
+
"DATA_DIR = \"./\"\n",
|
| 38 |
+
"FIGS_DIR = \"./figs\"\n",
|
| 39 |
+
"TABLES_DIR = \"./tables\"\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"# --- Imports & setup ---\n",
|
| 42 |
+
"import os\n",
|
| 43 |
+
"import pandas as pd\n",
|
| 44 |
+
"import numpy as np\n",
|
| 45 |
+
"import matplotlib.pyplot as plt\n",
|
| 46 |
+
"import json\n",
|
| 47 |
+
"\n",
|
| 48 |
+
"os.makedirs(FIGS_DIR, exist_ok=True)\n",
|
| 49 |
+
"os.makedirs(TABLES_DIR, exist_ok=True)\n",
|
| 50 |
+
"\n",
|
| 51 |
+
"def savefig(name):\n",
|
| 52 |
+
" path = os.path.join(FIGS_DIR, name)\n",
|
| 53 |
+
" plt.savefig(path, bbox_inches=\"tight\", dpi=144)\n",
|
| 54 |
+
" plt.close()\n",
|
| 55 |
+
" print(f\"Saved figure: {path}\")\n",
|
| 56 |
+
"\n",
|
| 57 |
+
"def savetab(df, name):\n",
|
| 58 |
+
" path = os.path.join(TABLES_DIR, name)\n",
|
| 59 |
+
" df.to_csv(path, index=False)\n",
|
| 60 |
+
" print(f\"Saved table: {path}\")\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"# Verify required files exist\n",
|
| 63 |
+
"required_files = [\n",
|
| 64 |
+
" \"geo_streets.csv\",\"geo_parks.csv\",\"poi_generic.csv\",\"households.csv\",\n",
|
| 65 |
+
" \"pets_registry.csv\",\"pet_incidents.csv\",\"mobility_trips.csv\",\"public_safety.csv\",\n",
|
| 66 |
+
" \"events_civic.csv\",\"observations.csv\",\"transit_lines.csv\",\"transit_stops.csv\",\n",
|
| 67 |
+
" \"transit_ridership_daily.csv\",\"bike_infra.csv\",\"traffic_counts.csv\",\n",
|
| 68 |
+
" \"prices_index.csv\",\"weather_daily.csv\",\"trees_inventory.csv\",\n",
|
| 69 |
+
" \"infrastructure_events.csv\",\"building_issues.csv\",\"DATA_DICTIONARY.json\"\n",
|
| 70 |
+
"]\n",
|
| 71 |
+
"missing = [f for f in required_files if not os.path.exists(os.path.join(DATA_DIR, f))]\n",
|
| 72 |
+
"if missing:\n",
|
| 73 |
+
" print(\"WARNING: Missing files (update DATA_DIR or regenerate dataset):\")\n",
|
| 74 |
+
" for m in missing:\n",
|
| 75 |
+
" print(\" -\", m)\n",
|
| 76 |
+
"else:\n",
|
| 77 |
+
" print(\"All expected files found.\")"
|
| 78 |
+
]
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"cell_type": "code",
|
| 82 |
+
"execution_count": 10,
|
| 83 |
+
"id": "39136d91-c63f-4e18-b8c5-fd77f95305bc",
|
| 84 |
+
"metadata": {},
|
| 85 |
+
"outputs": [
|
| 86 |
+
{
|
| 87 |
+
"name": "stdout",
|
| 88 |
+
"output_type": "stream",
|
| 89 |
+
"text": [
|
| 90 |
+
"['.ipynb_checkpoints', 'bike_infra.csv', 'building_issues.csv', 'DATA_DICTIONARY.json', 'DBbun_Davis_medium_demo.ipynb', 'events_civic.csv', 'figs', 'geo_parks.csv', 'geo_streets.csv', 'households.csv']\n"
|
| 91 |
+
]
|
| 92 |
+
}
|
| 93 |
+
],
|
| 94 |
+
"source": [
|
| 95 |
+
"import os\n",
|
| 96 |
+
"print(os.listdir(DATA_DIR)[:10])"
|
| 97 |
+
]
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
"cell_type": "code",
|
| 101 |
+
"execution_count": 11,
|
| 102 |
+
"id": "8bcd1b10",
|
| 103 |
+
"metadata": {},
|
| 104 |
+
"outputs": [
|
| 105 |
+
{
|
| 106 |
+
"data": {
|
| 107 |
+
"text/html": [
|
| 108 |
+
"<div>\n",
|
| 109 |
+
"<style scoped>\n",
|
| 110 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 111 |
+
" vertical-align: middle;\n",
|
| 112 |
+
" }\n",
|
| 113 |
+
"\n",
|
| 114 |
+
" .dataframe tbody tr th {\n",
|
| 115 |
+
" vertical-align: top;\n",
|
| 116 |
+
" }\n",
|
| 117 |
+
"\n",
|
| 118 |
+
" .dataframe thead th {\n",
|
| 119 |
+
" text-align: right;\n",
|
| 120 |
+
" }\n",
|
| 121 |
+
"</style>\n",
|
| 122 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 123 |
+
" <thead>\n",
|
| 124 |
+
" <tr style=\"text-align: right;\">\n",
|
| 125 |
+
" <th></th>\n",
|
| 126 |
+
" <th>table</th>\n",
|
| 127 |
+
" <th>rows</th>\n",
|
| 128 |
+
" </tr>\n",
|
| 129 |
+
" </thead>\n",
|
| 130 |
+
" <tbody>\n",
|
| 131 |
+
" <tr>\n",
|
| 132 |
+
" <th>0</th>\n",
|
| 133 |
+
" <td>streets</td>\n",
|
| 134 |
+
" <td>15</td>\n",
|
| 135 |
+
" </tr>\n",
|
| 136 |
+
" <tr>\n",
|
| 137 |
+
" <th>1</th>\n",
|
| 138 |
+
" <td>parks</td>\n",
|
| 139 |
+
" <td>4</td>\n",
|
| 140 |
+
" </tr>\n",
|
| 141 |
+
" <tr>\n",
|
| 142 |
+
" <th>2</th>\n",
|
| 143 |
+
" <td>poi</td>\n",
|
| 144 |
+
" <td>19</td>\n",
|
| 145 |
+
" </tr>\n",
|
| 146 |
+
" <tr>\n",
|
| 147 |
+
" <th>3</th>\n",
|
| 148 |
+
" <td>households</td>\n",
|
| 149 |
+
" <td>8000</td>\n",
|
| 150 |
+
" </tr>\n",
|
| 151 |
+
" <tr>\n",
|
| 152 |
+
" <th>4</th>\n",
|
| 153 |
+
" <td>pets</td>\n",
|
| 154 |
+
" <td>3213</td>\n",
|
| 155 |
+
" </tr>\n",
|
| 156 |
+
" <tr>\n",
|
| 157 |
+
" <th>5</th>\n",
|
| 158 |
+
" <td>pet_inc</td>\n",
|
| 159 |
+
" <td>338</td>\n",
|
| 160 |
+
" </tr>\n",
|
| 161 |
+
" <tr>\n",
|
| 162 |
+
" <th>6</th>\n",
|
| 163 |
+
" <td>trips</td>\n",
|
| 164 |
+
" <td>120000</td>\n",
|
| 165 |
+
" </tr>\n",
|
| 166 |
+
" <tr>\n",
|
| 167 |
+
" <th>7</th>\n",
|
| 168 |
+
" <td>safety</td>\n",
|
| 169 |
+
" <td>12000</td>\n",
|
| 170 |
+
" </tr>\n",
|
| 171 |
+
" <tr>\n",
|
| 172 |
+
" <th>8</th>\n",
|
| 173 |
+
" <td>events</td>\n",
|
| 174 |
+
" <td>195</td>\n",
|
| 175 |
+
" </tr>\n",
|
| 176 |
+
" <tr>\n",
|
| 177 |
+
" <th>9</th>\n",
|
| 178 |
+
" <td>obs</td>\n",
|
| 179 |
+
" <td>35000</td>\n",
|
| 180 |
+
" </tr>\n",
|
| 181 |
+
" <tr>\n",
|
| 182 |
+
" <th>10</th>\n",
|
| 183 |
+
" <td>lines</td>\n",
|
| 184 |
+
" <td>4</td>\n",
|
| 185 |
+
" </tr>\n",
|
| 186 |
+
" <tr>\n",
|
| 187 |
+
" <th>11</th>\n",
|
| 188 |
+
" <td>stops</td>\n",
|
| 189 |
+
" <td>4</td>\n",
|
| 190 |
+
" </tr>\n",
|
| 191 |
+
" <tr>\n",
|
| 192 |
+
" <th>12</th>\n",
|
| 193 |
+
" <td>ridership</td>\n",
|
| 194 |
+
" <td>92</td>\n",
|
| 195 |
+
" </tr>\n",
|
| 196 |
+
" <tr>\n",
|
| 197 |
+
" <th>13</th>\n",
|
| 198 |
+
" <td>bike</td>\n",
|
| 199 |
+
" <td>9</td>\n",
|
| 200 |
+
" </tr>\n",
|
| 201 |
+
" <tr>\n",
|
| 202 |
+
" <th>14</th>\n",
|
| 203 |
+
" <td>traffic</td>\n",
|
| 204 |
+
" <td>315</td>\n",
|
| 205 |
+
" </tr>\n",
|
| 206 |
+
" <tr>\n",
|
| 207 |
+
" <th>15</th>\n",
|
| 208 |
+
" <td>prices</td>\n",
|
| 209 |
+
" <td>301</td>\n",
|
| 210 |
+
" </tr>\n",
|
| 211 |
+
" <tr>\n",
|
| 212 |
+
" <th>16</th>\n",
|
| 213 |
+
" <td>weather</td>\n",
|
| 214 |
+
" <td>101136</td>\n",
|
| 215 |
+
" </tr>\n",
|
| 216 |
+
" <tr>\n",
|
| 217 |
+
" <th>17</th>\n",
|
| 218 |
+
" <td>trees</td>\n",
|
| 219 |
+
" <td>79</td>\n",
|
| 220 |
+
" </tr>\n",
|
| 221 |
+
" <tr>\n",
|
| 222 |
+
" <th>18</th>\n",
|
| 223 |
+
" <td>infra</td>\n",
|
| 224 |
+
" <td>253</td>\n",
|
| 225 |
+
" </tr>\n",
|
| 226 |
+
" <tr>\n",
|
| 227 |
+
" <th>19</th>\n",
|
| 228 |
+
" <td>bissues</td>\n",
|
| 229 |
+
" <td>1614</td>\n",
|
| 230 |
+
" </tr>\n",
|
| 231 |
+
" </tbody>\n",
|
| 232 |
+
"</table>\n",
|
| 233 |
+
"</div>"
|
| 234 |
+
],
|
| 235 |
+
"text/plain": [
|
| 236 |
+
" table rows\n",
|
| 237 |
+
"0 streets 15\n",
|
| 238 |
+
"1 parks 4\n",
|
| 239 |
+
"2 poi 19\n",
|
| 240 |
+
"3 households 8000\n",
|
| 241 |
+
"4 pets 3213\n",
|
| 242 |
+
"5 pet_inc 338\n",
|
| 243 |
+
"6 trips 120000\n",
|
| 244 |
+
"7 safety 12000\n",
|
| 245 |
+
"8 events 195\n",
|
| 246 |
+
"9 obs 35000\n",
|
| 247 |
+
"10 lines 4\n",
|
| 248 |
+
"11 stops 4\n",
|
| 249 |
+
"12 ridership 92\n",
|
| 250 |
+
"13 bike 9\n",
|
| 251 |
+
"14 traffic 315\n",
|
| 252 |
+
"15 prices 301\n",
|
| 253 |
+
"16 weather 101136\n",
|
| 254 |
+
"17 trees 79\n",
|
| 255 |
+
"18 infra 253\n",
|
| 256 |
+
"19 bissues 1614"
|
| 257 |
+
]
|
| 258 |
+
},
|
| 259 |
+
"metadata": {},
|
| 260 |
+
"output_type": "display_data"
|
| 261 |
+
},
|
| 262 |
+
{
|
| 263 |
+
"name": "stdout",
|
| 264 |
+
"output_type": "stream",
|
| 265 |
+
"text": [
|
| 266 |
+
"Saved table: ./tables\\dataset_shapes.csv\n"
|
| 267 |
+
]
|
| 268 |
+
}
|
| 269 |
+
],
|
| 270 |
+
"source": [
|
| 271 |
+
"# Load data\n",
|
| 272 |
+
"def load_csv(name):\n",
|
| 273 |
+
" return pd.read_csv(os.path.join(DATA_DIR, name))\n",
|
| 274 |
+
"\n",
|
| 275 |
+
"streets = load_csv(\"geo_streets.csv\")\n",
|
| 276 |
+
"parks = load_csv(\"geo_parks.csv\")\n",
|
| 277 |
+
"poi = load_csv(\"poi_generic.csv\")\n",
|
| 278 |
+
"households = load_csv(\"households.csv\")\n",
|
| 279 |
+
"pets = load_csv(\"pets_registry.csv\")\n",
|
| 280 |
+
"pet_inc = load_csv(\"pet_incidents.csv\")\n",
|
| 281 |
+
"trips = load_csv(\"mobility_trips.csv\")\n",
|
| 282 |
+
"safety = load_csv(\"public_safety.csv\")\n",
|
| 283 |
+
"events = load_csv(\"events_civic.csv\")\n",
|
| 284 |
+
"obs = load_csv(\"observations.csv\")\n",
|
| 285 |
+
"lines = load_csv(\"transit_lines.csv\")\n",
|
| 286 |
+
"stops = load_csv(\"transit_stops.csv\")\n",
|
| 287 |
+
"ridership = load_csv(\"transit_ridership_daily.csv\")\n",
|
| 288 |
+
"bike = load_csv(\"bike_infra.csv\")\n",
|
| 289 |
+
"traffic = load_csv(\"traffic_counts.csv\")\n",
|
| 290 |
+
"prices = load_csv(\"prices_index.csv\")\n",
|
| 291 |
+
"weather = load_csv(\"weather_daily.csv\")\n",
|
| 292 |
+
"trees = load_csv(\"trees_inventory.csv\")\n",
|
| 293 |
+
"infra = load_csv(\"infrastructure_events.csv\")\n",
|
| 294 |
+
"bissues = load_csv(\"building_issues.csv\")\n",
|
| 295 |
+
"\n",
|
| 296 |
+
"with open(os.path.join(DATA_DIR, \"DATA_DICTIONARY.json\"), \"r\", encoding=\"utf-8\") as f:\n",
|
| 297 |
+
" dd = json.load(f)\n",
|
| 298 |
+
"\n",
|
| 299 |
+
"# Peek sizes\n",
|
| 300 |
+
"summary_shapes = pd.DataFrame({\n",
|
| 301 |
+
" \"table\": [\"streets\",\"parks\",\"poi\",\"households\",\"pets\",\"pet_inc\",\"trips\",\"safety\",\"events\",\"obs\",\n",
|
| 302 |
+
" \"lines\",\"stops\",\"ridership\",\"bike\",\"traffic\",\"prices\",\"weather\",\"trees\",\"infra\",\"bissues\"],\n",
|
| 303 |
+
" \"rows\": [len(streets),len(parks),len(poi),len(households),len(pets),len(pet_inc),\n",
|
| 304 |
+
" len(trips),len(safety),len(events),len(obs),len(lines),len(stops),\n",
|
| 305 |
+
" len(ridership),len(bike),len(traffic),len(prices),len(weather),len(trees),len(infra),len(bissues)]\n",
|
| 306 |
+
"})\n",
|
| 307 |
+
"display(summary_shapes)\n",
|
| 308 |
+
"savetab(summary_shapes, \"dataset_shapes.csv\")"
|
| 309 |
+
]
|
| 310 |
+
},
|
| 311 |
+
{
|
| 312 |
+
"cell_type": "code",
|
| 313 |
+
"execution_count": 12,
|
| 314 |
+
"id": "54398070",
|
| 315 |
+
"metadata": {},
|
| 316 |
+
"outputs": [
|
| 317 |
+
{
|
| 318 |
+
"name": "stdout",
|
| 319 |
+
"output_type": "stream",
|
| 320 |
+
"text": [
|
| 321 |
+
"Saved figure: ./figs\\01_geo_scatter.png\n"
|
| 322 |
+
]
|
| 323 |
+
}
|
| 324 |
+
],
|
| 325 |
+
"source": [
|
| 326 |
+
"import re\n",
|
| 327 |
+
"\n",
|
| 328 |
+
"def wkt_point_to_lonlat(wkt):\n",
|
| 329 |
+
" # expects 'POINT(lon lat)'\n",
|
| 330 |
+
" m = re.match(r\"POINT\\(([-0-9\\.]+)\\s+([-0-9\\.]+)\\)\", str(wkt))\n",
|
| 331 |
+
" if not m:\n",
|
| 332 |
+
" return np.nan, np.nan\n",
|
| 333 |
+
" lon, lat = float(m.group(1)), float(m.group(2))\n",
|
| 334 |
+
" return lon, lat\n",
|
| 335 |
+
"\n",
|
| 336 |
+
"streets[[\"lon\",\"lat\"]] = streets[\"geometry_wkt\"].apply(lambda s: pd.Series(wkt_point_to_lonlat(s)))\n",
|
| 337 |
+
"parks[[\"lon\",\"lat\"]] = parks[\"geometry_wkt\"].apply(lambda s: pd.Series(wkt_point_to_lonlat(s)))\n",
|
| 338 |
+
"\n",
|
| 339 |
+
"# Save quick sample map\n",
|
| 340 |
+
"plt.figure(figsize=(6,6))\n",
|
| 341 |
+
"plt.scatter(streets[\"lon\"], streets[\"lat\"], s=12, alpha=0.7)\n",
|
| 342 |
+
"plt.scatter(parks[\"lon\"], parks[\"lat\"], s=40, marker=\"^\", alpha=0.9)\n",
|
| 343 |
+
"plt.title(\"Synthetic Geo Points — Streets & Parks (WKT)\")\n",
|
| 344 |
+
"plt.xlabel(\"Longitude\"); plt.ylabel(\"Latitude\")\n",
|
| 345 |
+
"savefig(\"01_geo_scatter.png\")"
|
| 346 |
+
]
|
| 347 |
+
},
|
| 348 |
+
{
|
| 349 |
+
"cell_type": "code",
|
| 350 |
+
"execution_count": 13,
|
| 351 |
+
"id": "c3230d2c",
|
| 352 |
+
"metadata": {},
|
| 353 |
+
"outputs": [
|
| 354 |
+
{
|
| 355 |
+
"data": {
|
| 356 |
+
"text/html": [
|
| 357 |
+
"<div>\n",
|
| 358 |
+
"<style scoped>\n",
|
| 359 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 360 |
+
" vertical-align: middle;\n",
|
| 361 |
+
" }\n",
|
| 362 |
+
"\n",
|
| 363 |
+
" .dataframe tbody tr th {\n",
|
| 364 |
+
" vertical-align: top;\n",
|
| 365 |
+
" }\n",
|
| 366 |
+
"\n",
|
| 367 |
+
" .dataframe thead th {\n",
|
| 368 |
+
" text-align: right;\n",
|
| 369 |
+
" }\n",
|
| 370 |
+
"</style>\n",
|
| 371 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 372 |
+
" <thead>\n",
|
| 373 |
+
" <tr style=\"text-align: right;\">\n",
|
| 374 |
+
" <th></th>\n",
|
| 375 |
+
" <th>category</th>\n",
|
| 376 |
+
" <th>subcategory</th>\n",
|
| 377 |
+
" <th>count</th>\n",
|
| 378 |
+
" </tr>\n",
|
| 379 |
+
" </thead>\n",
|
| 380 |
+
" <tbody>\n",
|
| 381 |
+
" <tr>\n",
|
| 382 |
+
" <th>5</th>\n",
|
| 383 |
+
" <td>community_center</td>\n",
|
| 384 |
+
" <td>civic</td>\n",
|
| 385 |
+
" <td>3</td>\n",
|
| 386 |
+
" </tr>\n",
|
| 387 |
+
" <tr>\n",
|
| 388 |
+
" <th>0</th>\n",
|
| 389 |
+
" <td>bakery</td>\n",
|
| 390 |
+
" <td>retail</td>\n",
|
| 391 |
+
" <td>2</td>\n",
|
| 392 |
+
" </tr>\n",
|
| 393 |
+
" <tr>\n",
|
| 394 |
+
" <th>3</th>\n",
|
| 395 |
+
" <td>cafe</td>\n",
|
| 396 |
+
" <td>coffee_tea</td>\n",
|
| 397 |
+
" <td>2</td>\n",
|
| 398 |
+
" </tr>\n",
|
| 399 |
+
" <tr>\n",
|
| 400 |
+
" <th>6</th>\n",
|
| 401 |
+
" <td>hardware</td>\n",
|
| 402 |
+
" <td>retail</td>\n",
|
| 403 |
+
" <td>2</td>\n",
|
| 404 |
+
" </tr>\n",
|
| 405 |
+
" <tr>\n",
|
| 406 |
+
" <th>9</th>\n",
|
| 407 |
+
" <td>post_office</td>\n",
|
| 408 |
+
" <td>civic</td>\n",
|
| 409 |
+
" <td>2</td>\n",
|
| 410 |
+
" </tr>\n",
|
| 411 |
+
" <tr>\n",
|
| 412 |
+
" <th>1</th>\n",
|
| 413 |
+
" <td>bank</td>\n",
|
| 414 |
+
" <td>retail</td>\n",
|
| 415 |
+
" <td>1</td>\n",
|
| 416 |
+
" </tr>\n",
|
| 417 |
+
" <tr>\n",
|
| 418 |
+
" <th>2</th>\n",
|
| 419 |
+
" <td>bar</td>\n",
|
| 420 |
+
" <td>neighborhood</td>\n",
|
| 421 |
+
" <td>1</td>\n",
|
| 422 |
+
" </tr>\n",
|
| 423 |
+
" <tr>\n",
|
| 424 |
+
" <th>4</th>\n",
|
| 425 |
+
" <td>clinic</td>\n",
|
| 426 |
+
" <td>primary</td>\n",
|
| 427 |
+
" <td>1</td>\n",
|
| 428 |
+
" </tr>\n",
|
| 429 |
+
" <tr>\n",
|
| 430 |
+
" <th>7</th>\n",
|
| 431 |
+
" <td>pharmacy</td>\n",
|
| 432 |
+
" <td>retail</td>\n",
|
| 433 |
+
" <td>1</td>\n",
|
| 434 |
+
" </tr>\n",
|
| 435 |
+
" <tr>\n",
|
| 436 |
+
" <th>8</th>\n",
|
| 437 |
+
" <td>playground</td>\n",
|
| 438 |
+
" <td>standard</td>\n",
|
| 439 |
+
" <td>1</td>\n",
|
| 440 |
+
" </tr>\n",
|
| 441 |
+
" <tr>\n",
|
| 442 |
+
" <th>10</th>\n",
|
| 443 |
+
" <td>school</td>\n",
|
| 444 |
+
" <td>public</td>\n",
|
| 445 |
+
" <td>1</td>\n",
|
| 446 |
+
" </tr>\n",
|
| 447 |
+
" <tr>\n",
|
| 448 |
+
" <th>11</th>\n",
|
| 449 |
+
" <td>shelter</td>\n",
|
| 450 |
+
" <td>animal</td>\n",
|
| 451 |
+
" <td>1</td>\n",
|
| 452 |
+
" </tr>\n",
|
| 453 |
+
" <tr>\n",
|
| 454 |
+
" <th>12</th>\n",
|
| 455 |
+
" <td>urgent_care</td>\n",
|
| 456 |
+
" <td>clinic</td>\n",
|
| 457 |
+
" <td>1</td>\n",
|
| 458 |
+
" </tr>\n",
|
| 459 |
+
" </tbody>\n",
|
| 460 |
+
"</table>\n",
|
| 461 |
+
"</div>"
|
| 462 |
+
],
|
| 463 |
+
"text/plain": [
|
| 464 |
+
" category subcategory count\n",
|
| 465 |
+
"5 community_center civic 3\n",
|
| 466 |
+
"0 bakery retail 2\n",
|
| 467 |
+
"3 cafe coffee_tea 2\n",
|
| 468 |
+
"6 hardware retail 2\n",
|
| 469 |
+
"9 post_office civic 2\n",
|
| 470 |
+
"1 bank retail 1\n",
|
| 471 |
+
"2 bar neighborhood 1\n",
|
| 472 |
+
"4 clinic primary 1\n",
|
| 473 |
+
"7 pharmacy retail 1\n",
|
| 474 |
+
"8 playground standard 1\n",
|
| 475 |
+
"10 school public 1\n",
|
| 476 |
+
"11 shelter animal 1\n",
|
| 477 |
+
"12 urgent_care clinic 1"
|
| 478 |
+
]
|
| 479 |
+
},
|
| 480 |
+
"metadata": {},
|
| 481 |
+
"output_type": "display_data"
|
| 482 |
+
},
|
| 483 |
+
{
|
| 484 |
+
"name": "stdout",
|
| 485 |
+
"output_type": "stream",
|
| 486 |
+
"text": [
|
| 487 |
+
"Saved table: ./tables\\poi_mix.csv\n",
|
| 488 |
+
"Saved figure: ./figs\\02_poi_category_bar.png\n"
|
| 489 |
+
]
|
| 490 |
+
}
|
| 491 |
+
],
|
| 492 |
+
"source": [
|
| 493 |
+
"poi_mix = poi.groupby([\"category\",\"subcategory\"])[\"poi_id\"].count().reset_index(name=\"count\").sort_values(\"count\", ascending=False)\n",
|
| 494 |
+
"display(poi_mix.head(20))\n",
|
| 495 |
+
"savetab(poi_mix, \"poi_mix.csv\")\n",
|
| 496 |
+
"\n",
|
| 497 |
+
"plt.figure(figsize=(8,4))\n",
|
| 498 |
+
"topcats = poi_mix.groupby(\"category\")[\"count\"].sum().sort_values(ascending=False).head(10)\n",
|
| 499 |
+
"topcats.plot(kind=\"bar\")\n",
|
| 500 |
+
"plt.title(\"POI by Category (Top 10)\")\n",
|
| 501 |
+
"plt.xlabel(\"Category\"); plt.ylabel(\"Count\")\n",
|
| 502 |
+
"savefig(\"02_poi_category_bar.png\")"
|
| 503 |
+
]
|
| 504 |
+
},
|
| 505 |
+
{
|
| 506 |
+
"cell_type": "code",
|
| 507 |
+
"execution_count": 14,
|
| 508 |
+
"id": "ab0b40ad",
|
| 509 |
+
"metadata": {},
|
| 510 |
+
"outputs": [
|
| 511 |
+
{
|
| 512 |
+
"data": {
|
| 513 |
+
"text/html": [
|
| 514 |
+
"<div>\n",
|
| 515 |
+
"<style scoped>\n",
|
| 516 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 517 |
+
" vertical-align: middle;\n",
|
| 518 |
+
" }\n",
|
| 519 |
+
"\n",
|
| 520 |
+
" .dataframe tbody tr th {\n",
|
| 521 |
+
" vertical-align: top;\n",
|
| 522 |
+
" }\n",
|
| 523 |
+
"\n",
|
| 524 |
+
" .dataframe thead th {\n",
|
| 525 |
+
" text-align: right;\n",
|
| 526 |
+
" }\n",
|
| 527 |
+
"</style>\n",
|
| 528 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 529 |
+
" <thead>\n",
|
| 530 |
+
" <tr style=\"text-align: right;\">\n",
|
| 531 |
+
" <th></th>\n",
|
| 532 |
+
" <th>era</th>\n",
|
| 533 |
+
" <th>dwelling_type</th>\n",
|
| 534 |
+
" <th>tenure</th>\n",
|
| 535 |
+
" <th>n</th>\n",
|
| 536 |
+
" </tr>\n",
|
| 537 |
+
" </thead>\n",
|
| 538 |
+
" <tbody>\n",
|
| 539 |
+
" <tr>\n",
|
| 540 |
+
" <th>0</th>\n",
|
| 541 |
+
" <td>1800s</td>\n",
|
| 542 |
+
" <td>apartment</td>\n",
|
| 543 |
+
" <td>own</td>\n",
|
| 544 |
+
" <td>185</td>\n",
|
| 545 |
+
" </tr>\n",
|
| 546 |
+
" <tr>\n",
|
| 547 |
+
" <th>1</th>\n",
|
| 548 |
+
" <td>1800s</td>\n",
|
| 549 |
+
" <td>apartment</td>\n",
|
| 550 |
+
" <td>rent</td>\n",
|
| 551 |
+
" <td>149</td>\n",
|
| 552 |
+
" </tr>\n",
|
| 553 |
+
" <tr>\n",
|
| 554 |
+
" <th>2</th>\n",
|
| 555 |
+
" <td>1800s</td>\n",
|
| 556 |
+
" <td>condo</td>\n",
|
| 557 |
+
" <td>own</td>\n",
|
| 558 |
+
" <td>174</td>\n",
|
| 559 |
+
" </tr>\n",
|
| 560 |
+
" <tr>\n",
|
| 561 |
+
" <th>3</th>\n",
|
| 562 |
+
" <td>1800s</td>\n",
|
| 563 |
+
" <td>condo</td>\n",
|
| 564 |
+
" <td>rent</td>\n",
|
| 565 |
+
" <td>165</td>\n",
|
| 566 |
+
" </tr>\n",
|
| 567 |
+
" <tr>\n",
|
| 568 |
+
" <th>4</th>\n",
|
| 569 |
+
" <td>1800s</td>\n",
|
| 570 |
+
" <td>single_family</td>\n",
|
| 571 |
+
" <td>own</td>\n",
|
| 572 |
+
" <td>156</td>\n",
|
| 573 |
+
" </tr>\n",
|
| 574 |
+
" <tr>\n",
|
| 575 |
+
" <th>5</th>\n",
|
| 576 |
+
" <td>1800s</td>\n",
|
| 577 |
+
" <td>single_family</td>\n",
|
| 578 |
+
" <td>rent</td>\n",
|
| 579 |
+
" <td>160</td>\n",
|
| 580 |
+
" </tr>\n",
|
| 581 |
+
" <tr>\n",
|
| 582 |
+
" <th>6</th>\n",
|
| 583 |
+
" <td>1800s</td>\n",
|
| 584 |
+
" <td>triple_decker</td>\n",
|
| 585 |
+
" <td>own</td>\n",
|
| 586 |
+
" <td>175</td>\n",
|
| 587 |
+
" </tr>\n",
|
| 588 |
+
" <tr>\n",
|
| 589 |
+
" <th>7</th>\n",
|
| 590 |
+
" <td>1800s</td>\n",
|
| 591 |
+
" <td>triple_decker</td>\n",
|
| 592 |
+
" <td>rent</td>\n",
|
| 593 |
+
" <td>139</td>\n",
|
| 594 |
+
" </tr>\n",
|
| 595 |
+
" <tr>\n",
|
| 596 |
+
" <th>8</th>\n",
|
| 597 |
+
" <td>1900-1945</td>\n",
|
| 598 |
+
" <td>apartment</td>\n",
|
| 599 |
+
" <td>own</td>\n",
|
| 600 |
+
" <td>172</td>\n",
|
| 601 |
+
" </tr>\n",
|
| 602 |
+
" <tr>\n",
|
| 603 |
+
" <th>9</th>\n",
|
| 604 |
+
" <td>1900-1945</td>\n",
|
| 605 |
+
" <td>apartment</td>\n",
|
| 606 |
+
" <td>rent</td>\n",
|
| 607 |
+
" <td>171</td>\n",
|
| 608 |
+
" </tr>\n",
|
| 609 |
+
" <tr>\n",
|
| 610 |
+
" <th>10</th>\n",
|
| 611 |
+
" <td>1900-1945</td>\n",
|
| 612 |
+
" <td>condo</td>\n",
|
| 613 |
+
" <td>own</td>\n",
|
| 614 |
+
" <td>176</td>\n",
|
| 615 |
+
" </tr>\n",
|
| 616 |
+
" <tr>\n",
|
| 617 |
+
" <th>11</th>\n",
|
| 618 |
+
" <td>1900-1945</td>\n",
|
| 619 |
+
" <td>condo</td>\n",
|
| 620 |
+
" <td>rent</td>\n",
|
| 621 |
+
" <td>179</td>\n",
|
| 622 |
+
" </tr>\n",
|
| 623 |
+
" <tr>\n",
|
| 624 |
+
" <th>12</th>\n",
|
| 625 |
+
" <td>1900-1945</td>\n",
|
| 626 |
+
" <td>single_family</td>\n",
|
| 627 |
+
" <td>own</td>\n",
|
| 628 |
+
" <td>145</td>\n",
|
| 629 |
+
" </tr>\n",
|
| 630 |
+
" <tr>\n",
|
| 631 |
+
" <th>13</th>\n",
|
| 632 |
+
" <td>1900-1945</td>\n",
|
| 633 |
+
" <td>single_family</td>\n",
|
| 634 |
+
" <td>rent</td>\n",
|
| 635 |
+
" <td>163</td>\n",
|
| 636 |
+
" </tr>\n",
|
| 637 |
+
" <tr>\n",
|
| 638 |
+
" <th>14</th>\n",
|
| 639 |
+
" <td>1900-1945</td>\n",
|
| 640 |
+
" <td>triple_decker</td>\n",
|
| 641 |
+
" <td>own</td>\n",
|
| 642 |
+
" <td>172</td>\n",
|
| 643 |
+
" </tr>\n",
|
| 644 |
+
" <tr>\n",
|
| 645 |
+
" <th>15</th>\n",
|
| 646 |
+
" <td>1900-1945</td>\n",
|
| 647 |
+
" <td>triple_decker</td>\n",
|
| 648 |
+
" <td>rent</td>\n",
|
| 649 |
+
" <td>151</td>\n",
|
| 650 |
+
" </tr>\n",
|
| 651 |
+
" <tr>\n",
|
| 652 |
+
" <th>16</th>\n",
|
| 653 |
+
" <td>1946-1999</td>\n",
|
| 654 |
+
" <td>apartment</td>\n",
|
| 655 |
+
" <td>own</td>\n",
|
| 656 |
+
" <td>163</td>\n",
|
| 657 |
+
" </tr>\n",
|
| 658 |
+
" <tr>\n",
|
| 659 |
+
" <th>17</th>\n",
|
| 660 |
+
" <td>1946-1999</td>\n",
|
| 661 |
+
" <td>apartment</td>\n",
|
| 662 |
+
" <td>rent</td>\n",
|
| 663 |
+
" <td>146</td>\n",
|
| 664 |
+
" </tr>\n",
|
| 665 |
+
" <tr>\n",
|
| 666 |
+
" <th>18</th>\n",
|
| 667 |
+
" <td>1946-1999</td>\n",
|
| 668 |
+
" <td>condo</td>\n",
|
| 669 |
+
" <td>own</td>\n",
|
| 670 |
+
" <td>167</td>\n",
|
| 671 |
+
" </tr>\n",
|
| 672 |
+
" <tr>\n",
|
| 673 |
+
" <th>19</th>\n",
|
| 674 |
+
" <td>1946-1999</td>\n",
|
| 675 |
+
" <td>condo</td>\n",
|
| 676 |
+
" <td>rent</td>\n",
|
| 677 |
+
" <td>159</td>\n",
|
| 678 |
+
" </tr>\n",
|
| 679 |
+
" </tbody>\n",
|
| 680 |
+
"</table>\n",
|
| 681 |
+
"</div>"
|
| 682 |
+
],
|
| 683 |
+
"text/plain": [
|
| 684 |
+
" era dwelling_type tenure n\n",
|
| 685 |
+
"0 1800s apartment own 185\n",
|
| 686 |
+
"1 1800s apartment rent 149\n",
|
| 687 |
+
"2 1800s condo own 174\n",
|
| 688 |
+
"3 1800s condo rent 165\n",
|
| 689 |
+
"4 1800s single_family own 156\n",
|
| 690 |
+
"5 1800s single_family rent 160\n",
|
| 691 |
+
"6 1800s triple_decker own 175\n",
|
| 692 |
+
"7 1800s triple_decker rent 139\n",
|
| 693 |
+
"8 1900-1945 apartment own 172\n",
|
| 694 |
+
"9 1900-1945 apartment rent 171\n",
|
| 695 |
+
"10 1900-1945 condo own 176\n",
|
| 696 |
+
"11 1900-1945 condo rent 179\n",
|
| 697 |
+
"12 1900-1945 single_family own 145\n",
|
| 698 |
+
"13 1900-1945 single_family rent 163\n",
|
| 699 |
+
"14 1900-1945 triple_decker own 172\n",
|
| 700 |
+
"15 1900-1945 triple_decker rent 151\n",
|
| 701 |
+
"16 1946-1999 apartment own 163\n",
|
| 702 |
+
"17 1946-1999 apartment rent 146\n",
|
| 703 |
+
"18 1946-1999 condo own 167\n",
|
| 704 |
+
"19 1946-1999 condo rent 159"
|
| 705 |
+
]
|
| 706 |
+
},
|
| 707 |
+
"metadata": {},
|
| 708 |
+
"output_type": "display_data"
|
| 709 |
+
},
|
| 710 |
+
{
|
| 711 |
+
"name": "stdout",
|
| 712 |
+
"output_type": "stream",
|
| 713 |
+
"text": [
|
| 714 |
+
"Saved table: ./tables\\households_profile.csv\n",
|
| 715 |
+
"Saved figure: ./figs\\03_households_occupants_hist.png\n"
|
| 716 |
+
]
|
| 717 |
+
}
|
| 718 |
+
],
|
| 719 |
+
"source": [
|
| 720 |
+
"hh_profile = households.groupby([\"era\",\"dwelling_type\",\"tenure\"]).size().reset_index(name=\"n\")\n",
|
| 721 |
+
"display(hh_profile.head(20))\n",
|
| 722 |
+
"savetab(hh_profile, \"households_profile.csv\")\n",
|
| 723 |
+
"\n",
|
| 724 |
+
"plt.figure(figsize=(8,4))\n",
|
| 725 |
+
"households[\"occupants\"].plot(kind=\"hist\", bins=20)\n",
|
| 726 |
+
"plt.title(\"Household Occupants — Distribution\")\n",
|
| 727 |
+
"plt.xlabel(\"Occupants\"); plt.ylabel(\"Frequency\")\n",
|
| 728 |
+
"savefig(\"03_households_occupants_hist.png\")"
|
| 729 |
+
]
|
| 730 |
+
},
|
| 731 |
+
{
|
| 732 |
+
"cell_type": "code",
|
| 733 |
+
"execution_count": 15,
|
| 734 |
+
"id": "ecabf3f1",
|
| 735 |
+
"metadata": {},
|
| 736 |
+
"outputs": [
|
| 737 |
+
{
|
| 738 |
+
"data": {
|
| 739 |
+
"text/html": [
|
| 740 |
+
"<div>\n",
|
| 741 |
+
"<style scoped>\n",
|
| 742 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 743 |
+
" vertical-align: middle;\n",
|
| 744 |
+
" }\n",
|
| 745 |
+
"\n",
|
| 746 |
+
" .dataframe tbody tr th {\n",
|
| 747 |
+
" vertical-align: top;\n",
|
| 748 |
+
" }\n",
|
| 749 |
+
"\n",
|
| 750 |
+
" .dataframe thead th {\n",
|
| 751 |
+
" text-align: right;\n",
|
| 752 |
+
" }\n",
|
| 753 |
+
"</style>\n",
|
| 754 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 755 |
+
" <thead>\n",
|
| 756 |
+
" <tr style=\"text-align: right;\">\n",
|
| 757 |
+
" <th></th>\n",
|
| 758 |
+
" <th>species</th>\n",
|
| 759 |
+
" <th>count</th>\n",
|
| 760 |
+
" </tr>\n",
|
| 761 |
+
" </thead>\n",
|
| 762 |
+
" <tbody>\n",
|
| 763 |
+
" <tr>\n",
|
| 764 |
+
" <th>0</th>\n",
|
| 765 |
+
" <td>dog</td>\n",
|
| 766 |
+
" <td>2067</td>\n",
|
| 767 |
+
" </tr>\n",
|
| 768 |
+
" <tr>\n",
|
| 769 |
+
" <th>1</th>\n",
|
| 770 |
+
" <td>cat</td>\n",
|
| 771 |
+
" <td>960</td>\n",
|
| 772 |
+
" </tr>\n",
|
| 773 |
+
" <tr>\n",
|
| 774 |
+
" <th>2</th>\n",
|
| 775 |
+
" <td>other</td>\n",
|
| 776 |
+
" <td>186</td>\n",
|
| 777 |
+
" </tr>\n",
|
| 778 |
+
" </tbody>\n",
|
| 779 |
+
"</table>\n",
|
| 780 |
+
"</div>"
|
| 781 |
+
],
|
| 782 |
+
"text/plain": [
|
| 783 |
+
" species count\n",
|
| 784 |
+
"0 dog 2067\n",
|
| 785 |
+
"1 cat 960\n",
|
| 786 |
+
"2 other 186"
|
| 787 |
+
]
|
| 788 |
+
},
|
| 789 |
+
"metadata": {},
|
| 790 |
+
"output_type": "display_data"
|
| 791 |
+
},
|
| 792 |
+
{
|
| 793 |
+
"name": "stdout",
|
| 794 |
+
"output_type": "stream",
|
| 795 |
+
"text": [
|
| 796 |
+
"Saved table: ./tables\\pets_species_counts.csv\n",
|
| 797 |
+
"Saved figure: ./figs\\04_pets_species_bar.png\n"
|
| 798 |
+
]
|
| 799 |
+
}
|
| 800 |
+
],
|
| 801 |
+
"source": [
|
| 802 |
+
"pet_species = pets[\"species\"].value_counts().reset_index()\n",
|
| 803 |
+
"pet_species.columns = [\"species\",\"count\"]\n",
|
| 804 |
+
"display(pet_species)\n",
|
| 805 |
+
"savetab(pet_species, \"pets_species_counts.csv\")\n",
|
| 806 |
+
"\n",
|
| 807 |
+
"plt.figure(figsize=(6,4))\n",
|
| 808 |
+
"pet_species.set_index(\"species\")[\"count\"].plot(kind=\"bar\")\n",
|
| 809 |
+
"plt.title(\"Pets by Species\")\n",
|
| 810 |
+
"plt.xlabel(\"Species\"); plt.ylabel(\"Count\")\n",
|
| 811 |
+
"savefig(\"04_pets_species_bar.png\")"
|
| 812 |
+
]
|
| 813 |
+
},
|
| 814 |
+
{
|
| 815 |
+
"cell_type": "code",
|
| 816 |
+
"execution_count": 16,
|
| 817 |
+
"id": "1478d234",
|
| 818 |
+
"metadata": {},
|
| 819 |
+
"outputs": [
|
| 820 |
+
{
|
| 821 |
+
"data": {
|
| 822 |
+
"text/html": [
|
| 823 |
+
"<div>\n",
|
| 824 |
+
"<style scoped>\n",
|
| 825 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 826 |
+
" vertical-align: middle;\n",
|
| 827 |
+
" }\n",
|
| 828 |
+
"\n",
|
| 829 |
+
" .dataframe tbody tr th {\n",
|
| 830 |
+
" vertical-align: top;\n",
|
| 831 |
+
" }\n",
|
| 832 |
+
"\n",
|
| 833 |
+
" .dataframe thead th {\n",
|
| 834 |
+
" text-align: right;\n",
|
| 835 |
+
" }\n",
|
| 836 |
+
"</style>\n",
|
| 837 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 838 |
+
" <thead>\n",
|
| 839 |
+
" <tr style=\"text-align: right;\">\n",
|
| 840 |
+
" <th></th>\n",
|
| 841 |
+
" <th>mode</th>\n",
|
| 842 |
+
" <th>share</th>\n",
|
| 843 |
+
" </tr>\n",
|
| 844 |
+
" </thead>\n",
|
| 845 |
+
" <tbody>\n",
|
| 846 |
+
" <tr>\n",
|
| 847 |
+
" <th>0</th>\n",
|
| 848 |
+
" <td>walk</td>\n",
|
| 849 |
+
" <td>0.329950</td>\n",
|
| 850 |
+
" </tr>\n",
|
| 851 |
+
" <tr>\n",
|
| 852 |
+
" <th>1</th>\n",
|
| 853 |
+
" <td>car</td>\n",
|
| 854 |
+
" <td>0.205342</td>\n",
|
| 855 |
+
" </tr>\n",
|
| 856 |
+
" <tr>\n",
|
| 857 |
+
" <th>2</th>\n",
|
| 858 |
+
" <td>subway</td>\n",
|
| 859 |
+
" <td>0.115433</td>\n",
|
| 860 |
+
" </tr>\n",
|
| 861 |
+
" <tr>\n",
|
| 862 |
+
" <th>3</th>\n",
|
| 863 |
+
" <td>bus</td>\n",
|
| 864 |
+
" <td>0.096825</td>\n",
|
| 865 |
+
" </tr>\n",
|
| 866 |
+
" <tr>\n",
|
| 867 |
+
" <th>4</th>\n",
|
| 868 |
+
" <td>autonomous</td>\n",
|
| 869 |
+
" <td>0.084275</td>\n",
|
| 870 |
+
" </tr>\n",
|
| 871 |
+
" <tr>\n",
|
| 872 |
+
" <th>5</th>\n",
|
| 873 |
+
" <td>bike</td>\n",
|
| 874 |
+
" <td>0.068183</td>\n",
|
| 875 |
+
" </tr>\n",
|
| 876 |
+
" <tr>\n",
|
| 877 |
+
" <th>6</th>\n",
|
| 878 |
+
" <td>streetcar</td>\n",
|
| 879 |
+
" <td>0.046658</td>\n",
|
| 880 |
+
" </tr>\n",
|
| 881 |
+
" <tr>\n",
|
| 882 |
+
" <th>7</th>\n",
|
| 883 |
+
" <td>micromobility</td>\n",
|
| 884 |
+
" <td>0.029967</td>\n",
|
| 885 |
+
" </tr>\n",
|
| 886 |
+
" <tr>\n",
|
| 887 |
+
" <th>8</th>\n",
|
| 888 |
+
" <td>horse</td>\n",
|
| 889 |
+
" <td>0.023367</td>\n",
|
| 890 |
+
" </tr>\n",
|
| 891 |
+
" </tbody>\n",
|
| 892 |
+
"</table>\n",
|
| 893 |
+
"</div>"
|
| 894 |
+
],
|
| 895 |
+
"text/plain": [
|
| 896 |
+
" mode share\n",
|
| 897 |
+
"0 walk 0.329950\n",
|
| 898 |
+
"1 car 0.205342\n",
|
| 899 |
+
"2 subway 0.115433\n",
|
| 900 |
+
"3 bus 0.096825\n",
|
| 901 |
+
"4 autonomous 0.084275\n",
|
| 902 |
+
"5 bike 0.068183\n",
|
| 903 |
+
"6 streetcar 0.046658\n",
|
| 904 |
+
"7 micromobility 0.029967\n",
|
| 905 |
+
"8 horse 0.023367"
|
| 906 |
+
]
|
| 907 |
+
},
|
| 908 |
+
"metadata": {},
|
| 909 |
+
"output_type": "display_data"
|
| 910 |
+
},
|
| 911 |
+
{
|
| 912 |
+
"name": "stdout",
|
| 913 |
+
"output_type": "stream",
|
| 914 |
+
"text": [
|
| 915 |
+
"Saved table: ./tables\\mobility_mode_share_overall.csv\n",
|
| 916 |
+
"Saved figure: ./figs\\05_trips_modes_overall_bar.png\n",
|
| 917 |
+
"Saved table: ./tables\\mobility_mode_counts_by_era.csv\n",
|
| 918 |
+
"Saved table: ./tables\\mobility_mode_share_by_era.csv\n",
|
| 919 |
+
"Saved figure: ./figs\\06_trips_by_mode_over_eras_line.png\n"
|
| 920 |
+
]
|
| 921 |
+
}
|
| 922 |
+
],
|
| 923 |
+
"source": [
|
| 924 |
+
"mode_share = trips[\"mode\"].value_counts(normalize=True).reset_index()\n",
|
| 925 |
+
"mode_share.columns = [\"mode\",\"share\"]\n",
|
| 926 |
+
"display(mode_share)\n",
|
| 927 |
+
"savetab(mode_share, \"mobility_mode_share_overall.csv\")\n",
|
| 928 |
+
"\n",
|
| 929 |
+
"plt.figure(figsize=(7,4))\n",
|
| 930 |
+
"trips[\"mode\"].value_counts().plot(kind=\"bar\")\n",
|
| 931 |
+
"plt.title(\"Trip Modes — Overall Counts\")\n",
|
| 932 |
+
"plt.xlabel(\"Mode\"); plt.ylabel(\"Trips\")\n",
|
| 933 |
+
"savefig(\"05_trips_modes_overall_bar.png\")\n",
|
| 934 |
+
"\n",
|
| 935 |
+
"mode_era = trips.groupby([\"era\",\"mode\"]).size().unstack(fill_value=0)\n",
|
| 936 |
+
"mode_era_pct = mode_era.div(mode_era.sum(axis=1), axis=0)\n",
|
| 937 |
+
"savetab(mode_era.reset_index(), \"mobility_mode_counts_by_era.csv\")\n",
|
| 938 |
+
"savetab(mode_era_pct.reset_index(), \"mobility_mode_share_by_era.csv\")\n",
|
| 939 |
+
"\n",
|
| 940 |
+
"plt.figure(figsize=(8,5))\n",
|
| 941 |
+
"for m in mode_era.columns:\n",
|
| 942 |
+
" plt.plot(mode_era.index, mode_era[m], marker=\"o\")\n",
|
| 943 |
+
"plt.title(\"Trips by Mode over Eras\")\n",
|
| 944 |
+
"plt.xlabel(\"Era\"); plt.ylabel(\"Count\")\n",
|
| 945 |
+
"plt.xticks(rotation=45, ha=\"right\")\n",
|
| 946 |
+
"savefig(\"06_trips_by_mode_over_eras_line.png\")"
|
| 947 |
+
]
|
| 948 |
+
},
|
| 949 |
+
{
|
| 950 |
+
"cell_type": "code",
|
| 951 |
+
"execution_count": 17,
|
| 952 |
+
"id": "50bd51c1",
|
| 953 |
+
"metadata": {},
|
| 954 |
+
"outputs": [
|
| 955 |
+
{
|
| 956 |
+
"data": {
|
| 957 |
+
"text/html": [
|
| 958 |
+
"<div>\n",
|
| 959 |
+
"<style scoped>\n",
|
| 960 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 961 |
+
" vertical-align: middle;\n",
|
| 962 |
+
" }\n",
|
| 963 |
+
"\n",
|
| 964 |
+
" .dataframe tbody tr th {\n",
|
| 965 |
+
" vertical-align: top;\n",
|
| 966 |
+
" }\n",
|
| 967 |
+
"\n",
|
| 968 |
+
" .dataframe thead th {\n",
|
| 969 |
+
" text-align: right;\n",
|
| 970 |
+
" }\n",
|
| 971 |
+
"</style>\n",
|
| 972 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 973 |
+
" <thead>\n",
|
| 974 |
+
" <tr style=\"text-align: right;\">\n",
|
| 975 |
+
" <th></th>\n",
|
| 976 |
+
" <th>category</th>\n",
|
| 977 |
+
" <th>count</th>\n",
|
| 978 |
+
" </tr>\n",
|
| 979 |
+
" </thead>\n",
|
| 980 |
+
" <tbody>\n",
|
| 981 |
+
" <tr>\n",
|
| 982 |
+
" <th>0</th>\n",
|
| 983 |
+
" <td>traffic</td>\n",
|
| 984 |
+
" <td>1740</td>\n",
|
| 985 |
+
" </tr>\n",
|
| 986 |
+
" <tr>\n",
|
| 987 |
+
" <th>1</th>\n",
|
| 988 |
+
" <td>theft</td>\n",
|
| 989 |
+
" <td>1739</td>\n",
|
| 990 |
+
" </tr>\n",
|
| 991 |
+
" <tr>\n",
|
| 992 |
+
" <th>2</th>\n",
|
| 993 |
+
" <td>noise</td>\n",
|
| 994 |
+
" <td>1735</td>\n",
|
| 995 |
+
" </tr>\n",
|
| 996 |
+
" <tr>\n",
|
| 997 |
+
" <th>3</th>\n",
|
| 998 |
+
" <td>vandalism</td>\n",
|
| 999 |
+
" <td>1720</td>\n",
|
| 1000 |
+
" </tr>\n",
|
| 1001 |
+
" <tr>\n",
|
| 1002 |
+
" <th>4</th>\n",
|
| 1003 |
+
" <td>disturbance</td>\n",
|
| 1004 |
+
" <td>1702</td>\n",
|
| 1005 |
+
" </tr>\n",
|
| 1006 |
+
" <tr>\n",
|
| 1007 |
+
" <th>5</th>\n",
|
| 1008 |
+
" <td>animal</td>\n",
|
| 1009 |
+
" <td>1702</td>\n",
|
| 1010 |
+
" </tr>\n",
|
| 1011 |
+
" <tr>\n",
|
| 1012 |
+
" <th>6</th>\n",
|
| 1013 |
+
" <td>other</td>\n",
|
| 1014 |
+
" <td>1662</td>\n",
|
| 1015 |
+
" </tr>\n",
|
| 1016 |
+
" </tbody>\n",
|
| 1017 |
+
"</table>\n",
|
| 1018 |
+
"</div>"
|
| 1019 |
+
],
|
| 1020 |
+
"text/plain": [
|
| 1021 |
+
" category count\n",
|
| 1022 |
+
"0 traffic 1740\n",
|
| 1023 |
+
"1 theft 1739\n",
|
| 1024 |
+
"2 noise 1735\n",
|
| 1025 |
+
"3 vandalism 1720\n",
|
| 1026 |
+
"4 disturbance 1702\n",
|
| 1027 |
+
"5 animal 1702\n",
|
| 1028 |
+
"6 other 1662"
|
| 1029 |
+
]
|
| 1030 |
+
},
|
| 1031 |
+
"metadata": {},
|
| 1032 |
+
"output_type": "display_data"
|
| 1033 |
+
},
|
| 1034 |
+
{
|
| 1035 |
+
"name": "stdout",
|
| 1036 |
+
"output_type": "stream",
|
| 1037 |
+
"text": [
|
| 1038 |
+
"Saved table: ./tables\\safety_incidents_by_category.csv\n",
|
| 1039 |
+
"Saved figure: ./figs\\07_safety_category_bar.png\n"
|
| 1040 |
+
]
|
| 1041 |
+
}
|
| 1042 |
+
],
|
| 1043 |
+
"source": [
|
| 1044 |
+
"safety_cat = safety[\"category\"].value_counts().reset_index()\n",
|
| 1045 |
+
"safety_cat.columns = [\"category\",\"count\"]\n",
|
| 1046 |
+
"display(safety_cat)\n",
|
| 1047 |
+
"savetab(safety_cat, \"safety_incidents_by_category.csv\")\n",
|
| 1048 |
+
"\n",
|
| 1049 |
+
"plt.figure(figsize=(7,4))\n",
|
| 1050 |
+
"safety_cat.set_index(\"category\")[\"count\"].plot(kind=\"bar\")\n",
|
| 1051 |
+
"plt.title(\"Public Safety Incidents by Category\")\n",
|
| 1052 |
+
"plt.xlabel(\"Category\"); plt.ylabel(\"Count\")\n",
|
| 1053 |
+
"savefig(\"07_safety_category_bar.png\")"
|
| 1054 |
+
]
|
| 1055 |
+
},
|
| 1056 |
+
{
|
| 1057 |
+
"cell_type": "code",
|
| 1058 |
+
"execution_count": 18,
|
| 1059 |
+
"id": "c8e99a14",
|
| 1060 |
+
"metadata": {},
|
| 1061 |
+
"outputs": [
|
| 1062 |
+
{
|
| 1063 |
+
"name": "stdout",
|
| 1064 |
+
"output_type": "stream",
|
| 1065 |
+
"text": [
|
| 1066 |
+
"Saved figure: ./figs\\08_noise_hist.png\n",
|
| 1067 |
+
"Saved figure: ./figs\\09_aqi_hist.png\n"
|
| 1068 |
+
]
|
| 1069 |
+
}
|
| 1070 |
+
],
|
| 1071 |
+
"source": [
|
| 1072 |
+
"noise = obs[obs[\"metric\"]==\"noise_db\"].copy()\n",
|
| 1073 |
+
"aqi = obs[obs[\"metric\"]==\"aqi\"].copy()\n",
|
| 1074 |
+
"\n",
|
| 1075 |
+
"# Noise distribution\n",
|
| 1076 |
+
"plt.figure(figsize=(7,4))\n",
|
| 1077 |
+
"noise[\"value\"].plot(kind=\"hist\", bins=30)\n",
|
| 1078 |
+
"plt.title(\"Noise (dB) — Distribution\")\n",
|
| 1079 |
+
"plt.xlabel(\"dB\"); plt.ylabel(\"Frequency\")\n",
|
| 1080 |
+
"savefig(\"08_noise_hist.png\")\n",
|
| 1081 |
+
"\n",
|
| 1082 |
+
"# AQI distribution\n",
|
| 1083 |
+
"plt.figure(figsize=(7,4))\n",
|
| 1084 |
+
"aqi[\"value\"].plot(kind=\"hist\", bins=30)\n",
|
| 1085 |
+
"plt.title(\"AQI — Distribution\")\n",
|
| 1086 |
+
"plt.xlabel(\"AQI\"); plt.ylabel(\"Frequency\")\n",
|
| 1087 |
+
"savefig(\"09_aqi_hist.png\")"
|
| 1088 |
+
]
|
| 1089 |
+
},
|
| 1090 |
+
{
|
| 1091 |
+
"cell_type": "code",
|
| 1092 |
+
"execution_count": 19,
|
| 1093 |
+
"id": "fc79bee7",
|
| 1094 |
+
"metadata": {},
|
| 1095 |
+
"outputs": [
|
| 1096 |
+
{
|
| 1097 |
+
"data": {
|
| 1098 |
+
"text/html": [
|
| 1099 |
+
"<div>\n",
|
| 1100 |
+
"<style scoped>\n",
|
| 1101 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 1102 |
+
" vertical-align: middle;\n",
|
| 1103 |
+
" }\n",
|
| 1104 |
+
"\n",
|
| 1105 |
+
" .dataframe tbody tr th {\n",
|
| 1106 |
+
" vertical-align: top;\n",
|
| 1107 |
+
" }\n",
|
| 1108 |
+
"\n",
|
| 1109 |
+
" .dataframe thead th {\n",
|
| 1110 |
+
" text-align: right;\n",
|
| 1111 |
+
" }\n",
|
| 1112 |
+
"</style>\n",
|
| 1113 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 1114 |
+
" <thead>\n",
|
| 1115 |
+
" <tr style=\"text-align: right;\">\n",
|
| 1116 |
+
" <th></th>\n",
|
| 1117 |
+
" <th>year</th>\n",
|
| 1118 |
+
" <th>attendees_est</th>\n",
|
| 1119 |
+
" </tr>\n",
|
| 1120 |
+
" </thead>\n",
|
| 1121 |
+
" <tbody>\n",
|
| 1122 |
+
" <tr>\n",
|
| 1123 |
+
" <th>0</th>\n",
|
| 1124 |
+
" <td>1946</td>\n",
|
| 1125 |
+
" <td>2999</td>\n",
|
| 1126 |
+
" </tr>\n",
|
| 1127 |
+
" <tr>\n",
|
| 1128 |
+
" <th>1</th>\n",
|
| 1129 |
+
" <td>1947</td>\n",
|
| 1130 |
+
" <td>2999</td>\n",
|
| 1131 |
+
" </tr>\n",
|
| 1132 |
+
" <tr>\n",
|
| 1133 |
+
" <th>2</th>\n",
|
| 1134 |
+
" <td>1948</td>\n",
|
| 1135 |
+
" <td>3000</td>\n",
|
| 1136 |
+
" </tr>\n",
|
| 1137 |
+
" <tr>\n",
|
| 1138 |
+
" <th>3</th>\n",
|
| 1139 |
+
" <td>1949</td>\n",
|
| 1140 |
+
" <td>2999</td>\n",
|
| 1141 |
+
" </tr>\n",
|
| 1142 |
+
" <tr>\n",
|
| 1143 |
+
" <th>4</th>\n",
|
| 1144 |
+
" <td>1950</td>\n",
|
| 1145 |
+
" <td>2999</td>\n",
|
| 1146 |
+
" </tr>\n",
|
| 1147 |
+
" </tbody>\n",
|
| 1148 |
+
"</table>\n",
|
| 1149 |
+
"</div>"
|
| 1150 |
+
],
|
| 1151 |
+
"text/plain": [
|
| 1152 |
+
" year attendees_est\n",
|
| 1153 |
+
"0 1946 2999\n",
|
| 1154 |
+
"1 1947 2999\n",
|
| 1155 |
+
"2 1948 3000\n",
|
| 1156 |
+
"3 1949 2999\n",
|
| 1157 |
+
"4 1950 2999"
|
| 1158 |
+
]
|
| 1159 |
+
},
|
| 1160 |
+
"metadata": {},
|
| 1161 |
+
"output_type": "display_data"
|
| 1162 |
+
},
|
| 1163 |
+
{
|
| 1164 |
+
"name": "stdout",
|
| 1165 |
+
"output_type": "stream",
|
| 1166 |
+
"text": [
|
| 1167 |
+
"Saved table: ./tables\\events_attendance_by_year.csv\n",
|
| 1168 |
+
"Saved figure: ./figs\\10_events_attendance_by_year.png\n"
|
| 1169 |
+
]
|
| 1170 |
+
}
|
| 1171 |
+
],
|
| 1172 |
+
"source": [
|
| 1173 |
+
"events['year'] = pd.to_datetime(events['date']).dt.year\n",
|
| 1174 |
+
"att_year = events.groupby('year')[\"attendees_est\"].sum().reset_index()\n",
|
| 1175 |
+
"display(att_year.head())\n",
|
| 1176 |
+
"savetab(att_year, \"events_attendance_by_year.csv\")\n",
|
| 1177 |
+
"\n",
|
| 1178 |
+
"plt.figure(figsize=(8,4))\n",
|
| 1179 |
+
"plt.plot(att_year[\"year\"], att_year[\"attendees_est\"])\n",
|
| 1180 |
+
"plt.title(\"Street‑Band Festival — Total Attendance by Year\")\n",
|
| 1181 |
+
"plt.xlabel(\"Year\"); plt.ylabel(\"Estimated attendees\")\n",
|
| 1182 |
+
"savefig(\"10_events_attendance_by_year.png\")"
|
| 1183 |
+
]
|
| 1184 |
+
},
|
| 1185 |
+
{
|
| 1186 |
+
"cell_type": "code",
|
| 1187 |
+
"execution_count": 20,
|
| 1188 |
+
"id": "f75741ef",
|
| 1189 |
+
"metadata": {},
|
| 1190 |
+
"outputs": [
|
| 1191 |
+
{
|
| 1192 |
+
"name": "stdout",
|
| 1193 |
+
"output_type": "stream",
|
| 1194 |
+
"text": [
|
| 1195 |
+
"Saved table: ./tables\\transit_ridership_by_year_pivot.csv\n",
|
| 1196 |
+
"Saved figure: ./figs\\11_transit_ridership_lines.png\n"
|
| 1197 |
+
]
|
| 1198 |
+
}
|
| 1199 |
+
],
|
| 1200 |
+
"source": [
|
| 1201 |
+
"ridership['year'] = pd.to_datetime(ridership['date']).dt.year\n",
|
| 1202 |
+
"rid_pvt = ridership.pivot_table(index=\"year\", columns=\"line_id\", values=\"boardings_est\", aggfunc=\"sum\")\n",
|
| 1203 |
+
"savetab(rid_pvt.reset_index(), \"transit_ridership_by_year_pivot.csv\")\n",
|
| 1204 |
+
"\n",
|
| 1205 |
+
"plt.figure(figsize=(9,5))\n",
|
| 1206 |
+
"for col in rid_pvt.columns:\n",
|
| 1207 |
+
" plt.plot(rid_pvt.index, rid_pvt[col])\n",
|
| 1208 |
+
"plt.title(\"Transit Ridership — Boardings by Line over Time\")\n",
|
| 1209 |
+
"plt.xlabel(\"Year\"); plt.ylabel(\"Boardings (est.)\")\n",
|
| 1210 |
+
"savefig(\"11_transit_ridership_lines.png\")"
|
| 1211 |
+
]
|
| 1212 |
+
},
|
| 1213 |
+
{
|
| 1214 |
+
"cell_type": "code",
|
| 1215 |
+
"execution_count": 21,
|
| 1216 |
+
"id": "c857cfce",
|
| 1217 |
+
"metadata": {},
|
| 1218 |
+
"outputs": [
|
| 1219 |
+
{
|
| 1220 |
+
"name": "stdout",
|
| 1221 |
+
"output_type": "stream",
|
| 1222 |
+
"text": [
|
| 1223 |
+
"Saved figure: ./figs\\12_traffic_scatter.png\n"
|
| 1224 |
+
]
|
| 1225 |
+
},
|
| 1226 |
+
{
|
| 1227 |
+
"data": {
|
| 1228 |
+
"text/html": [
|
| 1229 |
+
"<div>\n",
|
| 1230 |
+
"<style scoped>\n",
|
| 1231 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 1232 |
+
" vertical-align: middle;\n",
|
| 1233 |
+
" }\n",
|
| 1234 |
+
"\n",
|
| 1235 |
+
" .dataframe tbody tr th {\n",
|
| 1236 |
+
" vertical-align: top;\n",
|
| 1237 |
+
" }\n",
|
| 1238 |
+
"\n",
|
| 1239 |
+
" .dataframe thead th {\n",
|
| 1240 |
+
" text-align: right;\n",
|
| 1241 |
+
" }\n",
|
| 1242 |
+
"</style>\n",
|
| 1243 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 1244 |
+
" <thead>\n",
|
| 1245 |
+
" <tr style=\"text-align: right;\">\n",
|
| 1246 |
+
" <th></th>\n",
|
| 1247 |
+
" <th>facility_type</th>\n",
|
| 1248 |
+
" <th>count</th>\n",
|
| 1249 |
+
" </tr>\n",
|
| 1250 |
+
" </thead>\n",
|
| 1251 |
+
" <tbody>\n",
|
| 1252 |
+
" <tr>\n",
|
| 1253 |
+
" <th>0</th>\n",
|
| 1254 |
+
" <td>painted_lane</td>\n",
|
| 1255 |
+
" <td>6</td>\n",
|
| 1256 |
+
" </tr>\n",
|
| 1257 |
+
" <tr>\n",
|
| 1258 |
+
" <th>1</th>\n",
|
| 1259 |
+
" <td>protected_lane</td>\n",
|
| 1260 |
+
" <td>2</td>\n",
|
| 1261 |
+
" </tr>\n",
|
| 1262 |
+
" <tr>\n",
|
| 1263 |
+
" <th>2</th>\n",
|
| 1264 |
+
" <td>shared</td>\n",
|
| 1265 |
+
" <td>1</td>\n",
|
| 1266 |
+
" </tr>\n",
|
| 1267 |
+
" </tbody>\n",
|
| 1268 |
+
"</table>\n",
|
| 1269 |
+
"</div>"
|
| 1270 |
+
],
|
| 1271 |
+
"text/plain": [
|
| 1272 |
+
" facility_type count\n",
|
| 1273 |
+
"0 painted_lane 6\n",
|
| 1274 |
+
"1 protected_lane 2\n",
|
| 1275 |
+
"2 shared 1"
|
| 1276 |
+
]
|
| 1277 |
+
},
|
| 1278 |
+
"metadata": {},
|
| 1279 |
+
"output_type": "display_data"
|
| 1280 |
+
},
|
| 1281 |
+
{
|
| 1282 |
+
"name": "stdout",
|
| 1283 |
+
"output_type": "stream",
|
| 1284 |
+
"text": [
|
| 1285 |
+
"Saved table: ./tables\\bike_facility_counts.csv\n",
|
| 1286 |
+
"Saved figure: ./figs\\13_bike_facility_bar.png\n"
|
| 1287 |
+
]
|
| 1288 |
+
}
|
| 1289 |
+
],
|
| 1290 |
+
"source": [
|
| 1291 |
+
"plt.figure(figsize=(6,5))\n",
|
| 1292 |
+
"plt.scatter(traffic[\"vehicles_per_day\"], traffic[\"bikes_per_day\"], alpha=0.5)\n",
|
| 1293 |
+
"plt.title(\"Traffic: Vehicles vs Bikes per Day\")\n",
|
| 1294 |
+
"plt.xlabel(\"Vehicles/day\"); plt.ylabel(\"Bikes/day\")\n",
|
| 1295 |
+
"savefig(\"12_traffic_scatter.png\")\n",
|
| 1296 |
+
"\n",
|
| 1297 |
+
"bike_counts = bike[\"facility_type\"].value_counts().reset_index()\n",
|
| 1298 |
+
"bike_counts.columns = [\"facility_type\",\"count\"]\n",
|
| 1299 |
+
"display(bike_counts)\n",
|
| 1300 |
+
"savetab(bike_counts, \"bike_facility_counts.csv\")\n",
|
| 1301 |
+
"\n",
|
| 1302 |
+
"plt.figure(figsize=(6,4))\n",
|
| 1303 |
+
"bike_counts.set_index(\"facility_type\")[\"count\"].plot(kind=\"bar\")\n",
|
| 1304 |
+
"plt.title(\"Bike Facility Types\")\n",
|
| 1305 |
+
"plt.xlabel(\"Facility type\"); plt.ylabel(\"Count\")\n",
|
| 1306 |
+
"savefig(\"13_bike_facility_bar.png\")"
|
| 1307 |
+
]
|
| 1308 |
+
},
|
| 1309 |
+
{
|
| 1310 |
+
"cell_type": "code",
|
| 1311 |
+
"execution_count": 22,
|
| 1312 |
+
"id": "9a238c42",
|
| 1313 |
+
"metadata": {},
|
| 1314 |
+
"outputs": [
|
| 1315 |
+
{
|
| 1316 |
+
"name": "stdout",
|
| 1317 |
+
"output_type": "stream",
|
| 1318 |
+
"text": [
|
| 1319 |
+
"Saved table: ./tables\\weather_temp_monthly_mean.csv\n",
|
| 1320 |
+
"Saved figure: ./figs\\14_weather_temp_monthly.png\n",
|
| 1321 |
+
"Saved table: ./tables\\weather_rain_monthly_sum.csv\n",
|
| 1322 |
+
"Saved figure: ./figs\\15_weather_rain_monthly.png\n",
|
| 1323 |
+
"Saved table: ./tables\\weather_snow_monthly_sum.csv\n",
|
| 1324 |
+
"Saved figure: ./figs\\16_weather_snow_monthly.png\n"
|
| 1325 |
+
]
|
| 1326 |
+
}
|
| 1327 |
+
],
|
| 1328 |
+
"source": [
|
| 1329 |
+
"weather['month'] = pd.to_datetime(weather['date']).dt.month\n",
|
| 1330 |
+
"temp_month = weather.groupby('month')[\"temp_c\"].mean().reset_index()\n",
|
| 1331 |
+
"savetab(temp_month, \"weather_temp_monthly_mean.csv\")\n",
|
| 1332 |
+
"\n",
|
| 1333 |
+
"plt.figure(figsize=(7,4))\n",
|
| 1334 |
+
"plt.plot(temp_month[\"month\"], temp_month[\"temp_c\"], marker=\"o\")\n",
|
| 1335 |
+
"plt.title(\"Avg Temperature by Month\")\n",
|
| 1336 |
+
"plt.xlabel(\"Month\"); plt.ylabel(\"Temp (°C)\")\n",
|
| 1337 |
+
"savefig(\"14_weather_temp_monthly.png\")\n",
|
| 1338 |
+
"\n",
|
| 1339 |
+
"rain_month = weather.groupby('month')[\"precip_mm\"].sum().reset_index()\n",
|
| 1340 |
+
"savetab(rain_month, \"weather_rain_monthly_sum.csv\")\n",
|
| 1341 |
+
"\n",
|
| 1342 |
+
"plt.figure(figsize=(7,4))\n",
|
| 1343 |
+
"plt.bar(rain_month[\"month\"], rain_month[\"precip_mm\"])\n",
|
| 1344 |
+
"plt.title(\"Total Rainfall by Month\")\n",
|
| 1345 |
+
"plt.xlabel(\"Month\"); plt.ylabel(\"Precipitation (mm)\")\n",
|
| 1346 |
+
"savefig(\"15_weather_rain_monthly.png\")\n",
|
| 1347 |
+
"\n",
|
| 1348 |
+
"snow_month = weather.groupby('month')[\"snow_cm\"].sum().reset_index()\n",
|
| 1349 |
+
"savetab(snow_month, \"weather_snow_monthly_sum.csv\")\n",
|
| 1350 |
+
"\n",
|
| 1351 |
+
"plt.figure(figsize=(7,4))\n",
|
| 1352 |
+
"plt.bar(snow_month[\"month\"], snow_month[\"snow_cm\"])\n",
|
| 1353 |
+
"plt.title(\"Total Snowfall by Month\")\n",
|
| 1354 |
+
"plt.xlabel(\"Month\"); plt.ylabel(\"Snow (cm)\")\n",
|
| 1355 |
+
"savefig(\"16_weather_snow_monthly.png\")"
|
| 1356 |
+
]
|
| 1357 |
+
},
|
| 1358 |
+
{
|
| 1359 |
+
"cell_type": "code",
|
| 1360 |
+
"execution_count": 23,
|
| 1361 |
+
"id": "d7b08fa7",
|
| 1362 |
+
"metadata": {},
|
| 1363 |
+
"outputs": [
|
| 1364 |
+
{
|
| 1365 |
+
"data": {
|
| 1366 |
+
"text/html": [
|
| 1367 |
+
"<div>\n",
|
| 1368 |
+
"<style scoped>\n",
|
| 1369 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 1370 |
+
" vertical-align: middle;\n",
|
| 1371 |
+
" }\n",
|
| 1372 |
+
"\n",
|
| 1373 |
+
" .dataframe tbody tr th {\n",
|
| 1374 |
+
" vertical-align: top;\n",
|
| 1375 |
+
" }\n",
|
| 1376 |
+
"\n",
|
| 1377 |
+
" .dataframe thead th {\n",
|
| 1378 |
+
" text-align: right;\n",
|
| 1379 |
+
" }\n",
|
| 1380 |
+
"</style>\n",
|
| 1381 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 1382 |
+
" <thead>\n",
|
| 1383 |
+
" <tr style=\"text-align: right;\">\n",
|
| 1384 |
+
" <th></th>\n",
|
| 1385 |
+
" <th>type</th>\n",
|
| 1386 |
+
" <th>count</th>\n",
|
| 1387 |
+
" </tr>\n",
|
| 1388 |
+
" </thead>\n",
|
| 1389 |
+
" <tbody>\n",
|
| 1390 |
+
" <tr>\n",
|
| 1391 |
+
" <th>3</th>\n",
|
| 1392 |
+
" <td>water_main_break</td>\n",
|
| 1393 |
+
" <td>89</td>\n",
|
| 1394 |
+
" </tr>\n",
|
| 1395 |
+
" <tr>\n",
|
| 1396 |
+
" <th>0</th>\n",
|
| 1397 |
+
" <td>power_outage</td>\n",
|
| 1398 |
+
" <td>86</td>\n",
|
| 1399 |
+
" </tr>\n",
|
| 1400 |
+
" <tr>\n",
|
| 1401 |
+
" <th>1</th>\n",
|
| 1402 |
+
" <td>road_potholes</td>\n",
|
| 1403 |
+
" <td>54</td>\n",
|
| 1404 |
+
" </tr>\n",
|
| 1405 |
+
" <tr>\n",
|
| 1406 |
+
" <th>2</th>\n",
|
| 1407 |
+
" <td>tree_fall</td>\n",
|
| 1408 |
+
" <td>24</td>\n",
|
| 1409 |
+
" </tr>\n",
|
| 1410 |
+
" </tbody>\n",
|
| 1411 |
+
"</table>\n",
|
| 1412 |
+
"</div>"
|
| 1413 |
+
],
|
| 1414 |
+
"text/plain": [
|
| 1415 |
+
" type count\n",
|
| 1416 |
+
"3 water_main_break 89\n",
|
| 1417 |
+
"0 power_outage 86\n",
|
| 1418 |
+
"1 road_potholes 54\n",
|
| 1419 |
+
"2 tree_fall 24"
|
| 1420 |
+
]
|
| 1421 |
+
},
|
| 1422 |
+
"metadata": {},
|
| 1423 |
+
"output_type": "display_data"
|
| 1424 |
+
},
|
| 1425 |
+
{
|
| 1426 |
+
"name": "stdout",
|
| 1427 |
+
"output_type": "stream",
|
| 1428 |
+
"text": [
|
| 1429 |
+
"Saved table: ./tables\\infra_event_counts.csv\n",
|
| 1430 |
+
"Saved figure: ./figs\\17_infra_events_bar.png\n"
|
| 1431 |
+
]
|
| 1432 |
+
},
|
| 1433 |
+
{
|
| 1434 |
+
"data": {
|
| 1435 |
+
"text/html": [
|
| 1436 |
+
"<div>\n",
|
| 1437 |
+
"<style scoped>\n",
|
| 1438 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 1439 |
+
" vertical-align: middle;\n",
|
| 1440 |
+
" }\n",
|
| 1441 |
+
"\n",
|
| 1442 |
+
" .dataframe tbody tr th {\n",
|
| 1443 |
+
" vertical-align: top;\n",
|
| 1444 |
+
" }\n",
|
| 1445 |
+
"\n",
|
| 1446 |
+
" .dataframe thead th {\n",
|
| 1447 |
+
" text-align: right;\n",
|
| 1448 |
+
" }\n",
|
| 1449 |
+
"</style>\n",
|
| 1450 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 1451 |
+
" <thead>\n",
|
| 1452 |
+
" <tr style=\"text-align: right;\">\n",
|
| 1453 |
+
" <th></th>\n",
|
| 1454 |
+
" <th>type</th>\n",
|
| 1455 |
+
" <th>count</th>\n",
|
| 1456 |
+
" </tr>\n",
|
| 1457 |
+
" </thead>\n",
|
| 1458 |
+
" <tbody>\n",
|
| 1459 |
+
" <tr>\n",
|
| 1460 |
+
" <th>0</th>\n",
|
| 1461 |
+
" <td>hvac_failure</td>\n",
|
| 1462 |
+
" <td>676</td>\n",
|
| 1463 |
+
" </tr>\n",
|
| 1464 |
+
" <tr>\n",
|
| 1465 |
+
" <th>2</th>\n",
|
| 1466 |
+
" <td>roof_leak</td>\n",
|
| 1467 |
+
" <td>662</td>\n",
|
| 1468 |
+
" </tr>\n",
|
| 1469 |
+
" <tr>\n",
|
| 1470 |
+
" <th>1</th>\n",
|
| 1471 |
+
" <td>plumbing_issue</td>\n",
|
| 1472 |
+
" <td>276</td>\n",
|
| 1473 |
+
" </tr>\n",
|
| 1474 |
+
" </tbody>\n",
|
| 1475 |
+
"</table>\n",
|
| 1476 |
+
"</div>"
|
| 1477 |
+
],
|
| 1478 |
+
"text/plain": [
|
| 1479 |
+
" type count\n",
|
| 1480 |
+
"0 hvac_failure 676\n",
|
| 1481 |
+
"2 roof_leak 662\n",
|
| 1482 |
+
"1 plumbing_issue 276"
|
| 1483 |
+
]
|
| 1484 |
+
},
|
| 1485 |
+
"metadata": {},
|
| 1486 |
+
"output_type": "display_data"
|
| 1487 |
+
},
|
| 1488 |
+
{
|
| 1489 |
+
"name": "stdout",
|
| 1490 |
+
"output_type": "stream",
|
| 1491 |
+
"text": [
|
| 1492 |
+
"Saved table: ./tables\\building_issue_counts.csv\n",
|
| 1493 |
+
"Saved figure: ./figs\\18_building_issues_bar.png\n"
|
| 1494 |
+
]
|
| 1495 |
+
}
|
| 1496 |
+
],
|
| 1497 |
+
"source": [
|
| 1498 |
+
"infra['year'] = pd.to_datetime(infra['date']).dt.year\n",
|
| 1499 |
+
"infra_counts = infra.groupby(['type'])[\"infra_event_id\"].count().reset_index(name=\"count\").sort_values(\"count\", ascending=False)\n",
|
| 1500 |
+
"display(infra_counts)\n",
|
| 1501 |
+
"savetab(infra_counts, \"infra_event_counts.csv\")\n",
|
| 1502 |
+
"\n",
|
| 1503 |
+
"plt.figure(figsize=(7,4))\n",
|
| 1504 |
+
"infra_counts.set_index(\"type\")[\"count\"].plot(kind=\"bar\")\n",
|
| 1505 |
+
"plt.title(\"Infrastructure Events by Type\")\n",
|
| 1506 |
+
"plt.xlabel(\"Type\"); plt.ylabel(\"Count\")\n",
|
| 1507 |
+
"savefig(\"17_infra_events_bar.png\")\n",
|
| 1508 |
+
"\n",
|
| 1509 |
+
"bissues['year'] = pd.to_datetime(bissues['date']).dt.year\n",
|
| 1510 |
+
"bissues_counts = bissues.groupby(['type'])[\"building_issue_id\"].count().reset_index(name=\"count\").sort_values(\"count\", ascending=False)\n",
|
| 1511 |
+
"display(bissues_counts)\n",
|
| 1512 |
+
"savetab(bissues_counts, \"building_issue_counts.csv\")\n",
|
| 1513 |
+
"\n",
|
| 1514 |
+
"plt.figure(figsize=(7,4))\n",
|
| 1515 |
+
"bissues_counts.set_index(\"type\")[\"count\"].plot(kind=\"bar\")\n",
|
| 1516 |
+
"plt.title(\"Building Issues by Type\")\n",
|
| 1517 |
+
"plt.xlabel(\"Type\"); plt.ylabel(\"Count\")\n",
|
| 1518 |
+
"savefig(\"18_building_issues_bar.png\")"
|
| 1519 |
+
]
|
| 1520 |
+
},
|
| 1521 |
+
{
|
| 1522 |
+
"cell_type": "code",
|
| 1523 |
+
"execution_count": 24,
|
| 1524 |
+
"id": "8e57aa50",
|
| 1525 |
+
"metadata": {},
|
| 1526 |
+
"outputs": [
|
| 1527 |
+
{
|
| 1528 |
+
"name": "stdout",
|
| 1529 |
+
"output_type": "stream",
|
| 1530 |
+
"text": [
|
| 1531 |
+
"Saved figure: ./figs\\19_prices_housing_line.png\n",
|
| 1532 |
+
"Saved figure: ./figs\\20_prices_food_line.png\n",
|
| 1533 |
+
"Saved figure: ./figs\\21_prices_transit_line.png\n"
|
| 1534 |
+
]
|
| 1535 |
+
}
|
| 1536 |
+
],
|
| 1537 |
+
"source": [
|
| 1538 |
+
"plt.figure(figsize=(8,4))\n",
|
| 1539 |
+
"plt.plot(prices[\"year\"], prices[\"housing_idx\"])\n",
|
| 1540 |
+
"plt.title(\"Housing Price Index (Synthetic)\")\n",
|
| 1541 |
+
"plt.xlabel(\"Year\"); plt.ylabel(\"Index\")\n",
|
| 1542 |
+
"savefig(\"19_prices_housing_line.png\")\n",
|
| 1543 |
+
"\n",
|
| 1544 |
+
"plt.figure(figsize=(8,4))\n",
|
| 1545 |
+
"plt.plot(prices[\"year\"], prices[\"staple_food_idx\"])\n",
|
| 1546 |
+
"plt.title(\"Staple Food Index (Synthetic)\")\n",
|
| 1547 |
+
"plt.xlabel(\"Year\"); plt.ylabel(\"Index\")\n",
|
| 1548 |
+
"savefig(\"20_prices_food_line.png\")\n",
|
| 1549 |
+
"\n",
|
| 1550 |
+
"plt.figure(figsize=(8,4))\n",
|
| 1551 |
+
"plt.plot(prices[\"year\"], prices[\"transit_fare_idx\"])\n",
|
| 1552 |
+
"plt.title(\"Transit Fare Index (Synthetic)\")\n",
|
| 1553 |
+
"plt.xlabel(\"Year\"); plt.ylabel(\"Index\")\n",
|
| 1554 |
+
"savefig(\"21_prices_transit_line.png\")"
|
| 1555 |
+
]
|
| 1556 |
+
},
|
| 1557 |
+
{
|
| 1558 |
+
"cell_type": "markdown",
|
| 1559 |
+
"id": "765e0518",
|
| 1560 |
+
"metadata": {},
|
| 1561 |
+
"source": [
|
| 1562 |
+
"### Outputs\n",
|
| 1563 |
+
"- Figures saved under `FIGS_DIR`\n",
|
| 1564 |
+
"- Summary tables saved under `TABLES_DIR`\n",
|
| 1565 |
+
"\n",
|
| 1566 |
+
"You can zip these folders or publish selected images wherever you like.\n"
|
| 1567 |
+
]
|
| 1568 |
+
}
|
| 1569 |
+
],
|
| 1570 |
+
"metadata": {
|
| 1571 |
+
"kernelspec": {
|
| 1572 |
+
"display_name": "Python 3 (ipykernel)",
|
| 1573 |
+
"language": "python",
|
| 1574 |
+
"name": "python3"
|
| 1575 |
+
},
|
| 1576 |
+
"language_info": {
|
| 1577 |
+
"codemirror_mode": {
|
| 1578 |
+
"name": "ipython",
|
| 1579 |
+
"version": 3
|
| 1580 |
+
},
|
| 1581 |
+
"file_extension": ".py",
|
| 1582 |
+
"mimetype": "text/x-python",
|
| 1583 |
+
"name": "python",
|
| 1584 |
+
"nbconvert_exporter": "python",
|
| 1585 |
+
"pygments_lexer": "ipython3",
|
| 1586 |
+
"version": "3.12.7"
|
| 1587 |
+
}
|
| 1588 |
+
},
|
| 1589 |
+
"nbformat": 4,
|
| 1590 |
+
"nbformat_minor": 5
|
| 1591 |
+
}
|