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

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

w1 : W


w2 : W


w_shape : string

Defines the shape of the returned weights matrix. ‘all’ returns a matrix with all the unique IDs from w1 and w2; and ‘min’ returns a matrix with the IDs not shared by w1 and w2.

constrained : boolean

If False then the full set of neighbor pairs that are not shared by w1 and 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 queen weights matrix for a 4x4 (16 areas) region (w1) and a rook matrix for a 6x4 (24 areas) region (w2). The symmetric difference of these two matrices (with w_shape set to ‘all’ and constrained set to False) contains the corner joins in the overlap area, all the joins in the non-overlap area.

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