require(knitr) opts_chunk$set(error=FALSE, message=FALSE, warning=FALSE) library(BiocNeighbors)
Another application of the KMKNN or VP tree algorithms is to identify all neighboring points within a certain distance^[The default here is Euclidean, but again, we can set
distance="Manhattan" in the
BNPARAM object if so desired.] of the current point.
We first mock up some data:
nobs <- 10000 ndim <- 20 data <- matrix(runif(nobs*ndim), ncol=ndim)
We apply the
findNeighbors() function to
fout <- findNeighbors(data, threshold=1) head(fout$index) head(fout$distance)
Each entry of the
index list corresponds to a point in
data and contains the row indices in
data that are within
For example, the 3rd point in
data has the following neighbors:
... with the following distances to those neighbors:
Note that, for this function, the reported neighbors are not sorted by distance. The order of the output is completely arbitrary and will vary depending on the random seed. However, the identity of the neighbors is fully deterministic.
queryNeighbors() function is also provided for identifying all points within a certain distance of a query point.
Given a query data set:
nquery <- 1000 ndim <- 20 query <- matrix(runif(nquery*ndim), ncol=ndim)
... we apply the
qout <- queryNeighbors(data, query, threshold=1) length(qout$index)
... where each entry of
qout$index corresponds to a row of
query and contains its neighbors in
Again, the order of the output is arbitrary but the identity of the neighbors is deterministic.
Most of the options described for
findKNN() are also applicable here.
subsetto identify neighbors for a subset of points.
get.distanceto avoid retrieving distances when unnecessary.
BPPARAMto parallelize the calculations across multiple workers.
raw.indexto return the raw indices from a precomputed index.
Note that the argument for a precomputed index is
pre <- buildIndex(data, BNPARAM=KmknnParam()) fout.pre <- findNeighbors(BNINDEX=pre, threshold=1) qout.pre <- queryNeighbors(BNINDEX=pre, query=query, threshold=1)
Users are referred to the documentation of each function for specific details.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.