import warnings
warnings.filterwarnings('ignore')
import sys
sys.path.append("../")
from spopt import WardSpatial
import geopandas as gpd
import libpysal
import numpy as np
Cluster 77 communities into 8 regions using Ward linkage with a spatial connectivity constraint.
pth = libpysal.examples.get_path('airbnb_Chicago 2015.shp')
chicago = gpd.read_file(pth)
chicago.plot(column='community', categorical=True, figsize=(12,8), edgecolor='w')
<AxesSubplot:>
attrs_name = ['num_spots']
w = libpysal.weights.Queen.from_dataframe(chicago)
n_clusters = 8
model = WardSpatial(chicago, w, attrs_name, n_clusters)
model.solve()
chicago['ward_new'] = model.labels_
chicago.plot(column='ward_new', categorical=True, figsize=(12,8), edgecolor='w')
<AxesSubplot:>