libpysal.weights.DistanceBand¶
- class libpysal.weights.DistanceBand(data, threshold, p=2, alpha=-1.0, binary=True, ids=None, build_sp=True, silence_warnings=False, distance_metric='euclidean', radius=None)[source]¶
Spatial weights based on distance band.
- Parameters:
- data
array (n,k) or KDTree where KDtree.data is array (n,k) n observations on k characteristics used to measure distances between the n objects
- threshold
float distance band
- p
float DEPRECATED: use distance_metric Minkowski p-norm distance metric parameter: 1<=p<=infinity 2: Euclidean distance 1: Manhattan distance
- binarybool
If true w_{ij}=1 if d_{i,j}<=threshold, otherwise w_{i,j}=0 If false wij=dij^{alpha}
- alpha
float distance decay parameter for weight (default -1.0) if alpha is positive the weights will not decline with distance. If binary is True, alpha is ignored
- ids
list values to use for keys of the neighbors and weights dicts
- build_spbool
DEPRECATED True to build sparse distance matrix and false to build dense distance matrix; significant speed gains may be obtained dending on the sparsity of the of distance_matrix and threshold that is applied
- silentbool
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.
- data
- Attributes:
Examples
>>> import libpysal >>> points=[(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)] >>> wcheck = libpysal.weights.W( ... {0: [1, 3], 1: [0, 3], 2: [], 3: [0, 1], 4: [5], 5: [4]} ... )
WARNING: there is one disconnected observation (no neighbors) Island id: [2] >>> w=libpysal.weights.DistanceBand(points,threshold=11.2)
WARNING: there is one disconnected observation (no neighbors) Island id: [2] >>> libpysal.weights.util.neighbor_equality(w, wcheck) True >>> w=libpysal.weights.DistanceBand(points,threshold=14.2) >>> wcheck = libpysal.weights.W( … {0: [1, 3], 1: [0, 3, 4], 2: [4], 3: [1, 0], 4: [5, 2, 1], 5: [4]} … ) >>> libpysal.weights.util.neighbor_equality(w, wcheck) True
inverse distance weights
>>> w=libpysal.weights.DistanceBand(points,threshold=11.2,binary=False)
WARNING: there is one disconnected observation (no neighbors) Island id: [2] >>> w.weights[0] [0.1, 0.08944271909999159] >>> w.neighbors[0].tolist() [1, 3]
gravity weights
>>> w=libpysal.weights.DistanceBand(points,threshold=11.2,binary=False,alpha=-2.)
WARNING: there is one disconnected observation (no neighbors) Island id: [2] >>> w.weights[0] [0.01, 0.007999999999999998]
- __init__(data, threshold, p=2, alpha=-1.0, binary=True, ids=None, build_sp=True, silence_warnings=False, distance_metric='euclidean', radius=None)[source]¶
Casting to floats is a work around for a bug in scipy.spatial. See detail in pysal issue #126.
Methods
__init__(data, threshold[, p, alpha, ...])Casting to floats is a work around for a bug in scipy.spatial.
asymmetry([intrinsic])Asymmetry check.
from_WSP(WSP[, silence_warnings])Create a pysal W from a pysal WSP object (thin weights matrix).
from_adjlist(adjlist[, focal_col, ...])Return an adjacency list representation of a weights object.
from_array(array, threshold, **kwargs)Construct a DistanceBand weights from an array.
from_dataframe(df, threshold[, geom_col, ...])Make DistanceBand weights from a dataframe.
from_file([path, format])Read a weights file into a W object.
from_networkx(graph[, weight_col])Convert a
networkxgraph to a PySALWobject.from_shapefile(filepath, threshold[, idVariable])Distance-band based weights from shapefile
from_sparse(sparse)Convert a
scipy.sparsearray to a PySALWobject.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
Wof id values fromw.id_ordertonew_idsin all.set_shapefile(shapefile[, idVariable, full])Adding metadata for writing headers of
.galand.gwtfiles.set_transform([value])Transformations of weights.
symmetrize([inplace])Construct a symmetric KNN weight.
to_WSP()Generate a
WSPobject.to_adjlist([remove_symmetric, drop_islands, ...])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
networkxgraph.to_sparse([fmt])Generate a
scipy.sparsearray object from a pysal W.Attributes
asymmetriesList of id pairs with asymmetric weights sorted in ascending index location order.
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_array(array, threshold, **kwargs)[source]¶
Construct a DistanceBand weights from an array. Supports all the same options as
libpysal.weights.DistanceBand
- classmethod from_dataframe(df, threshold, geom_col=None, ids=None, use_index=True, **kwargs)[source]¶
Make DistanceBand weights from a dataframe.
- Parameters:
- df
pandas.dataframe a dataframe with a geometry column that can be used to construct a W object
- geom_col
str the name of the column in df that contains the geometries. Defaults to active geometry column.
- idslist-like,
str a list-like of ids to use to index the spatial weights object or the name of the column to use as IDs. If nothing is provided, the dataframe index is used if use_index=True or a positional index is used if use_index=False. Order of the resulting W is not respected from this list.
- use_indexbool
use index of df as ids to index the spatial weights object.
- df