BiocNeighbors 1.11.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,] 2668 6704 1004 4392 145 5351 8781 3095 7873 4672
## [2,] 6945 2816 8248 6700 5199 7924 20 9171 1879 9035
## [3,] 6541 449 7246 7216 1477 1807 4142 5502 2097 680
## [4,] 1052 9875 7227 2598 2052 9881 9143 9414 454 9159
## [5,] 7073 1757 1685 3091 1475 6395 752 8715 9144 8045
## [6,] 9647 9819 6192 5476 8778 8365 6490 5473 3664 3735
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1.0277173 1.0914721 1.1297769 1.1494751 1.1848833 1.1939250 1.2009841
## [2,] 0.9508659 0.9812366 0.9980456 1.0036180 1.0206922 1.0216785 1.0412465
## [3,] 1.0083753 1.0238427 1.0345228 1.0382965 1.0621566 1.0687579 1.0782509
## [4,] 0.9010146 0.9522892 0.9743294 0.9757185 0.9789672 0.9946637 1.0043460
## [5,] 0.8198585 0.8538470 0.9475119 0.9580788 0.9680987 0.9699691 0.9795823
## [6,] 0.8363876 0.9012231 0.9255593 0.9809969 1.0059936 1.0127409 1.0161912
## [,8] [,9] [,10]
## [1,] 1.2018832 1.2241961 1.2318584
## [2,] 1.0534640 1.0701963 1.0712315
## [3,] 1.0789961 1.0892437 1.0931458
## [4,] 1.0121718 1.0211644 1.0289971
## [5,] 0.9842019 0.9848962 0.9871926
## [6,] 1.0321459 1.0363159 1.0389104
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] 6541 449 7246 7216 1477 1807 4142 5502 2097 680
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 1.008375 1.023843 1.034523 1.038297 1.062157 1.068758 1.078251 1.078996
## [9] 1.089244 1.093146
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,] 5273 239 7734 6517 1861
## [2,] 6988 6151 4585 2714 419
## [3,] 7243 7122 7073 6257 8239
## [4,] 7497 5321 1381 6499 8323
## [5,] 1960 6337 4158 8979 5594
## [6,] 1713 2747 5012 3426 3979
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9150168 0.9255843 0.9803777 1.030903 1.0376688
## [2,] 0.7239696 0.9715286 0.9954888 1.001410 1.0020294
## [3,] 1.0295143 1.1062606 1.1102640 1.152907 1.1650865
## [4,] 1.0045123 1.0758423 1.1128335 1.116444 1.1411963
## [5,] 0.9910959 1.0253116 1.0303511 1.044971 1.0692681
## [6,] 0.6720374 0.8047867 0.8921586 0.924986 0.9268413
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] 7243 7122 7073 6257 8239
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 1.029514 1.106261 1.110264 1.152907 1.165086
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,] 6541 449 7246 7216 1477
## [2,] 1052 9875 7227 2598 2052
## [3,] 7073 1757 1685 3091 1475
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0083753 1.0238427 1.0345228 1.0382965 1.0621566
## [2,] 0.9010146 0.9522892 0.9743294 0.9757185 0.9789672
## [3,] 0.8198585 0.8538470 0.9475119 0.9580788 0.9680987
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.0 beta (2021-05-03 r80259)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.14-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.14-bioc/R/lib/libRlapack.so
##
## 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
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.27.0 BiocNeighbors_1.11.0 knitr_1.33
## [4] BiocStyle_2.21.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.6 magrittr_2.0.1 BiocGenerics_0.39.0
## [4] lattice_0.20-44 R6_2.5.0 rlang_0.4.11
## [7] stringr_1.4.0 tools_4.1.0 parallel_4.1.0
## [10] grid_4.1.0 xfun_0.23 jquerylib_0.1.4
## [13] htmltools_0.5.1.1 yaml_2.2.1 digest_0.6.27
## [16] bookdown_0.22 Matrix_1.3-3 BiocManager_1.30.15
## [19] S4Vectors_0.31.0 sass_0.4.0 evaluate_0.14
## [22] rmarkdown_2.8 stringi_1.6.2 compiler_4.1.0
## [25] bslib_0.2.5.1 stats4_4.1.0 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.