BiocNeighbors 1.21.2
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,] 7030 3505 7135 3980 2888 2312 8962 8800 5653 5508
## [2,] 2880 612 9755 345 8903 7187 7076 1273 2066 3141
## [3,] 8713 4297 3273 2204 561 3641 296 3766 6091 1180
## [4,] 8781 1830 1483 9211 478 2980 9959 8221 1555 6995
## [5,] 9752 7799 2104 6939 8142 4752 6528 3479 4276 9218
## [6,] 5174 4516 540 2002 1195 3030 4388 934 7731 5719
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.9975043 1.0807157 1.1156026 1.1157647 1.1363585 1.1386389 1.142451
## [2,] 0.9365430 0.9370640 0.9477851 0.9639305 0.9712702 0.9757776 1.004613
## [3,] 1.0038910 1.0125200 1.0936371 1.1101953 1.1450025 1.1619763 1.181290
## [4,] 0.9761846 1.0033327 1.1260520 1.1509686 1.1702621 1.1840650 1.197684
## [5,] 0.9111768 0.9555185 1.0105262 1.0628738 1.0739344 1.0931774 1.097458
## [6,] 0.9221052 0.9810187 1.0044383 1.0492361 1.0503656 1.0603614 1.071291
## [,8] [,9] [,10]
## [1,] 1.161919 1.169721 1.171899
## [2,] 1.013962 1.014666 1.021363
## [3,] 1.196823 1.199124 1.200235
## [4,] 1.207901 1.218113 1.222222
## [5,] 1.123617 1.125617 1.127123
## [6,] 1.088907 1.102767 1.109268
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] 8713 4297 3273 2204 561 3641 296 3766 6091 1180
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 1.003891 1.012520 1.093637 1.110195 1.145003 1.161976 1.181290 1.196823
## [9] 1.199124 1.200235
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,] 2671 3078 2820 4692 3608
## [2,] 3895 5770 5203 4309 9851
## [3,] 9769 9481 1024 2336 670
## [4,] 7097 3229 4027 7888 1913
## [5,] 2790 3809 3281 9191 3850
## [6,] 1094 6473 3900 2891 1060
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8599789 0.9335548 0.9395572 0.9451608 0.9514297
## [2,] 0.9346660 0.9871152 1.0268129 1.0576261 1.0660929
## [3,] 0.9500317 0.9580867 0.9635958 1.0025870 1.0076481
## [4,] 0.8890576 0.9004721 0.9206807 0.9529872 0.9939445
## [5,] 0.8643810 0.9119035 0.9836306 0.9867008 0.9979217
## [6,] 0.9676366 1.0332674 1.0739028 1.0748869 1.0750875
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] 9769 9481 1024 2336 670
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.9500317 0.9580867 0.9635958 1.0025870 1.0076481
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,] 8713 4297 3273 2204 561
## [2,] 8781 1830 1483 9211 478
## [3,] 9752 7799 2104 6939 8142
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0038910 1.0125200 1.093637 1.110195 1.145003
## [2,] 0.9761846 1.0033327 1.126052 1.150969 1.170262
## [3,] 0.9111768 0.9555185 1.010526 1.062874 1.073934
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.4.0 beta (2024-04-15 r86425)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.19-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.37.1 BiocNeighbors_1.21.2 knitr_1.46
## [4] BiocStyle_2.31.0
##
## loaded via a namespace (and not attached):
## [1] cli_3.6.2 rlang_1.1.3 xfun_0.43
## [4] jsonlite_1.8.8 S4Vectors_0.41.6 htmltools_0.5.8.1
## [7] stats4_4.4.0 sass_0.4.9 rmarkdown_2.26
## [10] grid_4.4.0 evaluate_0.23 jquerylib_0.1.4
## [13] fastmap_1.1.1 yaml_2.3.8 lifecycle_1.0.4
## [16] bookdown_0.39 BiocManager_1.30.22 compiler_4.4.0
## [19] codetools_0.2-20 Rcpp_1.0.12 lattice_0.22-6
## [22] digest_0.6.35 R6_2.5.1 parallel_4.4.0
## [25] bslib_0.7.0 Matrix_1.7-0 tools_4.4.0
## [28] BiocGenerics_0.49.1 cachem_1.0.8
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.