libpysal.weights.
KNN
(data, k=2, p=2, ids=None, radius=None, distance_metric='euclidean', **kwargs)[source]¶Creates nearest neighbor weights matrix based on k nearest neighbors.
Parameters: |
|
---|---|
Returns: |
|
See also
libpysal.weights.weights.W
Notes
Ties between neighbors of equal distance are arbitrarily broken.
Examples
>>> import libpysal
>>> import numpy as np
>>> points = [(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)]
>>> kd = libpysal.cg.KDTree(np.array(points))
>>> wnn2 = libpysal.weights.KNN(kd, 2)
>>> [1,3] == wnn2.neighbors[0]
True
>>> wnn2 = KNN(kd,2)
>>> wnn2[0]
{1: 1.0, 3: 1.0}
>>> wnn2[1]
{0: 1.0, 3: 1.0}
now with 1 rather than 0 offset
>>> wnn2 = libpysal.weights.KNN(kd, 2, ids=range(1,7))
>>> wnn2[1]
{2: 1.0, 4: 1.0}
>>> wnn2[2]
{1: 1.0, 4: 1.0}
>>> 0 in wnn2.neighbors
False
Attributes: |
|
---|
Methods
asymmetry ([intrinsic]) |
Asymmetry check. |
from_adjlist (adjlist[, focal_col, …]) |
Return an adjacency list representation of a weights object. |
from_array (array, *args, **kwargs) |
Creates nearest neighbor weights matrix based on k nearest neighbors. |
from_dataframe (df[, geom_col, ids]) |
Make KNN weights from a dataframe. |
from_networkx (graph[, weight_col]) |
Convert a networkx graph to a PySAL W object. |
from_shapefile (filepath, *args, **kwargs) |
Nearest neighbor weights from a shapefile. |
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 |
reweight ([k, p, new_data, new_ids, inplace]) |
Redo K-Nearest Neighbor weights construction using given parameters |
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 |
from_WSP | |
from_file |
__init__
(data, k=2, p=2, ids=None, radius=None, distance_metric='euclidean', **kwargs)[source]¶Initialize self. See help(type(self)) for accurate signature.
Methods
__init__ (data[, k, p, ids, radius, …]) |
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_array (array, *args, **kwargs) |
Creates nearest neighbor weights matrix based on k nearest neighbors. |
from_dataframe (df[, geom_col, ids]) |
Make KNN 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, *args, **kwargs) |
Nearest neighbor weights from a shapefile. |
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 |
reweight ([k, p, new_data, new_ids, inplace]) |
Redo K-Nearest Neighbor weights construction using given parameters |
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\). |