libpysal.weights.W

class libpysal.weights.W(neighbors, weights=None, id_order=None, silence_warnings=False, ids=None)[source]

Spatial weights class.

Parameters:

neighbors : dictionary

Key is region ID, value is a list of neighbor IDS. Example: {‘a’:[‘b’],’b’:[‘a’,’c’],’c’:[‘b’]}

weights : dictionary

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]}

id_order : list

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.

silent_island_warning: boolean

By default libpysal will print a warning if the dataset contains any disconnected observations or islands. To silence this warning set this parameter to True.

silent_connected_components : boolean

By default PySAL will print a warning if the dataset contains any disconnected components in the adjacency matrix. These are disconnected groups of islands. To silence this warning set this parameter to True.

ids : list

Values to use for keys of the neighbors and weights dicts.

Attributes (NOTE: these are described by their docstrings. to view, use the `help` function)

———-

asymmetries

cardinalities

component_labels

diagW2

diagWtW

diagWtW_WW

histogram

id2i

id_order

id_order_set

islands

max_neighbors

mean_neighbors

min_neighbors

n

n_components

neighbor_offsets

nonzero

pct_nonzero

s0

s1

s2

s2array

sd

sparse

trcW2

trcWtW

trcWtW_WW

transform

Examples

>>> from libpysal.weights.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:[]})

WARNING: there are 2 disconnected observations Island ids: [2, 3]

Attributes

asymmetries List of id pairs with asymmetric weights.
cardinalities Number of neighbors for each observation.
component_labels Store the graph component in which each observation falls.
diagW2 Diagonal of \(WW\).
diagWtW Diagonal of \(W^{'}W\).
diagWtW_WW Diagonal of \(W^{'}W + WW\).
histogram Cardinality histogram as a dictionary where key is the id and value is the number of neighbors for that unit.
id2i Dictionary where the key is an ID and the value is that ID’s index in W.id_order.
id_order Returns the ids for the observations in the order in which they would be encountered if iterating over the weights.
id_order_set Returns True if user has set id_order, False if not.
islands List of ids without any neighbors.
max_neighbors Largest number of neighbors.
mean_neighbors Average number of neighbors.
min_neighbors Minimum number of neighbors.
n Number of units.
n_components Store whether the adjacency matrix is fully connected.
neighbor_offsets Given the current id_order, neighbor_offsets[id] is the offsets of the id’s neighbors in id_order.
nonzero Number of nonzero weights.
pct_nonzero Percentage of nonzero weights.
s0 s0 is defined as
s1 s1 is defined as
s2 s2 is defined as
s2array Individual elements comprising s2.
sd Standard deviation of number of neighbors.
sparse Sparse matrix object.
transform Getter for transform property.
trcW2 Trace of \(WW\).
trcWtW Trace of \(W^{'}W\).
trcWtW_WW Trace of \(W^{'}W + WW\).

Methods

asymmetry([intrinsic]) Asymmetry check.
from_WSP(WSP[, silence_warnings])
from_adjlist(adjlist[, focal_col, …]) Return an adjacency list representation of a weights object.
from_file([path, format])
from_networkx(graph[, weight_col]) Convert a networkx graph to a PySAL W object.
from_shapefile(*args, **kwargs)
full() Generate a full numpy array.
get_transform() Getter for transform property.
plot(gdf[, indexed_on, ax, color, node_kws, …]) Plot spatial weights objects.
remap_ids(new_ids) In place modification throughout W of id values from w.id_order to new_ids in all
set_shapefile(shapefile[, idVariable, full]) Adding meta data for writing headers of gal and gwt files.
set_transform([value]) Transformations of weights.
symmetrize([inplace]) Construct a symmetric KNN weight.
to_WSP() Generate a WSP object.
to_adjlist([remove_symmetric, focal_col, …]) Compute an adjacency list representation of a weights object.
to_networkx() Convert a weights object to a networkx graph
__init__(neighbors, weights=None, id_order=None, silence_warnings=False, ids=None)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(neighbors[, weights, id_order, …]) Initialize self.
asymmetry([intrinsic]) Asymmetry check.
from_WSP(WSP[, silence_warnings])
from_adjlist(adjlist[, focal_col, …]) Return an adjacency list representation of a weights object.
from_file([path, format])
from_networkx(graph[, weight_col]) Convert a networkx graph to a PySAL W object.
from_shapefile(*args, **kwargs)
full() Generate a full numpy array.
get_transform() Getter for transform property.
plot(gdf[, indexed_on, ax, color, node_kws, …]) Plot spatial weights objects.
remap_ids(new_ids) In place modification throughout W of id values from w.id_order to new_ids in all
set_shapefile(shapefile[, idVariable, full]) Adding meta data for writing headers of gal and gwt files.
set_transform([value]) Transformations of weights.
symmetrize([inplace]) Construct a symmetric KNN weight.
to_WSP() Generate a WSP object.
to_adjlist([remove_symmetric, focal_col, …]) Compute an adjacency list representation of a weights object.
to_networkx() Convert a weights object to a networkx graph

Attributes

asymmetries List of id pairs with asymmetric weights.
cardinalities Number of neighbors for each observation.
component_labels Store the graph component in which each observation falls.
diagW2 Diagonal of \(WW\).
diagWtW Diagonal of \(W^{'}W\).
diagWtW_WW Diagonal of \(W^{'}W + WW\).
histogram Cardinality histogram as a dictionary where key is the id and value is the number of neighbors for that unit.
id2i Dictionary where the key is an ID and the value is that ID’s index in W.id_order.
id_order Returns the ids for the observations in the order in which they would be encountered if iterating over the weights.
id_order_set Returns True if user has set id_order, False if not.
islands List of ids without any neighbors.
max_neighbors Largest number of neighbors.
mean_neighbors Average number of neighbors.
min_neighbors Minimum number of neighbors.
n Number of units.
n_components Store whether the adjacency matrix is fully connected.
neighbor_offsets Given the current id_order, neighbor_offsets[id] is the offsets of the id’s neighbors in id_order.
nonzero Number of nonzero weights.
pct_nonzero Percentage of nonzero weights.
s0 s0 is defined as
s1 s1 is defined as
s2 s2 is defined as
s2array Individual elements comprising s2.
sd Standard deviation of number of neighbors.
sparse Sparse matrix object.
transform Getter for transform property.
trcW2 Trace of \(WW\).
trcWtW Trace of \(W^{'}W\).
trcWtW_WW Trace of \(W^{'}W + WW\).