libpysal.weights.higher_order_sp

libpysal.weights.higher_order_sp(w, k=2, shortest_path=True, diagonal=False, **kwargs)[source]

Contiguity weights for either a sparse W or W for order k.

Parameters
wW

sparse_matrix, spatial weights object or scipy.sparse.csr.csr_instance

kint

Order of contiguity

shortest_pathbool

True: i,j and k-order neighbors if the shortest path for i,j is k. False: i,j are k-order neighbors if there is a path from i,j of length k.

diagonalbool

True: keep k-order (i,j) joins when i==j False: remove k-order (i,j) joins when i==j

**kwargskeyword arguments

optional arguments for pysal.weights.W

Returns
wkW

WSP, type matches type of w argument

Notes

Lower order contiguities are removed.

Examples

>>> from libpysal.weights import lat2W, higher_order_sp
>>> w25 = lat2W(5,5)
>>> w25.n
25
>>> w25[0] == {1: 1.0, 5: 1.0}
True
>>> w25_2 = higher_order_sp(w25, 2)
>>> w25_2[0] == {10: 1.0, 2: 1.0, 6: 1.0}
True
>>> w25_2 = higher_order_sp(w25, 2, diagonal=True)
>>> w25_2[0] ==  {0: 1.0, 10: 1.0, 2: 1.0, 6: 1.0}
True
>>> w25_3 = higher_order_sp(w25, 3)
>>> w25_3[0] == {15: 1.0, 3: 1.0, 11: 1.0, 7: 1.0}
True
>>> w25_3 = higher_order_sp(w25, 3, shortest_path=False)
>>> w25_3[0] == {1: 1.0, 3: 1.0, 5: 1.0, 7: 1.0, 11: 1.0, 15: 1.0}
True