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.
PySAL KDTree or ArcKDTree where KDtree.data is array (n,k) n observations on k characteristics used to measure distances between the n objects
number of nearest neighbors
Minkowski p-norm distance metric parameter: 1<=p<=infinity 2: Euclidean distance 1: Manhattan distance Ignored if the KDTree is an ArcKDTree
identifiers to attach to each observation
instance Weights object with binary weights
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
__init__
(self, data, k=2, p=2, ids=None, radius=None, distance_metric='euclidean', **kwargs)[source]¶Initialize self. See help(type(self)) for accurate signature.
Methods
|
Initialize self. |
|
Asymmetry check. |
|
|
|
Return an adjacency list representation of a weights object. |
|
Creates nearest neighbor weights matrix based on k nearest neighbors. |
|
Make KNN weights from a dataframe. |
|
|
|
Convert a networkx graph to a PySAL W object. |
|
Nearest neighbor weights from a shapefile. |
|
Generate a full numpy array. |
|
Getter for transform property. |
|
Plot spatial weights objects. |
|
In place modification throughout W of id values from w.id_order to new_ids in all |
|
Redo K-Nearest Neighbor weights construction using given parameters |
|
Adding meta data for writing headers of gal and gwt files. |
|
Transformations of weights. |
|
Construct a symmetric KNN weight. |
|
Generate a WSP object. |
|
Compute an adjacency list representation of a weights object. |
|
Convert a weights object to a networkx graph |
Attributes
|
List of id pairs with asymmetric weights. |
|
Number of neighbors for each observation. |
|
Store the graph component in which each observation falls. |
|
Diagonal of \(WW\). |
|
Diagonal of \(W^{'}W\). |
|
Diagonal of \(W^{'}W + WW\). |
|
Cardinality histogram as a dictionary where key is the id and value is the number of neighbors for that unit. |
|
Dictionary where the key is an ID and the value is that ID’s index in W.id_order. |
|
Returns the ids for the observations in the order in which they would be encountered if iterating over the weights. |
|
Returns True if user has set id_order, False if not. |
|
List of ids without any neighbors. |
|
Largest number of neighbors. |
|
Average number of neighbors. |
|
Minimum number of neighbors. |
|
Number of units. |
|
Store whether the adjacency matrix is fully connected. |
|
Given the current id_order, neighbor_offsets[id] is the offsets of the id’s neighbors in id_order. |
|
Number of nonzero weights. |
|
Percentage of nonzero weights. |
|
s0 is defined as |
|
s1 is defined as |
|
s2 is defined as |
|
Individual elements comprising s2. |
|
Standard deviation of number of neighbors. |
|
Sparse matrix object. |
|
Getter for transform property. |
|
Trace of \(WW\). |
|
Trace of \(W^{'}W\). |
|
Trace of \(W^{'}W + WW\). |
from_array
(array, *args, **kwargs)[source]¶Creates nearest neighbor weights matrix based on k nearest neighbors.
(n, k) array representing n observations on k characteristics used to measure distances between the n objects
instance Weights object with binary weights
See also
libpysal.weights.weights.W
Notes
Ties between neighbors of equal distance are arbitrarily broken.
Examples
>>> from libpysal.weights import KNN
>>> points = [(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)]
>>> wnn2 = KNN.from_array(points, 2)
>>> [1,3] == wnn2.neighbors[0]
True
>>> wnn2 = KNN.from_array(points,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 = KNN.from_array(points, 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
from_dataframe
(df, geom_col='geometry', ids=None, *args, **kwargs)[source]¶Make KNN weights from a dataframe.
a dataframe with a geometry column that can be used to construct a W object
column name of the geometry stored in df
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.
See also
libpysal.weights.weights.W
from_shapefile
(filepath, *args, **kwargs)[source]¶Nearest neighbor weights from a shapefile.
shapefile containing attribute data.
number of nearest neighbors
Minkowski p-norm distance metric parameter: 1<=p<=infinity 2: Euclidean distance 1: Manhattan distance
identifiers to attach to each observation
If supplied arc_distances will be calculated based on the given radius. p will be ignored.
instance; Weights object with binary weights.
See also
libpysal.weights.weights.W
Notes
Ties between neighbors of equal distance are arbitrarily broken.
Examples
Polygon shapefile >>> import libpysal >>> from libpysal.weights import KNN >>> wc=KNN.from_shapefile(libpysal.examples.get_path(“columbus.shp”)) >>> “%.4f”%wc.pct_nonzero ‘4.0816’ >>> set([2,1]) == set(wc.neighbors[0]) True >>> wc3=KNN.from_shapefile(libpysal.examples.get_path(“columbus.shp”),k=3) >>> set(wc3.neighbors[0]) == set([2,1,3]) True >>> set(wc3.neighbors[2]) == set([4,3,0]) True
Point shapefile
>>> w=KNN.from_shapefile(libpysal.examples.get_path("juvenile.shp"))
>>> w.pct_nonzero
1.1904761904761905
>>> w1=KNN.from_shapefile(libpysal.examples.get_path("juvenile.shp"),k=1)
>>> "%.3f"%w1.pct_nonzero
'0.595'
reweight
(self, k=None, p=None, new_data=None, new_ids=None, inplace=True)[source]¶Redo K-Nearest Neighbor weights construction using given parameters
an array containing additional data to use in the KNN weight
a list aligned with new_data that provides the ids for each new observation
a flag denoting whether to modify the KNN object in place or to return a new KNN object
number of nearest neighbors
Minkowski p-norm distance metric parameter: 1<=p<=infinity 2: Euclidean distance 1: Manhattan distance Ignored if the KDTree is an ArcKDTree