BiocNeighbors 1.23.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,] 8200 788 604 3415 6404 4018 5147 4022 2035 5217
## [2,] 8241 8216 6436 9848 2026 9611 6765 2575 8070 3287
## [3,] 142 9896 6390 481 2472 2546 9289 8570 6772 2169
## [4,] 5143 2955 3789 5481 4473 7373 4913 471 383 8326
## [5,] 7498 8165 9426 6612 2426 508 7876 3522 840 8460
## [6,] 2977 451 5734 2029 7631 4282 3928 6954 4418 7703
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.7759338 0.9064194 0.9121747 0.9156263 0.9407649 0.9418500 0.9431673
## [2,] 0.7183277 0.8421181 0.8601347 0.8907141 0.9398991 0.9511869 0.9584130
## [3,] 0.8984018 0.9267198 0.9555765 1.0113035 1.0199516 1.0380032 1.0392230
## [4,] 1.0229927 1.0365472 1.0459641 1.0477714 1.0755179 1.0973888 1.0977385
## [5,] 0.9341836 0.9556050 0.9637877 0.9647740 0.9662666 0.9713417 0.9729271
## [6,] 0.9550695 0.9793211 1.0045489 1.0520686 1.0556554 1.0677389 1.0707360
## [,8] [,9] [,10]
## [1,] 0.9453139 0.9539768 0.9742706
## [2,] 0.9598594 0.9674096 0.9868433
## [3,] 1.0473610 1.0486216 1.0496327
## [4,] 1.0980666 1.0986944 1.0999729
## [5,] 0.9746487 0.9804176 0.9815910
## [6,] 1.0716110 1.0785403 1.0855952
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] 142 9896 6390 481 2472 2546 9289 8570 6772 2169
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.8984018 0.9267198 0.9555765 1.0113035 1.0199516 1.0380032 1.0392230
## [8] 1.0473610 1.0486216 1.0496327
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,] 9196 4689 8237 266 8978
## [2,] 1149 6989 1558 5249 9703
## [3,] 9681 8447 6069 5391 3029
## [4,] 6411 5675 1060 6008 2499
## [5,] 8634 8843 2981 756 8720
## [6,] 4370 2074 5175 8005 8042
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8660893 0.8750677 0.8769564 0.8863404 0.9049830
## [2,] 0.8904155 0.9015547 0.9387411 0.9555081 0.9706083
## [3,] 0.8168083 0.8458351 0.8605695 0.9101873 0.9195681
## [4,] 0.9082176 1.0952530 1.1078366 1.1313790 1.1353993
## [5,] 0.8956104 0.8992204 0.9449372 0.9909724 1.0084413
## [6,] 0.8721217 0.9158617 0.9384848 0.9399135 0.9480216
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] 9681 8447 6069 5391 3029
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.8168083 0.8458351 0.8605695 0.9101873 0.9195681
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,] 142 9896 6390 481 2472
## [2,] 5143 2955 3789 5481 4473
## [3,] 7498 8165 9426 6612 2426
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8984018 0.9267198 0.9555765 1.011304 1.0199516
## [2,] 1.0229927 1.0365472 1.0459641 1.047771 1.0755179
## [3,] 0.9341836 0.9556050 0.9637877 0.964774 0.9662666
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 RC (2024-04-16 r86468)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.20-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.39.0 BiocNeighbors_1.23.0 knitr_1.46
## [4] BiocStyle_2.33.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.43.0 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.51.0 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.