top of page

How to Detect Hot Spots and Spatial Clusters Using PySAL

Writer: Anvita Shrivastava
Anvita Shrivastava
22 hours ago
6 min read

Patterns in space are an inherent component of geographic analyses. In most GIS and remote sensing processes, knowing only the location of features is not enough; you also need to understand the distribution of their corresponding values—whether they are clustered, dispersed, or randomly spread. Spatial clustering analysis detects statistically significant areas with high or low values in space.


PySAL is an open-source Python ecosystem for spatial data science. This framework offers various capabilities, including spatial statistics, spatial econometrics, network analysis, exploratory spatial data analysis, etc. By utilizing PySAL, GIS specialists and data scientists can conduct spatial autocorrelation and spatial clustering analysis.


PySAL
PySAL

What Are Hot Spots and Spatial Clusters?


A hot spot is defined as a region where values of a geographical phenomenon are highly concentrated geographically. An example includes a collection of neighborhoods within a city where crime rates, real estate prices, or diseases are at a high level.


On the other hand, a cold spot refers to a geographical region where low values are highly concentrated geographically. Spatial cluster analysis can also examine the connection between neighbors that have varying levels of values.


Some of the patterns associated with clusters include:


  • High-High (HH): High-value feature surrounded by high-value features.

  • Low-Low (LL): Low-value feature surrounded by low-value features.

  • High-Low (HL): High-value feature surrounded by low-value neighbors.

  • Low-High (LH): Low-value feature surrounded by high-value neighbors.


The above patterns are often studied using Local Indicators of Spatial Association (LISA).


Why Use PySAL for Spatial Cluster Detection?


Standard Python libraries for data analysis work great with tabular and numeric data analysis, but there is one more link between the features in spatial datasets: locations.


The elements that are located nearby might affect or resemble each other. PySAL helps in analyzing such spatial links when working with geospatial data.


PySAL could help with:


  • Spatial Autocorrelation Analysis

  • Cluster Detection

  • Hot Spots and Cold Spots Detection

  • Geographic Patterns Analysis

  • Spatial Dependency Analysis

  • Local Moran’s I

  • Reproducible Spatial Analysis Workflow Creation

  • Integrating spatial analysis and statistics with GeoPandas and visualization libraries


Preparing Spatial Data with GeoPandas


A common PySAL workflow begins with a spatial dataset stored as a Shapefile, GeoPackage, GeoJSON, or another supported geospatial format.

For example, suppose you have a polygon dataset containing population values for administrative areas.

import geopandas as gpd

gdf = gpd.read_file("population_areas.shp")

print(gdf.head())

It is important to verify that the geometry and attribute data are valid before performing spatial analysis.

You should check:

print(gdf.crs)
print(gdf.geometry.is_valid)
print(gdf["population"].describe())

If necessary, repair invalid geometries and handle missing attribute values before calculating spatial statistics.


Creating a Spatial Weights Matrix


Spatial statistics require a way to define which geographic features are considered neighbors. PySAL uses spatial weights to represent these relationships.

For polygon datasets, two common approaches are:


Queen Contiguity


Queen contiguity considers polygons neighbors when they share either an edge or a vertex.

from libpysal.weights import Queen

w = Queen.from_dataframe(gdf)

print(w.n)

This approach is commonly used for administrative boundaries because even polygons touching only at a corner can be considered neighbors.


Rook Contiguity


Rook contiguity considers polygons neighbors when they share an edge.

from libpysal.weights import Rook

w = Rook.from_dataframe(gdf)

The appropriate weights model depends on the geographic phenomenon and the topology of the dataset.


Calculating Global Moran's I


Once a spatial weights matrix has been created, you can calculate Global Moran's I using esda.

from esda.moran import Moran

y = gdf["population"].values

mi = Moran(y, w)

print("Moran's I:", mi.I)
print("Expected I:", mi.EI)
print("p-value:", mi.p_sim)

The statistic itself should be interpreted together with its significance assessment.

A small p-value can provide evidence against the null hypothesis of spatial randomness, but statistical significance does not automatically explain why the spatial pattern exists.


Detecting Local Spatial Clusters


Global Moran's I identifies an overall spatial pattern. To locate individual clusters, you can use Local Moran's I.

PySAL's esda package provides the Moran_Local statistic.

from esda.moran import Moran_Local

local_moran = Moran_Local(y, w)

gdf["local_I"] = local_moran.Is
gdf["p_value"] = local_moran.p_sim

The local statistic is calculated for each geographic feature and can then be used to classify spatial relationships.


Visualizing Spatial Hot Spots


After calculating local statistics, you can visualize the resulting clusters using GeoPandas.

gdf.plot(
    column="cluster",
    categorical=True,
    legend=True,
    figsize=(10, 8)
)

A cluster map can make it easier to identify geographic concentrations that may not be obvious from a conventional thematic map.


For example, a population-density analysis might reveal a concentration of high-density areas around an urban core. A wildfire analysis could identify statistically significant concentrations of high incident counts, while an environmental dataset could reveal clusters of elevated pollution measurements.


Hot Spot Analysis vs. Spatial Cluster Analysis


Although the terms are sometimes used interchangeably, hot spot analysis and spatial cluster analysis can represent different statistical approaches.

Hot spot analysis generally focuses on identifying statistically significant concentrations of high or low values.

Spatial cluster analysis is broader and can include several methods for identifying spatially related observations.

For example:

Analysis

Purpose

Global Moran's I

Measures overall spatial autocorrelation

Local Moran's I

Identifies local spatial associations

HH cluster

Identifies high values near high values

LL cluster

Identifies low values near low values

HL outlier

Identifies high values near low values

LH outlier

Identifies low values near high values

Selecting a method should depend on the structure of the data, spatial relationships, and research question.


Important Considerations When Using PySAL


Cluster detection involves much more than applying statistical methods. There are a number of methodological factors that could affect the outcome.


Use Appropriate Spatial Weights


The weights matrix describes how the neighborhood is defined. The results using the queen, rook, distance-based, or k-nearest-neighbor weights matrix could differ.


Distance-based or k-nearest-neighbor weights matrices could be more appropriate for point data than polygon contiguity.


Test for Statistical Significance


While there could be clusters on the map, they may not necessarily be statistically significant. Local statistics would need to be checked for significance.


Multiple Testing Issue


The local test is an application of statistical tests for each observation geographically. This could lead to type I errors in certain cases. Depending on the particular analysis being done, multiple comparisons adjustments or some inferential technique would be useful.


Coordinate Reference System Check


Distance analysis based on distances in linear measurements requires an appropriate projected coordinate reference system.


For example, the use of longitude and latitude geographic coordinates to measure the distance may yield incorrect distances if the method used does not consider geographic coordinates.


Examine Data Quality


Problems like missing data, invalid geometric shapes, outliers, irregular boundaries, and wrong attribute data may influence the spatial statistics.


It is thus important to take data preparation as an integral process of the analysis.


PySAL in Remote Sensing and GIS


PySAL has the ability to provide a variety of GIS, remote sensing, and spatial science applications.


Some applications may include:


  • Urban development studies

  • Analysis of population distribution

  • Crime pattern analysis

  • Disease surveillance

  • Studies on agricultural production

  • Environmental monitoring

  • Wildfire incident analysis

  • Real estate analysis

  • Transportation planning

  • Land cover and use studies

  • Infrastructure assessment


In remote sensing applications, raster-based products such as NDVI, NDBI, NDWI, and land cover classification may be summarized in spatial units to study spatial patterns. This will enable researchers to study whether there are certain environmental or urban factors that are geographically clustered.


PySAL and the Geospatial Python Ecosystem


PySAL is frequently paired with other geospatial Python packages.


A standard process would include:


  • GeoPandas for vector data

  • Shapely for geometry

  • Rasterio for raster data

  • NumPy for numerical computing

  • pandas for tabular data

  • PySAL for spatial analysis

  • Matplotlib for visualization


The use of these packages together creates a reproducible process from data handling to analysis and visualization.


Hot spot identification and spatial clustering are integral aspects of geographic pattern analysis. PySAL is a robust Python toolkit designed to conduct spatial statistical analysis, which includes Global Moran's I and Local Moran's I.


Generally, the process entails geospatial data preparation, creation of the right spatial weight matrix, measurement of spatial autocorrelation globally, computation of local statistics, significance testing, and visualization of clusters formed.


Proper application of the tool can enable GIS experts, remote sensing specialists, and spatial data scientists to go beyond mapping and conduct quantitative analysis of geographic patterns that emerge from such analyses.


To learn more about PySAL and its geospatial capabilities, click here.


For more information or any questions regarding the LizardTech suite of products, please don't hesitate to contact us at:



USA (HQ): (720) 702–4849


(A GeoWGS84 Corp Company)



Comments


bottom of page