Cluster Espacial#

import geopandas as gpd
import matplotlib.pyplot as plt
import requests
gdf = gpd.read_file("https://github.com/algarciach/AnalisisGeoespacial/raw/main/Covid19_model/Data/covid19_municipios_antioquia.gpkg")
gdf.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 125 entries, 0 to 124
Data columns (total 15 columns):
 #   Column               Non-Null Count  Dtype   
---  ------               --------------  -----   
 0   codigo_municipio     125 non-null    object  
 1   nombre_municipio     125 non-null    object  
 2   codigo_subregion     125 non-null    object  
 3   nombre_subregion     125 non-null    object  
 4   area_municipio       125 non-null    float64 
 5   altitud              125 non-null    float64 
 6   temperatura          125 non-null    float64 
 7   humedad_relativa     125 non-null    float64 
 8   poblacion            125 non-null    int64   
 9   urbanizacion         125 non-null    float64 
 10  densidad             125 non-null    float64 
 11  muertes_covid19      125 non-null    int64   
 12  recuperados_covid19  125 non-null    int64   
 13  cfr                  125 non-null    float64 
 14  geometry             125 non-null    geometry
dtypes: float64(7), geometry(1), int64(3), object(4)
memory usage: 14.8+ KB

K-means#

Agrupamiento Espacial (Regionalización)#

var = ["altitud", "temperatura", "humedad_relativa", "urbanizacion", "densidad"]
from sklearn.preprocessing import StandardScaler
cat_std = StandardScaler().fit_transform(gdf[var])
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[4], line 1
----> 1 from sklearn.preprocessing import StandardScaler
      2 cat_std = StandardScaler().fit_transform(gdf[var])

ModuleNotFoundError: No module named 'sklearn'
from scipy.cluster.hierarchy import linkage, dendrogram
Z = linkage(cat_std, method='ward', metric='euclidean')
plt.figure(figsize=(3, 3))
plt.xlabel('Sample index')
plt.ylabel('Cluster Distance')
dendrogram(Z, leaf_rotation=90, leaf_font_size=8, color_threshold=0.2*83, truncate_mode='lastp', p=5, show_leaf_counts=True, show_contracted=True)
plt.title('Dendrogram of Clusters')
plt.tight_layout()
_images/31e4f8b4f8aa609e77e4b393ababdebeff02f4af81fb7b05b08ab30ac46b65bf.png
from sklearn import cluster
k5cls = cluster.KMeans(n_clusters=5).fit(cat_std)
gdf['kmeans5'] = k5cls.labels_
f, ax = plt.subplots(1, 1, figsize=(10, 10))
gdf.plot(column='kmeans5', categorical=True, linewidth=0, legend=True, ax=ax)
ax.set_axis_off()
_images/70b90a60de1e382dd0f7a9ac5850e1a54747283b344c83cc89dbbce5fe9ca703.png
f, ax = plt.subplots()
gdf.boxplot(column='altitud', by='kmeans5', ax=ax)
<Axes: title={'center': 'altitud'}, xlabel='kmeans5'>
_images/3c017226484be94b0fac602739b2facc0dd028388ba1e358bce1c5ad0d3f20a6.png
f, ax = plt.subplots()
gdf.boxplot(column='temperatura', by='kmeans5', ax=ax)
<Axes: title={'center': 'temperatura'}, xlabel='kmeans5'>
_images/1fdf48039c4431329e4e9edc5c3549fff6b7cb67c9e285e01ab2fabfe2467c6c.png
f, ax = plt.subplots()
gdf.boxplot(column='humedad_relativa', by='kmeans5', ax=ax)
<Axes: title={'center': 'humedad_relativa'}, xlabel='kmeans5'>
_images/6512476588167e04d8e82680ac945077911c96fd06aa55c74771f2e92a8b1695.png
f, ax = plt.subplots()
gdf.boxplot(column='urbanizacion', by='kmeans5', ax=ax)
<Axes: title={'center': 'urbanizacion'}, xlabel='kmeans5'>
_images/58dac88fe3ff96b2a919705dacee07d32011c769cde92a669b18862bc633bd12.png
f, ax = plt.subplots()
gdf.boxplot(column='densidad', by='kmeans5', ax=ax)
<Axes: title={'center': 'densidad'}, xlabel='kmeans5'>
_images/95c113cca6451e7d52ec4b270e2b8848f28a70627b79b0dd587260656b5ae273.png
from pysal.lib import weights

w_k4 = weights.distance.KNN.from_dataframe(gdf, k=5)
f, axs = plt.subplots(figsize=(10, 10))
ax = gdf.plot(edgecolor='k', facecolor='w', ax=axs)
w_k4.plot(gdf, ax=axs, edge_kws=dict(color='r', linestyle=':', linewidth=1), node_kws=dict(marker=''),)
axs.set_axis_off()
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1484: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.loc[neighbors].centroid
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/libpysal/weights/weights.py:1496: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  centroids = gdf.centroid
_images/f858f8671c237230dc5d08b08c920ab750e5b1de69dd7f527d4b76f6548a2e25.png
from sklearn.metrics.cluster import silhouette_score

cl_k4_wk4 = cluster.AgglomerativeClustering(n_clusters=5, connectivity=w_k4.sparse).fit(cat_std)
print(silhouette_score(cat_std, cl_k4_wk4.labels_))
0.16510355129590978
w_d30 = weights.DistanceBand.from_dataframe(gdf, 30000, binary=False)
cl_d30 = cluster.AgglomerativeClustering(n_clusters=5, connectivity=w_d30.sparse).fit(cat_std)
print(silhouette_score(cat_std,cl_d30.labels_))
0.2917484846601373
/usr/local/Caskroom/miniforge/base/envs/geo/lib/python3.11/site-packages/scipy/sparse/_data.py:128: RuntimeWarning: divide by zero encountered in reciprocal
  return self._with_data(data ** n)
gdf['knn5'] = cl_k4_wk4.labels_
f, ax = plt.subplots(1, figsize=(10, 10))
gdf.plot(column='knn5', categorical=True, linewidth=0, legend=True, ax=ax)
ax.set_axis_off()
_images/b93517df23e972014d334030090a1fddfab914d2dd450dd6d25f11cc61f8d1b2.png