weights

weights(
    data,
    method='queen',
    *,
    k=6,
    threshold=None,
    power=1.0,
    row_standardize=True,
    attach_islands=True,
    gdf=None,
    ref=None,
)

Spatial weights for the units of a scale, a dataset or a panel.

Parameters

Name Type Description Default
data A scale id (every unit of that scale), a dataset id or a panel (the units with data), or a GeoDataFrame with geo_id required
method str "queen" (units sharing a border or a corner), "rook" (a border), "knn" (the k nearest), "distance_band" (within threshold metres) or "inverse_distance" (within the same band, weighted by distance ** -power) 'queen'
k int Neighbours for "knn" 6
threshold float | None Distance in metres for "distance_band" (default: the smallest distance at which every unit has a neighbour) None
power float Distance decay for "inverse_distance" 1.0
row_standardize bool Make each unit’s weights sum to one True
attach_islands bool Link a unit without contiguous neighbours to its nearest unit True
gdf Boundaries to use instead of the project’s None

Returns

Name Type Description
libpysal.weights.W A libpysal.weights.W keyed by geo_id, to pass as w= to the spatial functions. They build queen weights themselves when w is not given.

Example

!pip install -q geo4bolivia
import geo4bolivia as geo

w = geo.weights("municipality")                    # queen contiguity, 343 units
w6 = geo.weights("municipality", method="knn", k=6)   # the six nearest
print(w.n, round(w.mean_neighbors, 2), w6.n, w6.mean_neighbors)
geo.moran("sdg", "index_sdg", w=w6).fig
343 5.52 343 6.0