The weights Spatial weights for PySAL
New in version 1.0.
Spatial weights
| Parameters: | neighbors : dictionary
weights = None : dictionary
id_order = None : list
|
|---|
Examples
>>> from pysal import W
>>> neighbors={0: [3, 1], 1: [0, 4, 2], 2: [1, 5], 3: [0, 6, 4], 4: [1, 3, 7, 5], 5: [2, 4, 8], 6: [3, 7], 7: [4, 6, 8], 8: [5, 7]}
>>> weights={0: [1, 1], 1: [1, 1, 1], 2: [1, 1], 3: [1, 1, 1], 4: [1, 1, 1, 1], 5: [1, 1, 1], 6: [1, 1], 7: [1, 1, 1], 8: [1, 1]}
>>> w=W(neighbors,weights)
>>> w.pct_nonzero
0.29629629629629628
Read from external gal file
>>> import pysal
>>> w=pysal.open("../examples/stl.gal").read()
>>> w.n
78
>>> w.pct_nonzero
0.065417488494411577
Set weights implicitly
>>> neighbors={0: [3, 1], 1: [0, 4, 2], 2: [1, 5], 3: [0, 6, 4], 4: [1, 3, 7, 5], 5: [2, 4, 8], 6: [3, 7], 7: [4, 6, 8], 8: [5, 7]}
>>> w=W(neighbors)
>>> w.pct_nonzero
0.29629629629629628
>>> w=lat2W(100,100)
>>> w.trcW2
39600.0
>>> w.trcWtW
39600.0
>>> w.transform='r'
>>> w.trcW2
2530.7222222222586
>>> w.trcWtW
2533.6666666666774
Cardinality Histogram
>>> w=pysal.rook_from_shapefile("../examples/sacramentot2.shp")
>>> w.histogram
[(1, 1), (2, 6), (3, 33), (4, 106), (5, 114), (6, 70), (7, 35), (8, 17), (9, 9), (10, 4), (11, 4), (12, 3), (13, 0), (14, 1)]
Attributes
Methods
| asymmetry | |
| full | |
| get_transform | |
| higher_order | |
| order | |
| set_transform | |
| shimbel |
Checks for w_{i,j} == w_{j,i} forall i,j
| Returns: | asymmetries : list
|
|---|
Examples
>>> from pysal import lat2W
>>> w=lat2W(3,3)
>>> w.asymmetry()
[]
>>> w.transform='r'
>>> w.asymmetry()
(array([1, 3, 0, 2, 4, 1, 5, 0, 4, 6, 1, 3, 5, 7, 2, 4, 8, 3, 7, 4, 6, 8, 5,
7]), array([0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8,
8]))
>>> neighbors={0:[1,2,3], 1:[1,2,3], 2:[0,1], 3:[0,1]}
>>> weights={0:[1,1,1], 1:[1,1,1], 2:[1,1], 3:[1,1]}
>>> w=W(neighbors,weights)
>>> w.asymmetry()
(array([1, 0]), array([0, 1]))

Generate a full numpy array
| Returns: | implicit : tuple
|
|---|
See also
Examples
>>> from pysal import W
>>> neighbors={'first':['second'],'second':['first','third'],'third':['second']}
>>> weights={'first':[1],'second':[1,1],'third':[1]}
>>> w=W(neighbors,weights)
>>> wf,ids=w.full()
>>> wf
array([[ 0., 1., 0.],
[ 1., 0., 1.],
[ 0., 1., 0.]])
>>> ids
['first', 'second', 'third']
Getter for transform property
| Returns: | transformation : string (or none) |
|---|
Examples
>>> from pysal import lat2W
>>> w=lat2W()
>>> w.weights[0]
[1.0, 1.0]
>>> w.transform
'O'
>>> w.transform='r'
>>> w.weights[0]
[0.5, 0.5]
>>> w.transform='b'
>>> w.weights[0]
[1.0, 1.0]
>>>
Contiguity weights object of order k
| Parameters: | k : int
|
|---|---|
| Returns: | implicit : W
|
See also
Notes
Implements the algorithm in Anselin and Smirnov (1996) [R6]
References
| [R6] | (1, 2) Anselin, L. and O. Smirnov (1996) “Efficient algorithms for constructing proper higher order spatial lag operators. Journal of Regional Science, 36, 67-89. |
Examples
>>> from pysal import lat2W
>>> w5=lat2W()
>>> w5_shimbel=w5.shimbel()
>>> w5_shimbel[0][24]
8
>>> w5_shimbel[0][0:4]
[-1, 1, 2, 3]
>>> w5_8th_order=w5.higher_order(8)
>>> w5_8th_order.neighbors[0]
[24]
>>> from pysal import rook_from_shapefile as rfs
>>> w=rfs('../examples/10740.shp')
>>> w2=w.higher_order(2)
>>> w[1]
{0: 1.0, 2: 1.0, 83: 1.0, 4: 1.0}
>>> w2[1]
{3: 1.0, 5: 1.0, 6: 1.0, 10: 1.0, 82: 1.0, 85: 1.0, 91: 1.0, 92: 1.0, 101: 1.0}
>>> w[147]
{144: 1.0, 146: 1.0, 164: 1.0, 165: 1.0, 150: 1.0}
>>> w[85]
{0: 1.0, 101: 1.0, 83: 1.0, 84: 1.0, 90: 1.0, 91: 1.0, 93: 1.0}
>>>
returns True if user has set id_order, False if not.
Examples
>>> from pysal import lat2W
>>> w=lat2W()
>>> w.id_order_set
True
Given the current id_order, neighbor_offsets[id] is the offsets of the id’s neighrbors in id_order
Examples
>>> from pysal import W
>>> neighbors={'c': ['b'], 'b': ['c', 'a'], 'a': ['b']}
>>> weights ={'c': [1.0], 'b': [1.0, 1.0], 'a': [1.0]}
>>> w=W(neighbors,weights)
>>> w.id_order = ['a','b','c']
>>> w.neighbor_offsets['b']
[2, 0]
>>> w.id_order = ['b','a','c']
>>> w.neighbor_offsets['b']
[2, 1]
Determine the non-redundant order of contiguity up to a specific order.
| Parameters: | kmax : int
|
|---|---|
| Returns: | implicit : dict
|
See also
Notes
Implements the algorithm in Anselin and Smirnov (1996) [R7]
References
| [R7] | (1, 2) Anselin, L. and O. Smirnov (1996) “Efficient algorithms for constructing proper higher order spatial lag operators. Journal of Regional Science, 36, 67-89. |
Examples
>>> from pysal import rook_from_shapefile as rfs
>>> w=rfs('../examples/10740.shp')
>>> w3=w.order()
>>> w3[1][0:5]
[1, -1, 1, 2, 1]
float

float

float

Transformations of weights.
| Parameters: | transform : string (not case sensitive)
|
|---|
Examples
>>> from pysal import lat2W
>>> w=lat2W()
>>> w.weights[0]
[1.0, 1.0]
>>> w.transform
'O'
>>> w.transform='r'
>>> w.weights[0]
[0.5, 0.5]
>>> w.transform='b'
>>> w.weights[0]
[1.0, 1.0]
>>>
Find the Shmibel matrix for the first order contiguity matrix.
| Returns: | implicit : list of lists
|
|---|
See also
Examples
>>> from pysal import lat2W
>>> w5=lat2W()
>>> w5_shimbel=w5.shimbel()
>>> w5_shimbel[0][24]
8
>>> w5_shimbel[0][0:4]
[-1, 1, 2, 3]
>>>
Sparse matrix object
For any matrix manipulations required for w, w.sparse should be used. This is based on scipy.sparse.
Getter for transform property
| Returns: | transformation : string (or none) |
|---|
Examples
>>> from pysal import lat2W
>>> w=lat2W()
>>> w.weights[0]
[1.0, 1.0]
>>> w.transform
'O'
>>> w.transform='r'
>>> w.weights[0]
[0.5, 0.5]
>>> w.transform='b'
>>> w.weights[0]
[1.0, 1.0]
>>>

Create a W object for a regular lattice.
| Parameters: | nrows : int
ncols : int
rook : boolean
id_type : string
|
|---|---|
| Returns: | w : W
|
Notes
Observations are row ordered: first k observations are in row 0, next k in row 1, and so on.
Examples
>>> from pysal import lat2W
>>> w9=lat2W(3,3)
>>> w9.pct_nonzero
0.29629629629629628
>>> w9[0]
{1: 1.0, 3: 1.0}
>>> w9[3]
{0: 1.0, 4: 1.0, 6: 1.0}
>>>
Construct spatial weights for regime neighbors.
Block contiguity structures are relevant when defining neighbor relations based on membership in a regime. For example, all counties belonging to the same state could be defined as neighbors, in an analysis of all counties in the US.
| Parameters: | regimes : list or array
|
|---|---|
| Returns: | W : spatial weights instance |
Examples
>>> from pysal import regime_weights
>>> import numpy as np
>>> regimes=np.ones(25)
>>> regimes[range(10,20)]=2
>>> regimes[range(21,25)]=3
>>> regimes
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2.,
2., 2., 2., 2., 2., 2., 2., 1., 3., 3., 3., 3.])
>>> w=regime_weights(regimes)
>>> w.weights[0]
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
>>> w.neighbors[0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
>>> regimes=['n','n','s','s','e','e','w','w','e']
>>> n=len(regimes)
>>> w=regime_weights(regimes)
>>> w.neighbors
{0: [1], 1: [0], 2: [3], 3: [2], 4: [5, 8], 5: [4, 8], 6: [7], 7: [6], 8: [4, 5]}
Combinations of size n taken from items
| Parameters: | items : sequence n : integer
|
|---|---|
| Returns: | implicit : generator
|
Examples
>>> x=range(4)
>>> for c in comb(x,2):
... print c
...
[0, 1]
[0, 2]
[0, 3]
[1, 2]
[1, 3]
[2, 3]
Determine the non-redundant order of contiguity up to a specific order.
| Parameters: | w : W
kmax : int
|
|---|---|
| Returns: | info : dictionary
|
Notes
Implements the algorithm in Anselin and Smirnov (1996) [R8]
References
| [R8] | (1, 2) Anselin, L. and O. Smirnov (1996) “Efficient algorithms for constructing proper higher order spatial lag operators. Journal of Regional Science, 36, 67-89. |
Examples
>>> from pysal import rook_from_shapefile as rfs
>>> w=rfs('../examples/10740.shp')
>>> w3=order(w,kmax=3)
>>> w3[1][0:5]
[1, -1, 1, 2, 1]
Contiguity weights object of order k
| Parameters: | w : W
order : int
|
|---|---|
| Returns: | implicit : W
|
Notes
Implements the algorithm in Anselin and Smirnov (1996) [R9]
References
| [R9] | (1, 2) Anselin, L. and O. Smirnov (1996) “Efficient algorithms for constructing proper higher order spatial lag operators. Journal of Regional Science, 36, 67-89. |
Examples
>>> from pysal import lat2W, higher_order
>>> w10=lat2W(10,10)
>>> w10_2=higher_order(w10,2)
>>> w10_2[0]
{2: 1.0, 11: 1.0, 20: 1.0}
>>> w5=lat2W()
>>> w5[0]
{1: 1.0, 5: 1.0}
>>> w5[1]
{0: 1.0, 2: 1.0, 6: 1.0}
>>> w5_2=higher_order(w5,2)
>>> w5_2[0]
{2: 1.0, 10: 1.0, 6: 1.0}
Find the Shmibel matrix for first order contiguity matrix.
| Parameters: | w : W
Returns : ——- : info : list of lists
|
|---|
Examples
>>> from pysal import lat2W, shimbel
>>> w5=lat2W()
>>> w5_shimbel=shimbel(w5)
>>> w5_shimbel[0][24]
8
>>> w5_shimbel[0][0:4]
[-1, 1, 2, 3]
>>>
Remaps the IDs in a spatial weights object
| Parameters: | w : W
old2new : dictionary
id_order : list
|
|---|---|
| Returns: | implicit : W
|
Examples
>>> from pysal import lat2W, remap_ids
>>> w = lat2W(3,2)
>>> w.id_order
[0, 1, 2, 3, 4, 5]
>>> w.neighbors[0]
[2, 1]
>>> old_to_new = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f'}
>>> w_new = remap_ids(w, old_to_new)
>>> w_new.id_order
['a', 'b', 'c', 'd', 'e', 'f']
>>> w_new.neighbors['a']
['c', 'b']
Generate a full numpy array
| Parameters: | w : W
|
|---|---|
| Returns: | implicit : tuple
|
Examples
>>> from pysal import W, full
>>> neighbors={'first':['second'],'second':['first','third'],'third':['second']}
>>> weights={'first':[1],'second':[1,1],'third':[1]}
>>> w=W(neighbors,weights)
>>> wf,ids=full(w)
>>> wf
array([[ 0., 1., 0.],
[ 1., 0., 1.],
[ 0., 1., 0.]])
>>> ids
['first', 'second', 'third']