spopt.region.WardSpatial

class spopt.region.WardSpatial(gdf, w, attrs_name, n_clusters=5, clustering_kwds={})[source]

Agglomerative clustering using Ward linkage with a spatial connectivity constraint.

Parameters:
gdfgeopandas.GeoDataFrame

Geodataframe containing original data.

wlibpysal.weights.W

Weights object created from given data.

attrs_namelist

Strings for attribute names (cols of geopandas.GeoDataFrame).

n_clustersint (default 5)

The number of clusters to form.

clustering_kwds: dict

Other parameters about clustering could be used in sklearn.cluster.AgglometariveClustering.

Returns:
labels_numpy.array

Cluster labels for observations.

Examples

>>> import geopandas
>>> import libpysal
>>> import numpy
>>> from spopt.region import WardSpatial

Read the data.

>>> libpysal.examples.load_example('AirBnB')
>>> pth = libpysal.examples.get_path('airbnb_Chicago 2015.shp')
>>> chicago = gpd.read_file(pth)

Initialize the parameters.

>>> w = libpysal.weights.Queen.from_dataframe(chicago)
>>> attrs_name = ['num_spots']
>>> n_clusters = 8

Run the WardSpatial algorithm.

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

Get the counts of region IDs for unit areas.

>>> numpy.array(numpy.unique(model.labels_, return_counts=True)).T
array([[ 0, 62],
       [ 1,  6],
       [ 2,  3],
       [ 3,  1],
       [ 4,  2],
       [ 5,  1],
       [ 6,  1],
       [ 7,  1]])
__init__(gdf, w, attrs_name, n_clusters=5, clustering_kwds={})[source]

Methods

__init__(gdf, w, attrs_name[, n_clusters, ...])

solve()

Solve the Ward

solve()[source]

Solve the Ward