BiocNeighbors 1.12.0
The BiocNeighbors package implements a few algorithms for exact nearest neighbor searching:
Both KMKNN and VP-trees involve a component of randomness during index construction, though the k-nearest neighbors result is fully deterministic1 Except in the presence of ties, see ?"BiocNeighbors-ties"
for details..
The most obvious application is to perform a k-nearest neighbors search. We’ll mock up an example here with a hypercube of points, for which we want to identify the 10 nearest neighbors for each point.
nobs <- 10000
ndim <- 20
data <- matrix(runif(nobs*ndim), ncol=ndim)
The findKNN()
method expects a numeric matrix as input with data points as the rows and variables/dimensions as the columns.
We indicate that we want to use the KMKNN algorithm by setting BNPARAM=KmknnParam()
(which is also the default, so this is not strictly necessary here).
We could use a VP tree instead by setting BNPARAM=VptreeParam()
.
fout <- findKNN(data, k=10, BNPARAM=KmknnParam())
head(fout$index)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 9270 3632 6066 2926 988 2026 2964 9045 618 6986
## [2,] 9035 6262 870 4707 3141 7992 4721 7654 51 4349
## [3,] 4846 212 2266 6843 2797 7281 5732 8945 2893 3563
## [4,] 6219 815 6299 9971 8477 1805 4935 2379 407 7506
## [5,] 4925 8726 8261 2515 1810 3095 8853 4745 4003 1796
## [6,] 8357 7981 4634 4166 7117 4714 4945 351 5361 8640
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.8875658 0.9165160 0.9491488 0.9838018 1.0102840 1.0112358 1.0153539
## [2,] 0.9541882 0.9583006 1.0010773 1.0054063 1.0534855 1.0663020 1.0712607
## [3,] 1.0389791 1.1262140 1.1463258 1.1466033 1.1686822 1.1790572 1.1826732
## [4,] 0.9299565 0.9747464 1.0207835 1.0973522 1.1068473 1.1135258 1.1144616
## [5,] 0.7368091 0.8558207 0.8862046 0.8934122 0.9194348 0.9367372 0.9482482
## [6,] 0.8932100 0.9212009 0.9304916 0.9598434 0.9908211 0.9942246 1.0015833
## [,8] [,9] [,10]
## [1,] 1.0156434 1.0184763 1.0447238
## [2,] 1.0720707 1.0737502 1.0810976
## [3,] 1.1882094 1.1882113 1.1886964
## [4,] 1.1144795 1.1231610 1.1291151
## [5,] 0.9493889 0.9514112 0.9781105
## [6,] 1.0035501 1.0233472 1.0344593
Each row of the index
matrix corresponds to a point in data
and contains the row indices in data
that are its nearest neighbors.
For example, the 3rd point in data
has the following nearest neighbors:
fout$index[3,]
## [1] 4846 212 2266 6843 2797 7281 5732 8945 2893 3563
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 1.038979 1.126214 1.146326 1.146603 1.168682 1.179057 1.182673 1.188209
## [9] 1.188211 1.188696
Note that the reported neighbors are sorted by distance.
Another application is to identify the k-nearest neighbors in one dataset based on query points in another dataset. Again, we mock up a small data set:
nquery <- 1000
ndim <- 20
query <- matrix(runif(nquery*ndim), ncol=ndim)
We then use the queryKNN()
function to identify the 5 nearest neighbors in data
for each point in query
.
qout <- queryKNN(data, query, k=5, BNPARAM=KmknnParam())
head(qout$index)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1181 9014 7304 7644 3277
## [2,] 4683 1078 1584 9187 4619
## [3,] 5228 4444 3476 3315 9555
## [4,] 471 6290 3617 8733 987
## [5,] 2380 6434 6842 7073 1597
## [6,] 9008 3694 9807 2770 8837
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8334414 1.0667303 1.0769156 1.0971659 1.1109855
## [2,] 0.9682987 0.9727096 0.9747299 0.9830652 0.9898010
## [3,] 1.0317505 1.0489856 1.0493264 1.0589954 1.0751410
## [4,] 0.7888981 0.8060299 1.0219693 1.0293811 1.0601746
## [5,] 0.7916928 0.8351133 0.8925435 0.9085304 0.9282645
## [6,] 0.8313627 0.8486379 0.8591487 0.9627915 0.9675100
Each row of the index
matrix contains the row indices in data
that are the nearest neighbors of a point in query
.
For example, the 3rd point in query
has the following nearest neighbors in data
:
qout$index[3,]
## [1] 5228 4444 3476 3315 9555
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 1.031751 1.048986 1.049326 1.058995 1.075141
Again, the reported neighbors are sorted by distance.
Users can perform the search for a subset of query points using the subset=
argument.
This yields the same result as but is more efficient than performing the search for all points and subsetting the output.
findKNN(data, k=5, subset=3:5)
## $index
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4846 212 2266 6843 2797
## [2,] 6219 815 6299 9971 8477
## [3,] 4925 8726 8261 2515 1810
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0389791 1.1262140 1.1463258 1.1466033 1.1686822
## [2,] 0.9299565 0.9747464 1.0207835 1.0973522 1.1068473
## [3,] 0.7368091 0.8558207 0.8862046 0.8934122 0.9194348
If only the indices are of interest, users can set get.distance=FALSE
to avoid returning the matrix of distances.
This will save some time and memory.
names(findKNN(data, k=2, get.distance=FALSE))
## [1] "index"
It is also simple to speed up functions by parallelizing the calculations with the BiocParallel framework.
library(BiocParallel)
out <- findKNN(data, k=10, BPPARAM=MulticoreParam(3))
For multiple queries to a constant data
, the pre-clustering can be performed in a separate step with buildIndex()
.
The result can then be passed to multiple calls, avoiding the overhead of repeated clustering2 The algorithm type is automatically determined when BNINDEX
is specified, so there is no need to also specify BNPARAM
in the later functions..
pre <- buildIndex(data, BNPARAM=KmknnParam())
out1 <- findKNN(BNINDEX=pre, k=5)
out2 <- queryKNN(BNINDEX=pre, query=query, k=2)
The default setting is to search on the Euclidean distance.
Alternatively, we can use the Manhattan distance by setting distance="Manhattan"
in the BiocNeighborParam
object.
out.m <- findKNN(data, k=5, BNPARAM=KmknnParam(distance="Manhattan"))
Advanced users may also be interested in the raw.index=
argument, which returns indices directly to the precomputed object rather than to data
.
This may be useful inside package functions where it may be more convenient to work on a common precomputed object.
sessionInfo()
## R version 4.1.1 Patched (2021-08-22 r80813)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.28.0 BiocNeighbors_1.12.0 knitr_1.36
## [4] BiocStyle_2.22.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 magrittr_2.0.1 BiocGenerics_0.40.0
## [4] lattice_0.20-45 R6_2.5.1 rlang_0.4.12
## [7] fastmap_1.1.0 stringr_1.4.0 tools_4.1.1
## [10] parallel_4.1.1 grid_4.1.1 xfun_0.27
## [13] jquerylib_0.1.4 htmltools_0.5.2 yaml_2.2.1
## [16] digest_0.6.28 bookdown_0.24 Matrix_1.3-4
## [19] BiocManager_1.30.16 S4Vectors_0.32.0 sass_0.4.0
## [22] evaluate_0.14 rmarkdown_2.11 stringi_1.7.5
## [25] compiler_4.1.1 bslib_0.3.1 stats4_4.1.1
## [28] jsonlite_1.7.2
Wang, X. 2012. “A Fast Exact k-Nearest Neighbors Algorithm for High Dimensional Search Using k-Means Clustering and Triangle Inequality.” Proc Int Jt Conf Neural Netw 43 (6): 2351–8.
Yianilos, P. N. 1993. “Data Structures and Algorithms for Nearest Neighbor Search in General Metric Spaces.” In SODA, 93:311–21. 194.