libpysal.weights.Rook¶
-
class
libpysal.weights.Rook(polygons, **kw)[source]¶ Construct a weights object from a collection of pysal polygons that share at least one edge.
- Parameters
- polygons
python:list a collection of PySAL shapes to build weights from
- ids
python:list a list of names to use to build the weights
- **kw
keywordarguments optional arguments for
pysal.weights.W
- polygons
See also
libpysal.weights.weights.W
-
__init__(self, polygons, \*\*kw)[source]¶ Initialize self. See help(type(self)) for accurate signature.
Methods
__init__(self, polygons, \*\*kw)Initialize self.
asymmetry(self[, intrinsic])Asymmetry check.
from_WSP(WSP[, silence_warnings])from_adjlist(adjlist[, focal_col, …])Return an adjacency list representation of a weights object.
from_dataframe(df[, geom_col, idVariable, …])Construct a weights object from a pandas dataframe with a geometry column.
from_file([path, format])Read a weights file into a W object.
from_iterable(iterable[, sparse])Construct a weights object from a collection of arbitrary polygons.
from_networkx(graph[, weight_col])Convert a
networkxgraph to a PySALWobject.from_shapefile(filepath[, idVariable, full])Rook contiguity weights from a polygon shapefile.
full(self)Generate a full
numpy.ndarray.get_transform(self)Getter for transform property.
plot(self, gdf[, indexed_on, ax, color, …])Plot spatial weights objects.
remap_ids(self, new_ids)In place modification throughout
Wof id values fromw.id_ordertonew_idsin all.set_shapefile(self, shapefile[, idVariable, …])Adding metadata for writing headers of
.galand.gwtfiles.set_transform(self[, value])Transformations of weights.
symmetrize(self[, inplace])Construct a symmetric KNN weight.
to_WSP(self)Generate a
WSPobject.to_adjlist(self[, remove_symmetric, …])Compute an adjacency list representation of a weights object.
to_file(self[, path, format])Write a weights to a file.
to_networkx(self)Convert a weights object to a
networkxgraph.Attributes
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
Trueif user has setid_order,Falseif 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 inid_order.nonzeroNumber of nonzero weights.
pct_nonzeroPercentage of nonzero weights.
s0s0is defined ass1s1is defined ass2s2is defined ass2arrayIndividual 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\).
-
classmethod
from_dataframe(df, geom_col='geometry', idVariable=None, ids=None, id_order=None, \*\*kwargs)[source]¶ Construct a weights object from a pandas dataframe with a geometry column. This will cast the polygons to PySAL polygons, then build the W using ids from the dataframe.
- Parameters
- df
DataFrame a :class: pandas.DataFrame containing geometries to use for spatial weights
- geom_col
python:str the name of the column in df that contains the geometries. Defaults to geometry
- idVariable
python:str the name of the column to use as IDs. If nothing is provided, the dataframe index is used
- ids
python:list a list of ids to use to index the spatial weights object. Order is not respected from this list.
- id_order
python:list an ordered list of ids to use to index the spatial weights object. If used, the resulting weights object will iterate over results in the order of the names provided in this argument.
- df
See also
libpysal.weights.weights.Wlibpysal.weights.contiguity.Rook
-
classmethod
from_iterable(iterable, sparse=False, \*\*kwargs)[source]¶ Construct a weights object from a collection of arbitrary polygons. This will cast the polygons to PySAL polygons, then build the W.
- Parameters
- iterablepython:iterable
a collection of of shapes to be cast to PySAL shapes. Must support iteration. Can be either Shapely or PySAL shapes.
- **kw
keywordarguments optional arguments for
pysal.weights.W- See Also
- ——–
- :class:`libpysal.weights.weights.W`
- :class:`libpysal.weights.contiguity.Rook`
-
classmethod
from_shapefile(filepath, idVariable=None, full=False, \*\*kwargs)[source]¶ Rook contiguity weights from a polygon shapefile.
- Parameters
- shapefile
python:str name of polygon shapefile including suffix.
- sparsebool
If True return WSP instance If False return W instance
- shapefile
- Returns
- w
W instance of spatial weights
- w
See also
libpysal.weights.weights.Wlibpysal.weights.contiguity.Rook
Notes
Rook contiguity defines as neighbors any pair of polygons that share a common edge in their polygon definitions.
Examples
>>> from libpysal.weights import Rook >>> import libpysal >>> wr=Rook.from_shapefile(libpysal.examples.get_path("columbus.shp"), "POLYID") >>> "%.3f"%wr.pct_nonzero '8.330' >>> wr=Rook.from_shapefile(libpysal.examples.get_path("columbus.shp"), sparse=True) >>> pct_sp = wr.sparse.nnz *1. / wr.n**2 >>> "%.3f"%pct_sp '0.083'