This page was generated from notebooks/ward.ipynb. Interactive online version:

# Ward¶

Author: Xin Feng

This algorithm is an agglomerative clustering using ward linkage with a spatial connectivity constraint. Specifically, it is a “bottom-up” approach: each zone starts as its own cluster, and pairs of clusters are chosen to merge at each step in order to minimally increase a given linkage distance. Ward linkage refers to the variance of the clusters being merged. `Ward`

algorithm in pysal/spopt is the function
(sklearn.cluster.AgglomerativeClustering) when the linkage criterion is ward.

```
[1]:
```

```
import sys
sys.path.append("../")
from spopt.region import WardSpatial
```

```
[2]:
```

```
import warnings
warnings.filterwarnings('ignore')
import geopandas as gpd
import libpysal
from libpysal.examples import load_example
import numpy as np
```

```
[3]:
```

```
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [12, 8]
```

## Airbnb Spots Clustering in Chicago¶

To illustrate `Ward`

we utilize data on Airbnb spots in Chicago, which can be downloaded from libpysal.examples.

We can first explore the data by plotting the number of Airbnb spots in each community in the sample, using a quintile classification:

```
[4]:
```

```
load_example('AirBnB')
```

```
[4]:
```

```
<libpysal.examples.base.Example at 0x1606592e0>
```

```
[5]:
```

```
pth = libpysal.examples.get_path('airbnb_Chicago 2015.shp')
chicago = gpd.read_file(pth)
chicago.plot(column='num_spots', scheme='Quantiles', cmap='GnBu', edgecolor='grey', legend=True)
```

```
[5]:
```

```
<AxesSubplot:>
```

## Regionalization¶

With `Ward`

, we can aggregate these 77 communities into 5 clusters. During the merging process, the variance of the clusters is minimized.

We first define the variable that will be used to measure the variance of clusters. The variable is the number of Airbnb spots in each community in this case.

```
[6]:
```

```
attrs_name = ['num_spots']
```

Next, we specify a number of other parameters that will serve as input to the `Ward`

model.

A spatial weights object describes the spatial connectivity of the spatial objects:

```
[7]:
```

```
w = libpysal.weights.Queen.from_dataframe(chicago)
```

The number of clusters that we would like to group these counties into:

```
[8]:
```

```
n_clusters = 5
```

There are also some optional parameters about clustering in (sklearn.cluster.AgglomerativeClustering). They can be added in the `Ward`

function as a dictionary. In this example, we only use the default settings, you can define them as needed.

The model can then be solved:

```
[9]:
```

```
model = WardSpatial(chicago, w, attrs_name, n_clusters)
model.solve()
```

```
[10]:
```

```
chicago['ward_new'] = model.labels_
```

```
[11]:
```

```
chicago['number'] = 1
chicago[['ward_new','number']].groupby(by='ward_new').count()
```

```
[11]:
```

number | |
---|---|

ward_new | |

0 | 3 |

1 | 2 |

2 | 3 |

3 | 62 |

4 | 7 |

```
[12]:
```

```
chicago.plot(column='ward_new', categorical=True, edgecolor='w')
```

```
[12]:
```

```
<AxesSubplot:>
```

The model solution results in five clusters, two of which have three communities, one with two, one with seven, and one with sixty-two communities.