# How Generate Spatial Weights Matrix works

Spatial statistics does not mean applying traditional (nonspatial) statistical methods to data that just happens to be spatial (having x- and y-coordinates). Spatial statistics integrate space and spatial relationships directly into their mathematics (area, distance, length, and so on). For many spatial statistics, these spatial relationships are specified formally through a spatial weights matrix file or table.

A spatial weights matrix is a representation of the spatial structure of your data. It is a quantification of the spatial relationships that exist among the features in your dataset (or, at least, a quantification of the way you conceptualize those relationships). Because the spatial weights matrix imposes a structure on your data, you should select a conceptualization that best reflects how features actually interact with each other (giving thought, of course, to what it is you are trying to measure). If you are measuring clustering of a particular species of seed-propagating tree in a forest, for example, some form of inverse distance is probably most appropriate. However, if you are assessing the geographic distribution of a region's commuters, travel time or travel cost might be a better choice.

While physically implemented in a variety of ways, conceptually, the spatial weights matrix is an NxN table (N is the number of features in the dataset). There is one row for every feature and one column for every feature. The cell value for any given row/column combination is the weight that quantifies the spatial relationship between those row and column features.

At the most basic level, there are two strategies for creating weights to quantify the relationships among data features: binary or variable weighting. For binary strategies (fixed distance, K nearest neighbors, Delaunay Triangulation, contiguity, or space-time window) a feature is either a neighbor (1) or it is not (0). For weighted strategies (inverse distance or zone of indifference), neighboring features have a varying amount of impact (or influence), and weights are computed to reflect that variation.

Based on your parameter specifications, the Generate Spatial Weights Matrix tool creates a spatial weights matrix (SWM) file. The spatial relationship values in that file are stored using sparse matrix techniques to minimize disk space, computer memory, and the number of required calculations. These relationship values are utilized in the mathematics of several spatial statistics tools including Spatial Autocorrelation (Global Moran's I), Hot Spot Analysis (Getis-Ord Gi*), and Cluster and Outlier Analysis (Anselin Local Moran's I). While the spatial weights matrix file can conceivably store NxN spatial relationships, in most cases, each feature should only be related to a handful of others. The sparse methodology takes advantage of this by only storing nonzero relationships.

##### Note:

It is possible to run out of memory when you are using an SWM file. This generally occurs when you select Conceptualization of Spatial Relationships or Distance Band or Threshold Distance, resulting in features having many, many neighbors, negating the sparse nature of the SWM file. You generally do not want to create a spatial weights matrix where every feature has thousands of neighbors. You want all features to have at least one neighbor and almost all features to have at least eight neighbors. You can ensure that each feature has a specified minimum number of neighbors by entering that minimum value for the Number of Neighbors parameter.

##### Dive-in:

The spatial weights matrix (SWM) file is written using a little endian binary file format. For more information about how the SWM file is read and written to disk, right-click the Generate Spatial Weights Matrix tool and choose Edit. This will display the Python source code for this tool. The code for reading an SWM file is in the WeightsUtils.py file, which is installed in your <ArcGIS Pro>/Resources/ArcToolbox/Scripts folder.