require(knitr) opts_chunk$set(error=FALSE, message=FALSE, warning=FALSE) library(BiocNeighbors)
r Biocpkg("BiocNeighbors") package provides several algorithms for approximate neighbor searches:
r CRANpkg("RcppAnnoy")package. It works by building a tree where a random hyperplane partitions a group of points into two child groups at each internal node. This is repeated to construct a forest of trees where the number of trees determines the accuracy of the search. Given a query data point, we identify all points in the same leaf node for each tree. We then take the union of leaf node sets across trees and search them exactly for the nearest neighbors.
r CRANpkg("RcppHNSW")package. It works by building a series of nagivable small world graphs containing links between points across the entire data set. The algorithm walks through the graphs where each step is chosen to move closer to a given query point. Different graphs contain links of different lengths, yielding a hierarchy where earlier steps are large and later steps are small. The accuracy of the search is determined by the connectivity of the graphs and the size of the intermediate list of potential neighbors.
These methods complement the exact algorithms
r Biocpkg("BiocNeighbors", vignette="exact.html", label="described previously").
Again, it is straightforward to switch from one algorithm to another by simply changing the
BNPARAM argument in
We perform the k-nearest neighbors search with the Annoy algorithm by specifying
nobs <- 10000 ndim <- 20 data <- matrix(runif(nobs*ndim), ncol=ndim) fout <- findKNN(data, k=10, BNPARAM=AnnoyParam()) head(fout$index) head(fout$distance)
We can also identify the k-nearest neighbors in one dataset based on query points in another dataset.
nquery <- 1000 ndim <- 20 query <- matrix(runif(nquery*ndim), ncol=ndim) qout <- queryKNN(data, query, k=5, BNPARAM=AnnoyParam()) head(qout$index) head(qout$distance)
It is similarly easy to use the HNSW algorithm by setting
Most of the options described for the exact methods are also applicable here. For example:
subsetto identify neighbors for a subset of points.
get.distanceto avoid retrieving distances when unnecessary.
BPPARAMto parallelize the calculations across multiple workers.
BNINDEXto build the forest once for a given data set and re-use it across calls.
The use of a pre-built
BNINDEX is illustrated below:
pre <- buildIndex(data, BNPARAM=AnnoyParam()) out1 <- findKNN(BNINDEX=pre, k=5) out2 <- queryKNN(BNINDEX=pre, query=query, k=2)
Both Annoy and HNSW perform searches based on the Euclidean distance by default.
Searching by Manhattan distance is done by simply setting
Users are referred to the documentation of each function for specific details on the available arguments.
Both Annoy and HNSW generate indexing structures - a forest of trees and series of graphs, respectively -
that are saved to file when calling
By default, this file is located in
tempdir()^[On HPC file systems, you can change
TEMPDIR to a location that is more amenable to concurrent access.] and will be removed when the session finishes.
If the index is to persist across sessions, the path of the index file can be directly specified in
This can be used to construct an index object directly using the relevant constructors, e.g.,
However, it becomes the responsibility of the user to clean up any temporary indexing files after calculations are complete.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.