BiocNeighbors 1.16.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,] 2012 8301 3129 5063 6673 8164 6678 6661 6303 207
## [2,] 6893 4086 1261 4834 5844 8247 5454 7903 1776 3173
## [3,] 3266 8560 6474 4549 6528 7257 9299 4898 7209 3105
## [4,] 6542 3522 1900 1642 3814 8377 1894 3216 6319 4634
## [5,] 9861 8793 9961 2947 4443 4288 5960 5127 4856 2287
## [6,] 1345 19 8576 9277 6318 3962 3454 6659 7360 2278
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.8645231 0.8760211 0.9346622 0.9361070 0.9532215 1.0194163 1.0255641
## [2,] 0.8104338 0.9384300 0.9799627 1.0181037 1.0183741 1.0208650 1.0228868
## [3,] 1.0515589 1.0891772 1.1056474 1.1144700 1.1217574 1.1222773 1.1231935
## [4,] 0.8952890 0.9323904 0.9692655 1.0294458 1.0385250 1.0420035 1.0537440
## [5,] 0.9242727 0.9956845 0.9975908 1.0597906 1.0763323 1.0938655 1.1061778
## [6,] 0.8623324 0.8764437 0.9165682 0.9437424 0.9694523 0.9700571 0.9957225
## [,8] [,9] [,10]
## [1,] 1.0378044 1.050532 1.052400
## [2,] 1.0340713 1.034213 1.057159
## [3,] 1.1713657 1.175250 1.176196
## [4,] 1.0618711 1.063934 1.067016
## [5,] 1.1325433 1.142662 1.143680
## [6,] 0.9995322 1.000443 1.009075
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] 3266 8560 6474 4549 6528 7257 9299 4898 7209 3105
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 1.051559 1.089177 1.105647 1.114470 1.121757 1.122277 1.123193 1.171366
## [9] 1.175250 1.176196
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,] 1461 440 1427 6588 4119
## [2,] 5186 4312 3184 9433 1536
## [3,] 1064 7008 5549 1839 5259
## [4,] 7748 9042 7452 8437 5829
## [5,] 965 196 3414 286 6720
## [6,] 9015 1736 6030 6984 4064
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8341055 0.9385532 0.9646674 0.9800647 0.9854151
## [2,] 1.0247365 1.0490958 1.0766292 1.1134266 1.1137131
## [3,] 0.9525084 0.9568452 0.9730784 0.9736631 1.0000199
## [4,] 0.8285880 0.9184197 0.9619846 0.9688385 0.9826963
## [5,] 0.9553952 0.9957002 1.0286125 1.0379920 1.0384748
## [6,] 0.8018889 0.9943344 1.0131962 1.0319141 1.0346542
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] 1064 7008 5549 1839 5259
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.9525084 0.9568452 0.9730784 0.9736631 1.0000199
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,] 3266 8560 6474 4549 6528
## [2,] 6542 3522 1900 1642 3814
## [3,] 9861 8793 9961 2947 4443
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0515589 1.0891772 1.1056474 1.114470 1.121757
## [2,] 0.8952890 0.9323904 0.9692655 1.029446 1.038525
## [3,] 0.9242727 0.9956845 0.9975908 1.059791 1.076332
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.1 (2022-06-23 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.32.0 BiocNeighbors_1.16.0 knitr_1.40
## [4] BiocStyle_2.26.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.9 magrittr_2.0.3 BiocGenerics_0.44.0
## [4] lattice_0.20-45 R6_2.5.1 rlang_1.0.6
## [7] fastmap_1.1.0 stringr_1.4.1 tools_4.2.1
## [10] parallel_4.2.1 grid_4.2.1 xfun_0.34
## [13] cli_3.4.1 jquerylib_0.1.4 htmltools_0.5.3
## [16] yaml_2.3.6 digest_0.6.30 bookdown_0.29
## [19] Matrix_1.5-1 BiocManager_1.30.19 S4Vectors_0.36.0
## [22] sass_0.4.2 codetools_0.2-18 cachem_1.0.6
## [25] evaluate_0.17 rmarkdown_2.17 stringi_1.7.8
## [28] compiler_4.2.1 bslib_0.4.0 stats4_4.2.1
## [31] jsonlite_1.8.3
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.