BiocNeighbors 1.18.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,] 582 2270 5791 4578 4901 9741 7710 477 6597 3535
## [2,] 8800 4952 485 1960 8355 8099 5515 5320 9266 5679
## [3,] 6353 8551 6667 4065 462 2033 2745 1006 6286 1900
## [4,] 7850 986 1052 8624 4208 3356 3164 2549 2778 324
## [5,] 9195 8876 9813 3084 4300 8142 5270 1141 409 8714
## [6,] 3199 5195 9216 1893 980 215 3091 143 3251 3935
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.9423195 0.9756641 0.9847178 0.9890979 1.0068238 1.0354410 1.0443751
## [2,] 0.9453144 0.9763334 0.9870803 1.0133113 1.0246422 1.0434734 1.0651721
## [3,] 0.9408063 0.9451763 0.9553391 1.0003538 1.0094183 1.0242468 1.0336839
## [4,] 0.7062376 0.8767202 0.9725641 0.9800004 0.9813318 0.9998995 1.0108560
## [5,] 0.8377788 0.9897278 0.9927016 1.0092706 1.0482807 1.0512856 1.0592706
## [6,] 0.7635893 0.8494161 0.8634719 0.8818101 0.8855169 0.8868454 0.9139534
## [,8] [,9] [,10]
## [1,] 1.0531019 1.0543471 1.0625680
## [2,] 1.0833491 1.1203142 1.1508091
## [3,] 1.0383780 1.0422855 1.0474117
## [4,] 1.0117072 1.0154406 1.0279073
## [5,] 1.0885647 1.0928781 1.0931754
## [6,] 0.9172289 0.9185597 0.9225301
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] 6353 8551 6667 4065 462 2033 2745 1006 6286 1900
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.9408063 0.9451763 0.9553391 1.0003538 1.0094183 1.0242468 1.0336839
## [8] 1.0383780 1.0422855 1.0474117
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,] 8911 5163 9137 4948 9174
## [2,] 962 6595 2444 6711 9914
## [3,] 7997 1737 9000 4305 9817
## [4,] 2510 4558 9610 3921 9177
## [5,] 4928 8454 9119 664 5269
## [6,] 3361 4253 9504 1396 1521
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0060029 1.0124993 1.0310411 1.0451824 1.0519400
## [2,] 0.9421791 0.9782141 1.0145446 1.0348064 1.0394971
## [3,] 0.8476367 0.8884421 0.9029476 0.9357987 0.9447380
## [4,] 0.8704513 0.8827004 0.9134809 0.9233919 0.9235727
## [5,] 0.8306225 0.9136258 0.9741991 1.0154403 1.0366326
## [6,] 0.9759394 1.0517612 1.0536610 1.0751126 1.1184950
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] 7997 1737 9000 4305 9817
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.8476367 0.8884421 0.9029476 0.9357987 0.9447380
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,] 6353 8551 6667 4065 462
## [2,] 7850 986 1052 8624 4208
## [3,] 9195 8876 9813 3084 4300
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9408063 0.9451763 0.9553391 1.0003538 1.0094183
## [2,] 0.7062376 0.8767202 0.9725641 0.9800004 0.9813318
## [3,] 0.8377788 0.9897278 0.9927016 1.0092706 1.0482807
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.0 RC (2023-04-13 r84257)
## Platform: x86_64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.6.4
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## 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.34.1 BiocNeighbors_1.18.0 knitr_1.42
## [4] BiocStyle_2.28.0
##
## loaded via a namespace (and not attached):
## [1] cli_3.6.1 rlang_1.1.1 xfun_0.39
## [4] jsonlite_1.8.4 S4Vectors_0.38.1 htmltools_0.5.5
## [7] stats4_4.3.0 sass_0.4.6 rmarkdown_2.21
## [10] grid_4.3.0 evaluate_0.21 jquerylib_0.1.4
## [13] fastmap_1.1.1 yaml_2.3.7 bookdown_0.34
## [16] BiocManager_1.30.20 compiler_4.3.0 codetools_0.2-19
## [19] Rcpp_1.0.10 lattice_0.21-8 digest_0.6.31
## [22] R6_2.5.1 parallel_4.3.0 bslib_0.4.2
## [25] Matrix_1.5-4 tools_4.3.0 BiocGenerics_0.46.0
## [28] 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.