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,] 4775 6528 8173 170 5619 2198 5297 1936 8728 4425
## [2,] 341 5086 7898 730 811 3128 2694 178 6761 968
## [3,] 3410 9568 6485 2440 3434 203 6637 4016 6120 6455
## [4,] 5459 664 323 7881 3832 8279 9467 7763 3890 6264
## [5,] 9872 3905 2301 8908 9383 6180 6875 9976 7899 5461
## [6,] 2161 3652 2623 5487 8728 5987 4712 4471 8720 4003
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.9312608 0.9415885 0.9926737 1.0003344 1.0141275 1.0354932 1.0467049
## [2,] 0.7959710 0.9859921 0.9862059 0.9971433 1.0035566 1.0078116 1.0201296
## [3,] 0.9599306 0.9697063 0.9763220 0.9929391 1.0175576 1.0195634 1.0224867
## [4,] 0.8274187 0.8301665 0.9073785 0.9476564 0.9608506 0.9622307 0.9716198
## [5,] 0.9870743 0.9895582 0.9919432 0.9954354 1.0019267 1.0094022 1.0383270
## [6,] 0.8267817 0.8623414 0.9579904 0.9786076 0.9947813 1.0104989 1.0483870
## [,8] [,9] [,10]
## [1,] 1.0484185 1.0546042 1.073766
## [2,] 1.0226921 1.0285783 1.048286
## [3,] 1.0282193 1.0353541 1.036565
## [4,] 0.9794823 0.9935105 1.002375
## [5,] 1.0585023 1.0608303 1.066522
## [6,] 1.0538350 1.0611264 1.062101
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] 3410 9568 6485 2440 3434 203 6637 4016 6120 6455
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.9599306 0.9697063 0.9763220 0.9929391 1.0175576 1.0195634 1.0224867
## [8] 1.0282193 1.0353541 1.0365654
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,] 6661 408 510 9254 1569
## [2,] 233 5656 3887 3894 2992
## [3,] 9724 2465 3537 8918 2296
## [4,] 1560 424 4107 3514 7971
## [5,] 5205 6511 5781 9738 7668
## [6,] 7424 5076 4255 4381 3591
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9205877 1.0195510 1.0272116 1.0289750 1.0320516
## [2,] 0.7925081 0.8915312 0.9038004 0.9064119 0.9115216
## [3,] 0.9369008 1.0212951 1.0462161 1.0578836 1.0685148
## [4,] 0.7809978 0.8955024 0.9058172 0.9757421 0.9814785
## [5,] 0.8341215 0.8969954 0.9843150 1.0008446 1.0054414
## [6,] 0.9419893 0.9613209 0.9621820 0.9908284 0.9998522
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] 9724 2465 3537 8918 2296
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.9369008 1.0212951 1.0462161 1.0578836 1.0685148
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,] 3410 9568 6485 2440 3434
## [2,] 5459 664 323 7881 3832
## [3,] 9872 3905 2301 8908 9383
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9599306 0.9697063 0.9763220 0.9929391 1.0175576
## [2,] 0.8274187 0.8301665 0.9073785 0.9476564 0.9608506
## [3,] 0.9870743 0.9895582 0.9919432 0.9954354 1.0019267
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 ucrt)
## Platform: x86_64-w64-mingw32/x64
## 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.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.