BiocNeighbors 1.20.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,] 1236 1653 1075 4569 5470 9434 6883 209 4864 7983
## [2,] 8446 8632 7772 7198 330 1038 5053 5948 806 2158
## [3,] 3988 1055 4600 525 1550 3803 7354 8626 1431 6319
## [4,] 3121 4500 4328 4089 7464 2174 2186 1942 4122 1902
## [5,] 6731 4945 4236 3322 2099 4062 918 5578 9232 9879
## [6,] 9706 3192 6126 7755 9266 8814 6323 5083 1313 9053
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.8135366 0.9694217 1.0088395 1.0162544 1.0338453 1.0415711 1.0510825
## [2,] 0.8683627 1.0169743 1.0254785 1.0283984 1.0591126 1.0621904 1.0685105
## [3,] 0.7666488 0.9860280 1.0373977 1.0413672 1.0634921 1.0664803 1.0754251
## [4,] 0.9044270 1.0272129 1.0652842 1.0664035 1.0887058 1.1105853 1.1113703
## [5,] 0.8259157 0.8633699 0.9135972 0.9147899 0.9319336 0.9440326 0.9648193
## [6,] 0.8637840 0.9073375 0.9136007 0.9460880 0.9561181 0.9617090 0.9687266
## [,8] [,9] [,10]
## [1,] 1.0566770 1.0602086 1.0613762
## [2,] 1.0750615 1.0776401 1.1006276
## [3,] 1.0855745 1.0875338 1.0883333
## [4,] 1.1148853 1.1204494 1.1265577
## [5,] 0.9721001 0.9775189 1.0142058
## [6,] 0.9770323 0.9789646 0.9844721
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] 3988 1055 4600 525 1550 3803 7354 8626 1431 6319
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.7666488 0.9860280 1.0373977 1.0413672 1.0634921 1.0664803 1.0754251
## [8] 1.0855745 1.0875338 1.0883333
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,] 3677 3151 5696 8661 9076
## [2,] 6347 5542 1780 5031 1872
## [3,] 6862 7875 1096 8627 8833
## [4,] 7383 553 3665 4285 8439
## [5,] 4518 7223 1915 3459 1006
## [6,] 7602 8793 5394 8629 2478
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9069448 0.9792873 1.0127049 1.0359784 1.0474939
## [2,] 0.9788922 1.0278919 1.0438174 1.0476592 1.0497407
## [3,] 0.9460433 0.9585220 0.9758957 1.0037015 1.0874484
## [4,] 0.8494316 0.9175868 0.9296744 1.0155764 1.0192590
## [5,] 0.7456975 0.8419112 0.8694424 0.9124401 0.9346771
## [6,] 0.8438429 0.8639105 0.8998855 0.9686530 0.9709077
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] 6862 7875 1096 8627 8833
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.9460433 0.9585220 0.9758957 1.0037015 1.0874484
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,] 3988 1055 4600 525 1550
## [2,] 3121 4500 4328 4089 7464
## [3,] 6731 4945 4236 3322 2099
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7666488 0.9860280 1.0373977 1.0413672 1.0634921
## [2,] 0.9044270 1.0272129 1.0652842 1.0664035 1.0887058
## [3,] 0.8259157 0.8633699 0.9135972 0.9147899 0.9319336
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.3.2 (2023-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server 2022 x64 (build 20348)
##
## Matrix products: default
##
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.36.0 BiocNeighbors_1.20.2 knitr_1.45
## [4] BiocStyle_2.30.0
##
## loaded via a namespace (and not attached):
## [1] cli_3.6.2 rlang_1.1.2 xfun_0.41
## [4] jsonlite_1.8.8 S4Vectors_0.40.2 htmltools_0.5.7
## [7] stats4_4.3.2 sass_0.4.8 rmarkdown_2.25
## [10] grid_4.3.2 evaluate_0.23 jquerylib_0.1.4
## [13] fastmap_1.1.1 yaml_2.3.8 lifecycle_1.0.4
## [16] bookdown_0.37 BiocManager_1.30.22 compiler_4.3.2
## [19] codetools_0.2-19 Rcpp_1.0.11 lattice_0.22-5
## [22] digest_0.6.33 R6_2.5.1 parallel_4.3.2
## [25] bslib_0.6.1 Matrix_1.6-4 tools_4.3.2
## [28] BiocGenerics_0.48.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.