Spatial Weights

Spatial weights are used to quantify spatial relationships for analysis and modeling in Spatial AI.

The spatial weights are represented as a weighted graph, where each observation in a dataset represents a node. If two nodes are neighbors in a geographic context, then there is an edge between them with a weight associated with it. In some cases, the weight is binary, indicating that both nodes are spatially connected; in other cases, the weight is based on the distance between the nodes. As it is a common practice to work with sparse graphs, the spatial weights are usually implemented with an adjacency list.

Many machine learning algorithms and spatial analytics, such as spatial autocorrelation statistics and regionalization algorithms, rely on spatial weights in Spatial AI. The following table describes the spatial weights supported in Spatial AI.

Spatial Weights Description
KNNWeightsDefinition It is based on proximity and defines a spatial relationship of K-nearest neighbors.
KernelBasedWeightsDefinition It uses a kernel function to define spatial relationships, and it is a decay function, which means that closer neighbors have larger values while further neighbors have smaller ones.
  • The bandwidth parameter is the distance used for the kernel function.
  • The fixed parameter indicates if the bandwidth is constant for all the observations.
  • The function parameter is the kernel that will be used; this can be one of the following values: {'triangular', 'uniform', 'quadratic', 'gaussian'}.
  • The parameter k is required to estimate the bandwidth, representing the number of neighbors for each observation.
DistanceBandWeightsDefinition Uses the distance between two nodes as the weight for the edge connecting them. If the binary parameter is True, then the weight is 1 for all the neighbors at a distance less than the threshold value. Otherwise, it uses the decay parameter alpha to estimate the weight. By default, it uses the Euclidean distance metric.
RookWeightsDefinition The relationship is based on contiguity, where two geometries are neighbors if they share at least one edge.
QueenWeightsDefinition This definition is based on contiguity. Two geometries with a common vertex are neighbors with a weight of 1.

The type of spatial weights depends on the problem scenario to be resolved. In the case of spatial weights not based on contiguity, the data must be in a projected referenced system. See the oraclesai.weights module in Python API Reference for Oracle Spatial AI for more information.

The following example shows how to create an instance of the SpatialWeights class from the block_groups SpatialDataFrame using the KNNWeightsDefinition and SpatialWeights.create functions.

from oraclesai.weights import SpatialWeights, KNNWeightsDefinition

spatial_weights = SpatialWeights.create(block_groups, KNNWeightsDefinition(k=5))

print(f"neighbors' indexes: {spatial_weights.neighbors[0]}")
print(f"neighbors' weights: {spatial_weights.weights[0]}")

The preceding code prints the neighbors’ indexes of the first observation and their weights. In this case, all the weights are binary. The neighbors and their weights can be referenced using the neighbors and weights properties respectively.

neighbors' indexes: [2806, 81, 1717, 80, 1916]
neighbors' weights: [1.0, 1.0, 1.0, 1.0, 1.0]