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
- dataarray
(n,k) or KDTree where KDtree.data is array (n,k) n observations on k characteristics used to measure distances between the n objects
- thresholdfloat
distance band
- pfloat
Minkowski p-norm distance metric parameter: 1<=p<=infinity 2: Euclidean distance 1: Manhattan distance
- binaryboolean
If true w_{ij}=1 if d_{i,j}<=threshold, otherwise w_{i,j}=0 If false wij=dij^{alpha}
- alphafloat
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
- idslist
values to use for keys of the neighbors and weights dicts
- build_spboolean
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
- silentboolean
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.
Notes
This was initially implemented running scipy 0.8.0dev (in epd 6.1). earlier versions of scipy (0.7.0) have a logic bug in scipy/sparse/dok.py so serge changed line 221 of that file on sal-dev to fix the logic bug.
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]
- Attributes
- weightsdict
of neighbor weights keyed by observation id
- neighborsdict
of neighbors keyed by observation id
-
__init__
(self, 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__
(self, data, threshold[, p, alpha, …])Casting to floats is a work around for a bug in scipy.spatial.
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_array
(array, threshold, \*\*kwargs)Construct a DistanceBand weights from an array.
from_dataframe
(df, threshold[, geom_col, ids])Make DistanceBand weights from a dataframe.
from_file
([path, format])from_networkx
(graph[, weight_col])Convert a networkx graph to a PySAL W object.
from_shapefile
(filepath, threshold[, idVariable])Distance-band based weights from shapefile
full
(self)Generate a full numpy array.
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 W of id values from w.id_order to new_ids in all
set_shapefile
(self, shapefile[, idVariable, …])Adding meta data for writing headers of gal and gwt files.
set_transform
(self[, value])Transformations of weights.
symmetrize
(self[, inplace])Construct a symmetric KNN weight.
to_WSP
(self)Generate a WSP object.
to_adjlist
(self[, remove_symmetric, …])Compute an adjacency list representation of a weights object.
to_networkx
(self)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_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='geometry', ids=None, **kwargs)[source]¶ Make DistanceBand weights from a dataframe.
- Parameters
- dfpandas.dataframe
a dataframe with a geometry column that can be used to construct a W object
- geom_colstring
column name of the geometry stored in df
- idsstring or iterable
if string, the column name of the indices from the dataframe if iterable, a list of ids to use for the W if None, df.index is used.