BiocNeighbors 1.11.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,] 4903 3250 5479 3118 2870 1097 6286 21 2557 556
## [2,] 9741 9949 9204 8295 7120 3911 2284 6499 1498 5298
## [3,] 2182 304 8130 5983 1324 5775 8157 414 7414 2457
## [4,] 9863 2612 4356 1309 1262 7354 2681 1790 9710 3596
## [5,] 6181 6516 5449 6511 2165 1534 6498 1680 8942 4143
## [6,] 4589 5919 8107 1609 6097 6422 9837 9790 9651 5641
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.8266472 1.0681442 1.0828731 1.0830781 1.0846848 1.0877639 1.0966363
## [2,] 0.8506258 0.9281563 0.9501957 0.9726065 1.0091748 1.0200575 1.0376724
## [3,] 0.9968944 1.0493829 1.1200320 1.1275936 1.1404919 1.1554324 1.1638620
## [4,] 0.8892484 0.9720772 0.9750788 1.0171176 1.0546121 1.0552655 1.0821961
## [5,] 0.9048722 0.9144032 0.9396395 0.9426881 0.9463277 0.9655449 0.9742865
## [6,] 0.7577713 0.9026077 0.9202607 0.9207380 0.9217692 0.9256759 0.9313021
## [,8] [,9] [,10]
## [1,] 1.1028133 1.1096928 1.1241598
## [2,] 1.0449442 1.0661957 1.0664761
## [3,] 1.1642499 1.1850993 1.2058203
## [4,] 1.0939058 1.0973944 1.0996668
## [5,] 0.9773802 0.9865815 0.9913859
## [6,] 0.9412170 0.9534582 0.9623420
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] 2182 304 8130 5983 1324 5775 8157 414 7414 2457
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.9968944 1.0493829 1.1200320 1.1275936 1.1404919 1.1554324 1.1638620
## [8] 1.1642499 1.1850993 1.2058203
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,] 9818 8208 3466 8381 2709
## [2,] 5857 203 2084 1152 6391
## [3,] 95 3699 2910 8772 5621
## [4,] 2500 7793 7980 3058 1636
## [5,] 6923 6183 5679 9424 7317
## [6,] 7334 5623 4719 7832 932
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8042721 0.8806759 0.9051048 0.9134669 0.9186544
## [2,] 0.9822555 1.0601822 1.0642626 1.0693824 1.0979472
## [3,] 0.8513230 0.9066060 0.9093766 0.9238101 0.9577076
## [4,] 0.8041765 0.9084255 0.9641614 0.9746977 0.9980528
## [5,] 0.9642384 0.9707410 0.9828442 0.9934895 1.0291007
## [6,] 0.7834045 0.8110311 0.8499109 0.9916108 1.0199240
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] 95 3699 2910 8772 5621
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.8513230 0.9066060 0.9093766 0.9238101 0.9577076
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,] 2182 304 8130 5983 1324
## [2,] 9863 2612 4356 1309 1262
## [3,] 6181 6516 5449 6511 2165
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9968944 1.0493829 1.1200320 1.1275936 1.1404919
## [2,] 0.8892484 0.9720772 0.9750788 1.0171176 1.0546121
## [3,] 0.9048722 0.9144032 0.9396395 0.9426881 0.9463277
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.1.0 RC (2021-05-16 r80304)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.27.0 BiocNeighbors_1.11.0 knitr_1.33
## [4] BiocStyle_2.21.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.6 magrittr_2.0.1 BiocGenerics_0.39.0
## [4] lattice_0.20-44 R6_2.5.0 rlang_0.4.11
## [7] stringr_1.4.0 tools_4.1.0 parallel_4.1.0
## [10] grid_4.1.0 xfun_0.23 jquerylib_0.1.4
## [13] htmltools_0.5.1.1 yaml_2.2.1 digest_0.6.27
## [16] bookdown_0.22 Matrix_1.3-3 BiocManager_1.30.15
## [19] S4Vectors_0.31.0 sass_0.4.0 evaluate_0.14
## [22] rmarkdown_2.8 stringi_1.6.2 compiler_4.1.0
## [25] bslib_0.2.5.1 stats4_4.1.0 jsonlite_1.7.2
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.