libpysal.weights.Queen

class libpysal.weights.Queen(polygons, **kw)[source]

Construct a weights object from a collection of pysal polygons that share at least one vertex.

Parameters
polygonslist

a collection of PySAL shapes to build weights from

idslist

a list of names to use to build the weights

**kwkeyword arguments

optional arguments for pysal.weights.W

__init__(polygons, **kw)[source]

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

Methods

__init__(polygons, **kw)

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_dataframe(df[, geom_col])

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 networkx graph to a PySAL W object.

from_shapefile(filepath[, idVariable, full])

Queen contiguity weights from a polygon shapefile.

from_xarray(da[, z_value, coords_labels, k, …])

Construct a weights object from a xarray.DataArray with an additional attribute index containing coordinate values of the raster in the form of Pandas.Index/MultiIndex.

full()

Generate a full numpy.ndarray.

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 metadata 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_file([path, format])

Write a weights to a file.

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\).

classmethod from_dataframe(df, geom_col=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
dfDataFrame

a :class: pandas.DataFrame containing geometries to use for spatial weights

geom_colstr

the name of the column in df that contains the geometries. Defaults to active geometry column

idVariablestr

the name of the column to use as IDs. If nothing is provided, the dataframe index is used

idslist

a list of ids to use to index the spatial weights object. Order is not respected from this list.

id_orderlist

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.

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
iterableiterable

a collection of of shapes to be cast to PySAL shapes. Must support iteration. Contents may either be a shapely or PySAL shape.

**kwkeyword arguments

optional arguments for pysal.weights.W

See Also
———
:class:`libpysal.weights.weights.W`
:class:`libpysal.weights.contiguiyt.Queen`
classmethod from_shapefile(filepath, idVariable=None, full=False, **kwargs)[source]

Queen contiguity weights from a polygon shapefile.

Parameters
shapefilestr

name of polygon shapefile including suffix.

idVariablestr

name of a column in the shapefile’s DBF to use for ids.

sparsebool

If True return WSP instance If False return W instance

Returns
——-
wW

instance of spatial weights

Examples

>>> from libpysal.weights import Queen
>>> import libpysal
>>> wq=Queen.from_shapefile(libpysal.examples.get_path("columbus.shp"))
>>> "%.3f"%wq.pct_nonzero
'9.829'
>>> wq=Queen.from_shapefile(libpysal.examples.get_path("columbus.shp"),"POLYID")
>>> "%.3f"%wq.pct_nonzero
'9.829'
>>> wq=Queen.from_shapefile(libpysal.examples.get_path("columbus.shp"), sparse=True)
>>> pct_sp = wq.sparse.nnz *1. / wq.n**2
>>> "%.3f"%pct_sp
'0.098'

Notes

Queen contiguity defines as neighbors any pair of polygons that share at least one vertex in their polygon definitions.

classmethod from_xarray(da, z_value=None, coords_labels={}, k=1, include_nodata=False, n_jobs=1, sparse=True, **kwargs)[source]

Construct a weights object from a xarray.DataArray with an additional attribute index containing coordinate values of the raster in the form of Pandas.Index/MultiIndex.

Parameters
daxarray.DataArray

Input 2D or 3D DataArray with shape=(z, y, x)

z_valueint/string/float

Select the z_value of 3D DataArray with multiple layers.

coords_labelsdictionary

Pass dimension labels for coordinates and layers if they do not belong to default dimensions, which are (band/time, y/lat, x/lon) e.g. coords_labels = {“y_label”: “latitude”, “x_label”: “longitude”, “z_label”: “year”} Default is {} empty dictionary.

sparsebool

type of weight object. Default is True. For libpysal.weights.W, sparse = False

kint

Order of contiguity, this will select all neighbors upto kth order. Default is 1.

include_nodatabool

If True, missing values will be assumed as non-missing when selecting higher_order neighbors, Default is False

n_jobsint

Number of cores to be used in the sparse weight construction. If -1, all available cores are used. Default is 1.

**kwargskeyword arguments

optional arguments passed when sparse = False

Returns
wlibpysal.weights.W/libpysal.weights.WSP

instance of spatial weights class W or WSP with an index attribute

Notes

  1. Lower order contiguities are also selected.

2. Returned object contains index attribute that includes a Pandas.MultiIndex object from the DataArray.