BiocNeighbors 1.14.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,] 1012 5204 6622 228 7687 6668 3961 3537 2976 2555
## [2,] 6877 5739 413 7887 5823 4407 8670 2443 2929 7678
## [3,] 6534 3567 4401 1479 5232 2824 9330 8392 833 3424
## [4,] 9328 9776 9601 8498 3463 56 3499 6184 4882 2094
## [5,] 3647 921 976 9431 6285 3018 6834 1945 3466 5390
## [6,] 313 1280 9807 2592 9974 8443 9182 9421 8203 6685
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.7702514 0.8285045 0.9305885 0.9372155 0.9506589 0.9570240 0.9596741
## [2,] 0.9156375 0.9398827 0.9512830 0.9776621 0.9866050 0.9955466 1.0156480
## [3,] 0.8460563 0.8698524 1.0248896 1.0298173 1.0317935 1.0322435 1.0391951
## [4,] 0.9200945 0.9210948 0.9248840 0.9633795 0.9654476 0.9704577 0.9758003
## [5,] 1.0343984 1.0467662 1.0770364 1.1065768 1.1071398 1.1087040 1.1117910
## [6,] 0.9591996 1.0215987 1.0226420 1.0287238 1.0305231 1.0482483 1.0676963
## [,8] [,9] [,10]
## [1,] 0.9723196 0.9727652 0.982152
## [2,] 1.0274505 1.0372914 1.050326
## [3,] 1.0425242 1.0521931 1.053321
## [4,] 0.9919063 1.0084052 1.012156
## [5,] 1.1214517 1.1257479 1.130316
## [6,] 1.0711629 1.0934674 1.101910
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] 6534 3567 4401 1479 5232 2824 9330 8392 833 3424
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.8460563 0.8698524 1.0248896 1.0298173 1.0317935 1.0322435 1.0391951
## [8] 1.0425242 1.0521931 1.0533212
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,] 787 4934 7708 9993 937
## [2,] 1704 2347 2287 3959 5336
## [3,] 8031 2790 2413 2689 7391
## [4,] 6237 5886 2803 4874 9073
## [5,] 8405 5720 833 3033 1798
## [6,] 6719 8724 4636 2092 1751
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9255459 0.9276751 0.9451619 0.9520291 0.9561595
## [2,] 0.8767975 0.8957958 0.9372746 0.9989255 1.0724145
## [3,] 0.8605236 0.8801248 0.8803738 0.8868539 0.8912795
## [4,] 1.0466980 1.0513802 1.0850684 1.1140605 1.1328829
## [5,] 0.7264516 0.9854557 1.0779111 1.0996217 1.1014603
## [6,] 0.8443905 0.8720023 0.8886987 0.9930363 0.9930646
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] 8031 2790 2413 2689 7391
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.8605236 0.8801248 0.8803738 0.8868539 0.8912795
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,] 6534 3567 4401 1479 5232
## [2,] 9328 9776 9601 8498 3463
## [3,] 3647 921 976 9431 6285
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8460563 0.8698524 1.024890 1.0298173 1.0317935
## [2,] 0.9200945 0.9210948 0.924884 0.9633795 0.9654476
## [3,] 1.0343984 1.0467662 1.077036 1.1065768 1.1071398
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.2.0 RC (2022-04-19 r82224 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server 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
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.30.0 BiocNeighbors_1.14.0 knitr_1.38
## [4] BiocStyle_2.24.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.8.3 magrittr_2.0.3 BiocGenerics_0.42.0
## [4] lattice_0.20-45 R6_2.5.1 rlang_1.0.2
## [7] fastmap_1.1.0 stringr_1.4.0 tools_4.2.0
## [10] parallel_4.2.0 grid_4.2.0 xfun_0.30
## [13] cli_3.3.0 jquerylib_0.1.4 htmltools_0.5.2
## [16] yaml_2.3.5 digest_0.6.29 bookdown_0.26
## [19] Matrix_1.4-1 BiocManager_1.30.17 S4Vectors_0.34.0
## [22] sass_0.4.1 evaluate_0.15 rmarkdown_2.14
## [25] stringi_1.7.6 compiler_4.2.0 bslib_0.3.1
## [28] stats4_4.2.0 jsonlite_1.8.0
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.