libpysal.weights.W(neighbors, weights=None, id_order=None, silence_warnings=False, ids=None)[source]¶Spatial weights class.
Key is region ID, value is a list of neighbor IDS. Example: {‘a’:[‘b’],’b’:[‘a’,’c’],’c’:[‘b’]}
Key is region ID, value is a list of edge weights. If not supplied all edge weights are assumed to have a weight of 1. Example: {‘a’:[0.5],’b’:[0.5,1.5],’c’:[1.5]}
An ordered list of ids, defines the order of observations when iterating over W if not set, lexicographical ordering is used to iterate and the id_order_set property will return False. This can be set after creation by setting the ‘id_order’ property.
By default libpysal will print a warning if the dataset contains any disconnected components or islands. To silence this warning set this parameter to True.
Values to use for keys of the neighbors and weights dicts.
Examples
>>> from libpysal.weights 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)
>>> "%.3f"%w.pct_nonzero
'29.630'
Read from external gal file
>>> import libpysal
>>> w = libpysal.io.open(libpysal.examples.get_path("stl.gal")).read()
>>> w.n
78
>>> "%.3f"%w.pct_nonzero
'6.542'
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)
>>> round(w.pct_nonzero,3)
29.63
>>> from libpysal.weights import lat2W
>>> w = lat2W(100, 100)
>>> w.trcW2
39600.0
>>> w.trcWtW
39600.0
>>> w.transform='r'
>>> round(w.trcW2, 3)
2530.722
>>> round(w.trcWtW, 3)
2533.667
Cardinality Histogram >>> w.histogram [(2, 4), (3, 392), (4, 9604)]
Disconnected observations (islands)
>>> from libpysal.weights import W
>>> w = W({1:[0],0:[1],2:[], 3:[]})
UserWarning: The weights matrix is not fully connected: There are 3 disconnected components. There are 2 islands with ids: 2, 3.
asymmetriesList of id pairs with asymmetric weights.
cardinalitiesNumber of neighbors for each observation.
component_labelsStore the graph component in which each observation falls.
diagW2Diagonal of \(WW\).
diagWtWDiagonal of \(W^{'}W\).
diagWtW_WWDiagonal of \(W^{'}W + WW\).
histogramCardinality histogram as a dictionary where key is the id and value is the number of neighbors for that unit.
id2iDictionary where the key is an ID and the value is that ID’s index in W.id_order.
id_orderReturns the ids for the observations in the order in which they would be encountered if iterating over the weights.
id_order_setReturns True if user has set id_order, False if not.
islandsList of ids without any neighbors.
max_neighborsLargest number of neighbors.
mean_neighborsAverage number of neighbors.
min_neighborsMinimum number of neighbors.
nNumber of units.
n_componentsStore whether the adjacency matrix is fully connected.
neighbor_offsetsGiven the current id_order, neighbor_offsets[id] is the offsets of the id’s neighbors in id_order.
nonzeroNumber of nonzero weights.
pct_nonzeroPercentage of nonzero weights.
s0s0 is defined as
s1s1 is defined as
s2s2 is defined as
s2arrayIndividual elements comprising s2.
sdStandard deviation of number of neighbors.
sparseSparse matrix object.
transformGetter for transform property.
trcW2Trace of \(WW\).
trcWtWTrace of \(W^{'}W\).
trcWtW_WWTrace of \(W^{'}W + WW\).
Methods
|
Asymmetry check. |
|
Return an adjacency list representation of a weights object. |
|
Convert a networkx graph to a PySAL W object. |
|
Generate a full numpy array. |
|
Getter for transform property. |
|
Plot spatial weights objects. |
|
In place modification throughout W of id values from w.id_order to new_ids in all |
|
Adding meta data for writing headers of gal and gwt files. |
|
Transformations of weights. |
|
Construct a symmetric KNN weight. |
|
Generate a WSP object. |
|
Compute an adjacency list representation of a weights object. |
|
Convert a weights object to a networkx graph |
from_WSP |
|
from_file |
|
from_shapefile |
__init__(self, neighbors, weights=None, id_order=None, silence_warnings=False, ids=None)[source]¶Initialize self. See help(type(self)) for accurate signature.
Methods
|
Initialize self. |
|
Asymmetry check. |
|
|
|
Return an adjacency list representation of a weights object. |
|
|
|
Convert a networkx graph to a PySAL W object. |
|
|
|
Generate a full numpy array. |
|
Getter for transform property. |
|
Plot spatial weights objects. |
|
In place modification throughout W of id values from w.id_order to new_ids in all |
|
Adding meta data for writing headers of gal and gwt files. |
|
Transformations of weights. |
|
Construct a symmetric KNN weight. |
|
Generate a WSP object. |
|
Compute an adjacency list representation of a weights object. |
|
Convert a weights object to a networkx graph |
Attributes
|
List of id pairs with asymmetric weights. |
|
Number of neighbors for each observation. |
|
Store the graph component in which each observation falls. |
|
Diagonal of \(WW\). |
|
Diagonal of \(W^{'}W\). |
|
Diagonal of \(W^{'}W + WW\). |
|
Cardinality histogram as a dictionary where key is the id and value is the number of neighbors for that unit. |
|
Dictionary where the key is an ID and the value is that ID’s index in W.id_order. |
|
Returns the ids for the observations in the order in which they would be encountered if iterating over the weights. |
|
Returns True if user has set id_order, False if not. |
|
List of ids without any neighbors. |
|
Largest number of neighbors. |
|
Average number of neighbors. |
|
Minimum number of neighbors. |
|
Number of units. |
|
Store whether the adjacency matrix is fully connected. |
|
Given the current id_order, neighbor_offsets[id] is the offsets of the id’s neighbors in id_order. |
|
Number of nonzero weights. |
|
Percentage of nonzero weights. |
|
s0 is defined as |
|
s1 is defined as |
|
s2 is defined as |
|
Individual elements comprising s2. |
|
Standard deviation of number of neighbors. |
|
Sparse matrix object. |
|
Getter for transform property. |
|
Trace of \(WW\). |
|
Trace of \(W^{'}W\). |
|
Trace of \(W^{'}W + WW\). |