import warnings
warnings.filterwarnings('ignore')
import sys
sys.path.append("../")
from spopt.region.skater import Skater
import geopandas as gpd
import libpysal
from libpysal.examples import load_example
import numpy as np
from sklearn.metrics import pairwise as skm
Cluster 77 communities into 10 regions such that each region consists of at least 3 communities and homogeneity in the number of Airbnb spots in communities is maximized.
load_example('AirBnB')
<libpysal.examples.base.Example at 0x16afa9b20>
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:>
Initialize the parameters
w = libpysal.weights.Queen.from_dataframe(chicago)
attrs_name = ['num_spots']
n_clusters = 10
floor = 3
trace = False
islands = "increase"
spanning_forest_kwds = dict(
dissimilarity=skm.manhattan_distances, affinity=None, reduction=np.sum, center=np.mean
)
model = Skater(chicago, w, attrs_name, n_clusters, floor, trace, islands, spanning_forest_kwds)
model.solve()
chicago['skater_new'] = model.labels_
chicago.plot(column='skater_new', categorical=True, figsize=(12,8), edgecolor='w')
<AxesSubplot:>