pysal.lib.weights.w_difference(w1, w2, w_shape='w1', constrained=True, silence_warnings=False)[source]

Returns a binary weights object, w, that includes only neighbor pairs in w1 that are not in w2. The w_shape and constrained parameters determine which pairs in w1 that are not in w2 are returned.

w1 : W


w2 : W


w_shape : string

Defines the shape of the returned weights matrix. ‘w1’ returns a matrix with the same IDs as w1; ‘all’ returns a matrix with all the unique IDs from w1 and w2; and ‘min’ returns a matrix with the IDs occurring in w1 and not in w2.

constrained : boolean

If False then the full set of neighbor pairs in w1 that are not in w2 are returned. If True then those pairs that would not be possible if w_shape=’min’ are dropped. Ignored if w_shape is set to ‘min’.

silence_warnings : boolean

Switch to turn off (default on) print statements for every observation with islands

w : W



ID comparisons are performed using ==, therefore the integer ID 2 is equivalent to the float ID 2.0.


Construct rook (w2) and queen (w1) weights matrices for two 4x4 regions (16 areas). A queen matrix has all the joins a rook matrix does plus joins between areas that share a corner. The new matrix formed by the difference of rook from queen contains only join at corners (typically called a bishop matrix). Note that the difference of queen from rook would result in a weights matrix with no joins.

>>> from pysal.lib.weights import lat2W
>>> w1 = lat2W(4,4,rook=False)
>>> w2 = lat2W(4,4,rook=True)
>>> import pysal.lib
>>> w = pysal.lib.weights.set_operations.w_difference(w1, w2, constrained=False)
>>> w1[0] == w[0]
>>> w1.neighbors[15]
[10, 11, 14]
>>> w2.neighbors[15]
[11, 14]
>>> w.neighbors[15]