BiocNeighbors 1.4.1
The BiocNeighbors package implements a few algorithms for exact nearest neighbor searching:
Both methods involve a component of randomness during index construction, though the k-nearest neighbors result is fully deterministic1 Except in the presence of ties, see ?findKNN
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,] 5780 2400 1779 4331 7961 4190 6901 7654 3303 1148
## [2,] 8341 3607 9329 5306 7821 5704 2425 887 839 5322
## [3,] 1426 9879 9693 9950 5450 3607 9588 9247 3939 4238
## [4,] 7385 6061 8639 307 1967 1614 5670 3612 8624 7258
## [5,] 2847 8757 6124 965 8248 5423 6521 4720 8748 38
## [6,] 4426 9701 9824 9943 2247 5329 2941 8141 1305 2470
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.9641071 0.9698867 0.9821833 0.9833184 0.9839802 0.9957514 1.0138334
## [2,] 0.9468863 0.9715150 0.9829346 1.0262488 1.0303923 1.0331301 1.0387390
## [3,] 0.6952047 0.8365728 0.8719797 0.9394383 0.9423852 0.9429820 0.9443994
## [4,] 1.0528042 1.0632266 1.0704953 1.0734085 1.0852417 1.0909726 1.1148824
## [5,] 0.8764859 0.9155654 0.9884708 1.0181945 1.0237347 1.0265508 1.0302054
## [6,] 0.8501055 0.8716474 0.9017471 0.9515497 0.9560144 0.9703327 0.9743750
## [,8] [,9] [,10]
## [1,] 1.0177632 1.0348117 1.0377992
## [2,] 1.0422043 1.0442326 1.0468834
## [3,] 0.9571395 0.9698406 0.9703301
## [4,] 1.1292918 1.1293928 1.1514815
## [5,] 1.0555274 1.0617686 1.0701864
## [6,] 0.9853485 0.9853705 0.9966202
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] 1426 9879 9693 9950 5450 3607 9588 9247 3939 4238
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.6952047 0.8365728 0.8719797 0.9394383 0.9423852 0.9429820 0.9443994
## [8] 0.9571395 0.9698406 0.9703301
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,] 5 584 8632 5867 8204
## [2,] 4475 7706 120 1518 2866
## [3,] 6597 5807 1342 6550 8879
## [4,] 3246 3175 914 1778 4872
## [5,] 9531 5952 3194 746 5721
## [6,] 7517 6204 3966 9357 4084
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0028418 1.1399802 1.1544500 1.1624324 1.1700077
## [2,] 0.8821578 0.8864041 0.8919386 0.9185352 0.9237819
## [3,] 0.7622327 1.0048679 1.0132730 1.0174456 1.0446646
## [4,] 0.8746881 0.9076532 0.9273415 0.9460961 0.9621586
## [5,] 0.9154788 0.9559962 0.9780533 1.0348812 1.0362163
## [6,] 0.9768172 1.0261409 1.0386026 1.0719880 1.0845750
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] 6597 5807 1342 6550 8879
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.7622327 1.0048679 1.0132730 1.0174456 1.0446646
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,] 1426 9879 9693 9950 5450
## [2,] 7385 6061 8639 307 1967
## [3,] 2847 8757 6124 965 8248
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.6952047 0.8365728 0.8719797 0.9394383 0.9423852
## [2,] 1.0528042 1.0632266 1.0704953 1.0734085 1.0852417
## [3,] 0.8764859 0.9155654 0.9884708 1.0181945 1.0237347
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 3.6.1 (2019-07-05)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.10-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.10-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.20.0 BiocNeighbors_1.4.1 knitr_1.25
## [4] BiocStyle_2.14.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.2 bookdown_0.14 lattice_0.20-38
## [4] digest_0.6.22 grid_3.6.1 stats4_3.6.1
## [7] magrittr_1.5 evaluate_0.14 rlang_0.4.1
## [10] stringi_1.4.3 S4Vectors_0.24.0 Matrix_1.2-17
## [13] rmarkdown_1.16 tools_3.6.1 stringr_1.4.0
## [16] parallel_3.6.1 xfun_0.10 yaml_2.2.0
## [19] compiler_3.6.1 BiocGenerics_0.32.0 BiocManager_1.30.9
## [22] htmltools_0.4.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.