docs: Complete Phase 6 notebooks and Phase 7 documentation review
Phase 6 - Jupyter Notebooks (15 total): - Overview tab: livability_choropleth, top_bottom_10_bar, income_safety_scatter - Housing tab: affordability_choropleth, rent_trend_line, tenure_breakdown_bar - Safety tab: crime_rate_choropleth, crime_breakdown_bar, crime_trend_line - Demographics tab: income_choropleth, age_distribution, population_density_bar - Amenities tab: amenity_index_choropleth, amenity_radar, transit_accessibility_bar Phase 7 - Documentation: - Updated CLAUDE.md with Sprint 9 completion status - Added notebooks directory to application structure - Expanded figures directory listing Closes #71, #72, #73, #74, #75, #76, #77 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
183
notebooks/overview/income_safety_scatter.ipynb
Normal file
183
notebooks/overview/income_safety_scatter.ipynb
Normal file
@@ -0,0 +1,183 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Income vs Safety Scatter Plot\n",
|
||||
"\n",
|
||||
"Explores the correlation between median household income and safety score across Toronto neighbourhoods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 1. Data Reference\n",
|
||||
"\n",
|
||||
"### Source Tables\n",
|
||||
"\n",
|
||||
"| Table | Grain | Key Columns |\n",
|
||||
"|-------|-------|-------------|\n",
|
||||
"| `mart_neighbourhood_overview` | neighbourhood × year | neighbourhood_name, median_household_income, safety_score, population |\n",
|
||||
"\n",
|
||||
"### SQL Query"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"from sqlalchemy import create_engine\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"engine = create_engine(os.environ.get('DATABASE_URL', 'postgresql://portfolio:portfolio@localhost:5432/portfolio'))\n",
|
||||
"\n",
|
||||
"query = \"\"\"\n",
|
||||
"SELECT\n",
|
||||
" neighbourhood_name,\n",
|
||||
" median_household_income,\n",
|
||||
" safety_score,\n",
|
||||
" population,\n",
|
||||
" livability_score,\n",
|
||||
" crime_rate_per_100k\n",
|
||||
"FROM mart_neighbourhood_overview\n",
|
||||
"WHERE year = (SELECT MAX(year) FROM mart_neighbourhood_overview)\n",
|
||||
" AND median_household_income IS NOT NULL\n",
|
||||
" AND safety_score IS NOT NULL\n",
|
||||
"ORDER BY median_household_income DESC\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"df = pd.read_sql(query, engine)\n",
|
||||
"print(f\"Loaded {len(df)} neighbourhoods with income and safety data\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Transformation Steps\n",
|
||||
"\n",
|
||||
"1. Filter out null values for income and safety\n",
|
||||
"2. Optionally scale income to thousands for readability\n",
|
||||
"3. Pass to scatter figure factory with optional trendline"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Scale income to thousands for better axis readability\n",
|
||||
"df['income_thousands'] = df['median_household_income'] / 1000\n",
|
||||
"\n",
|
||||
"# Prepare data for figure factory\n",
|
||||
"data = df.to_dict('records')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Sample Output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df[['neighbourhood_name', 'median_household_income', 'safety_score', 'crime_rate_per_100k']].head(10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2. Data Visualization\n",
|
||||
"\n",
|
||||
"### Figure Factory\n",
|
||||
"\n",
|
||||
"Uses `create_scatter_figure` from `portfolio_app.figures.scatter`.\n",
|
||||
"\n",
|
||||
"**Key Parameters:**\n",
|
||||
"- `x_column`: 'income_thousands' (median household income in $K)\n",
|
||||
"- `y_column`: 'safety_score' (0-100 percentile rank)\n",
|
||||
"- `name_column`: 'neighbourhood_name' (hover label)\n",
|
||||
"- `size_column`: 'population' (optional, bubble size)\n",
|
||||
"- `trendline`: True (adds OLS regression line)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"sys.path.insert(0, '../..')\n",
|
||||
"\n",
|
||||
"from portfolio_app.figures.scatter import create_scatter_figure\n",
|
||||
"\n",
|
||||
"fig = create_scatter_figure(\n",
|
||||
" data=data,\n",
|
||||
" x_column='income_thousands',\n",
|
||||
" y_column='safety_score',\n",
|
||||
" name_column='neighbourhood_name',\n",
|
||||
" size_column='population',\n",
|
||||
" title='Income vs Safety by Neighbourhood',\n",
|
||||
" x_title='Median Household Income ($K)',\n",
|
||||
" y_title='Safety Score (0-100)',\n",
|
||||
" trendline=True,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"fig.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Interpretation\n",
|
||||
"\n",
|
||||
"This scatter plot reveals the relationship between income and safety:\n",
|
||||
"\n",
|
||||
"- **Positive correlation**: Higher income neighbourhoods tend to have higher safety scores\n",
|
||||
"- **Bubble size**: Represents population (larger = more people)\n",
|
||||
"- **Trendline**: Orange dashed line shows the overall trend\n",
|
||||
"- **Outliers**: Neighbourhoods far from the trendline are interesting cases\n",
|
||||
" - Above line: Safer than income would predict\n",
|
||||
" - Below line: Less safe than income would predict"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Calculate correlation coefficient\n",
|
||||
"correlation = df['median_household_income'].corr(df['safety_score'])\n",
|
||||
"print(f\"Correlation coefficient (Income vs Safety): {correlation:.3f}\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.11.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
184
notebooks/overview/livability_choropleth.ipynb
Normal file
184
notebooks/overview/livability_choropleth.ipynb
Normal file
@@ -0,0 +1,184 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Livability Score Choropleth Map\n",
|
||||
"\n",
|
||||
"Displays neighbourhood livability scores on an interactive map of Toronto's 158 neighbourhoods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 1. Data Reference\n",
|
||||
"\n",
|
||||
"### Source Tables\n",
|
||||
"\n",
|
||||
"| Table | Grain | Key Columns |\n",
|
||||
"|-------|-------|-------------|\n",
|
||||
"| `mart_neighbourhood_overview` | neighbourhood × year | livability_score, safety_score, affordability_score, amenity_score, geometry |\n",
|
||||
"\n",
|
||||
"### SQL Query"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"from sqlalchemy import create_engine\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# Connect to database\n",
|
||||
"engine = create_engine(os.environ.get('DATABASE_URL', 'postgresql://portfolio:portfolio@localhost:5432/portfolio'))\n",
|
||||
"\n",
|
||||
"query = \"\"\"\n",
|
||||
"SELECT\n",
|
||||
" neighbourhood_id,\n",
|
||||
" neighbourhood_name,\n",
|
||||
" geometry,\n",
|
||||
" year,\n",
|
||||
" livability_score,\n",
|
||||
" safety_score,\n",
|
||||
" affordability_score,\n",
|
||||
" amenity_score,\n",
|
||||
" population,\n",
|
||||
" median_household_income\n",
|
||||
"FROM mart_neighbourhood_overview\n",
|
||||
"WHERE year = (SELECT MAX(year) FROM mart_neighbourhood_overview)\n",
|
||||
"ORDER BY livability_score DESC\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"df = pd.read_sql(query, engine)\n",
|
||||
"print(f\"Loaded {len(df)} neighbourhoods\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Transformation Steps\n",
|
||||
"\n",
|
||||
"1. Filter to most recent year of data\n",
|
||||
"2. Extract GeoJSON from PostGIS geometry column\n",
|
||||
"3. Pass to choropleth figure factory"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Transform geometry to GeoJSON\n",
|
||||
"import geopandas as gpd\n",
|
||||
"import json\n",
|
||||
"\n",
|
||||
"# Convert WKB geometry to GeoDataFrame\n",
|
||||
"gdf = gpd.GeoDataFrame(\n",
|
||||
" df,\n",
|
||||
" geometry=gpd.GeoSeries.from_wkb(df['geometry']),\n",
|
||||
" crs='EPSG:4326'\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Create GeoJSON FeatureCollection\n",
|
||||
"geojson = json.loads(gdf.to_json())\n",
|
||||
"\n",
|
||||
"# Prepare data for figure factory\n",
|
||||
"data = df.drop(columns=['geometry']).to_dict('records')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Sample Output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df[['neighbourhood_name', 'livability_score', 'safety_score', 'affordability_score', 'amenity_score']].head(10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2. Data Visualization\n",
|
||||
"\n",
|
||||
"### Figure Factory\n",
|
||||
"\n",
|
||||
"Uses `create_choropleth_figure` from `portfolio_app.figures.choropleth`.\n",
|
||||
"\n",
|
||||
"**Key Parameters:**\n",
|
||||
"- `geojson`: GeoJSON FeatureCollection with neighbourhood boundaries\n",
|
||||
"- `data`: List of dicts with neighbourhood_id and scores\n",
|
||||
"- `location_key`: 'neighbourhood_id'\n",
|
||||
"- `color_column`: 'livability_score' (or safety_score, etc.)\n",
|
||||
"- `color_scale`: 'RdYlGn' (red=low, yellow=mid, green=high)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"sys.path.insert(0, '../..')\n",
|
||||
"\n",
|
||||
"from portfolio_app.figures.choropleth import create_choropleth_figure\n",
|
||||
"\n",
|
||||
"fig = create_choropleth_figure(\n",
|
||||
" geojson=geojson,\n",
|
||||
" data=data,\n",
|
||||
" location_key='neighbourhood_id',\n",
|
||||
" color_column='livability_score',\n",
|
||||
" hover_data=['neighbourhood_name', 'safety_score', 'affordability_score', 'amenity_score'],\n",
|
||||
" color_scale='RdYlGn',\n",
|
||||
" title='Toronto Neighbourhood Livability Score',\n",
|
||||
" zoom=10,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"fig.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Score Components\n",
|
||||
"\n",
|
||||
"The livability score is a weighted composite:\n",
|
||||
"\n",
|
||||
"| Component | Weight | Source |\n",
|
||||
"|-----------|--------|--------|\n",
|
||||
"| Safety | 30% | Inverse of crime rate per 100K |\n",
|
||||
"| Affordability | 40% | Inverse of rent-to-income ratio |\n",
|
||||
"| Amenities | 30% | Amenities per 1,000 residents |"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.11.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
167
notebooks/overview/top_bottom_10_bar.ipynb
Normal file
167
notebooks/overview/top_bottom_10_bar.ipynb
Normal file
@@ -0,0 +1,167 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Top & Bottom 10 Neighbourhoods Bar Chart\n",
|
||||
"\n",
|
||||
"Horizontal bar chart showing the highest and lowest scoring neighbourhoods by livability."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 1. Data Reference\n",
|
||||
"\n",
|
||||
"### Source Tables\n",
|
||||
"\n",
|
||||
"| Table | Grain | Key Columns |\n",
|
||||
"|-------|-------|-------------|\n",
|
||||
"| `mart_neighbourhood_overview` | neighbourhood × year | neighbourhood_name, livability_score |\n",
|
||||
"\n",
|
||||
"### SQL Query"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"from sqlalchemy import create_engine\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"engine = create_engine(os.environ.get('DATABASE_URL', 'postgresql://portfolio:portfolio@localhost:5432/portfolio'))\n",
|
||||
"\n",
|
||||
"query = \"\"\"\n",
|
||||
"SELECT\n",
|
||||
" neighbourhood_name,\n",
|
||||
" livability_score,\n",
|
||||
" safety_score,\n",
|
||||
" affordability_score,\n",
|
||||
" amenity_score\n",
|
||||
"FROM mart_neighbourhood_overview\n",
|
||||
"WHERE year = (SELECT MAX(year) FROM mart_neighbourhood_overview)\n",
|
||||
" AND livability_score IS NOT NULL\n",
|
||||
"ORDER BY livability_score DESC\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"df = pd.read_sql(query, engine)\n",
|
||||
"print(f\"Loaded {len(df)} neighbourhoods with scores\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Transformation Steps\n",
|
||||
"\n",
|
||||
"1. Sort by livability_score descending\n",
|
||||
"2. Take top 10 and bottom 10\n",
|
||||
"3. Pass to ranking bar figure factory"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# The figure factory handles top/bottom selection internally\n",
|
||||
"# Just prepare as list of dicts\n",
|
||||
"data = df.to_dict('records')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Sample Output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"Top 5:\")\n",
|
||||
"display(df.head(5))\n",
|
||||
"print(\"\\nBottom 5:\")\n",
|
||||
"display(df.tail(5))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2. Data Visualization\n",
|
||||
"\n",
|
||||
"### Figure Factory\n",
|
||||
"\n",
|
||||
"Uses `create_ranking_bar` from `portfolio_app.figures.bar_charts`.\n",
|
||||
"\n",
|
||||
"**Key Parameters:**\n",
|
||||
"- `data`: List of dicts with all neighbourhoods\n",
|
||||
"- `name_column`: 'neighbourhood_name'\n",
|
||||
"- `value_column`: 'livability_score'\n",
|
||||
"- `top_n`: 10 (green bars)\n",
|
||||
"- `bottom_n`: 10 (red bars)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"sys.path.insert(0, '../..')\n",
|
||||
"\n",
|
||||
"from portfolio_app.figures.bar_charts import create_ranking_bar\n",
|
||||
"\n",
|
||||
"fig = create_ranking_bar(\n",
|
||||
" data=data,\n",
|
||||
" name_column='neighbourhood_name',\n",
|
||||
" value_column='livability_score',\n",
|
||||
" title='Top & Bottom 10 Neighbourhoods by Livability',\n",
|
||||
" top_n=10,\n",
|
||||
" bottom_n=10,\n",
|
||||
" color_top='#4CAF50', # Green for top performers\n",
|
||||
" color_bottom='#F44336', # Red for bottom performers\n",
|
||||
" value_format='.1f',\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"fig.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Interpretation\n",
|
||||
"\n",
|
||||
"- **Green bars**: Highest livability scores (best combination of safety, affordability, and amenities)\n",
|
||||
"- **Red bars**: Lowest livability scores (areas that may need targeted investment)\n",
|
||||
"\n",
|
||||
"The ranking bar chart provides quick context for which neighbourhoods stand out at either extreme."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.11.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
Reference in New Issue
Block a user