Find the k Nearest Neighbors
This function uses a kd-tree to find all k nearest neighbors in a data matrix (including distances) fast.
kNN(x, k, sort = TRUE, search = "kdtree", bucketSize = 10, splitRule = "suggest", approx = 0)
- a data matrix, a dist object or a kNN object.
- number of neighbors to find.
- nearest neighbor search strategy (one of "kdtree", "linear" or "dist").
- sort the neighbors by distance? Note that this is expensive and
sort = FALSEis much faster. kNN objects can be sorted using
- max size of the kd-tree leafs.
- rule to split the kd-tree. One of "STD", "MIDPT", "FAIR", "SL_MIDPT", "SL_FAIR" or "SUGGEST" (SL stands for sliding). "SUGGEST" uses ANNs best guess.
- use approximate nearest neighbors. All NN up to a distance of
a factor of 1+
approxeps may be used. Some actual NN may be omitted leading to spurious clusters and noise points. However, the algorithm will enjoy a significant speedup.
search controls if a kd-tree or linear search (both implemented in
the ANN library; see Mount and Arya, 2010). Note, that these implementations cannot handle NAs.
search="dist" precomputes Euclidean distances first using R.
NAs are handled, but the resulting distance matrix cannot contain NAs. To use other distance measures, a precomputed distance matrix can be
search is ignored).
splitRule influence how the kd-tree is built.
approx uses the approximate nearest neighbor search implemented in ANN.
All nearest neighbors up to a distance of
will be considered and all with a distance greater than
eps will not
be considered. The other points might be considered. Note that this results in
some actual nearest neighbors being omitted leading to spurious clusters and noise points. However, the algorithm will enjoy a significant speedup. For more details see Mount and Arya (2010). Note: self-matches are removed!
An object of class kNN containing a list with the following components:
David M. Mount and Sunil Arya (2010). ANN: A Library for Approximate Nearest Neighbor Searching, http://www.cs.umd.edu/~mount/ANN/.
data(iris) x <- iris[, -5] # finding kNN directly in data (using a kd-tree) nn <- kNN(x, k=5) nn # explore neighborhood of point 10 i <- 10 nn$id[i,] plot(x, col = ifelse(1:nrow(iris) %in% nn$id[i,], "red", "black")) # Visualize the 5 nearest neighbors plot(nn, x) # Visualize a reduced 2-NN graph plot(kNN(nn, k = 2), x)