{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Spatial weights in PySAL\n",
"\n",
"In this session, you will be learning the ins and outs of one of the key\n",
"pieces in spatial analysis: spatial weights matrices. These are\n",
"structured sets of numbers that formalise geographical relationships\n",
"between the observations in a dataset. Essentially, a spatial weights\n",
"matrix of a given geography is a positive definite matrix of dimensions\n",
"$N$ by $N$, where $N$ is the total number of observations:\n",
"\n",
"$$\n",
"W = \\left(\\begin{array}{cccc}\n",
"0 & w_{12} & \\dots & w_{1N} \\\\\n",
"w_{21} & \\ddots & w_{ij} & \\vdots \\\\\n",
"\\vdots & w_{ji} & 0 & \\vdots \\\\\n",
"w_{N1} & \\dots & \\dots & 0\n",
"\\end{array} \\right)\n",
"$$\n",
"\n",
"where each cell $w_{ij}$ contains a value that represents the degree of\n",
"spatial contact or interaction between observations $i$ and $j$. A\n",
"fundamental concept in this context is that of *neighbour* and\n",
"*neighbourhood*. By convention, elements in the diagonal ($w_{ii}$) are\n",
"set to zero. A *neighbour* of a given observation $i$ is another\n",
"observation with which $i$ has some degree of connection. In terms of\n",
"$W$, $i$ and $j$ are thus neighbors if $w_{ij} > 0$. Following this\n",
"logic, the neighbourhood of $i$ will be the set of observations in the\n",
"system with which it has a certain connection or those observations with\n",
"a weight greater than zero.\n",
"\n",
"There are several ways to create such matrices and many more to\n",
"transform them so they contain an accurate representation that aligns\n",
"with the way you understand spatial interactions between the elements of\n",
"a system. This session will introduce the most commonly used ones and\n",
"show how to compute them with `PySAL`."
],
"id": "b3d2a21c-a8de-419c-b9bf-1639338790c5"
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import contextily\n",
"import geopandas as gpd\n",
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"\n",
"from libpysal import graph"
],
"id": "d9f2da62"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data\n",
"\n",
"For this tutorial, you will use a dataset of Basic Settlement Units\n",
"(ZSJ) in Prague for 2021. The table is available as part of this course,\n",
"so it can be accessed remotely through the web. If you want to see how\n",
"the table was created, a notebook is available\n",
"[here](../data/prague_zsj_2021/preprocessing.ipynb).\n",
"\n",
"To make things easier, you will read data from a file posted online so,\n",
"for now, you do not need to download any dataset:"
],
"id": "29f0cd7e-d61b-4abf-ad69-82b227119d11"
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"metadata": {},
"data": {
"text/html": [
"

Make this Notebook Trusted to load map: File -> Trust Notebook