libpysal.weights.lat2SW

libpysal.weights.lat2SW(nrows=3, ncols=5, criterion='rook', row_st=False)[source]

Create a sparse W matrix for a regular lattice.

Parameters
nrowsint

number of rows

ncolsint

number of columns

rook{“rook”, “queen”, “bishop”}

type of contiguity. Default is rook.

row_stbool

If True, the created sparse W object is row-standardized so every row sums up to one. Defaults to False.

Returns
wscipy.sparse.dia_matrix

instance of a scipy sparse matrix

Notes

Observations are row ordered: first k observations are in row 0, next k in row 1, and so on. This method directly creates the W matrix using the strucuture of the contiguity type.

Examples

>>> from libpysal.weights import lat2SW
>>> w9 = lat2SW(3,3)
>>> w9[0,1] == 1
True
>>> w9[3,6] == 1
True
>>> w9r = lat2SW(3,3, row_st=True)
>>> w9r[3,6] == 1./3
True