BiocNeighbors 1.2.0
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,] 448 2627 1231 8438 4061 4975 1579 1361 604 3987
## [2,] 7175 8400 2402 3607 3896 1960 3639 8093 7422 3650
## [3,] 4331 5857 4317 3943 1700 6106 3121 2275 2570 6008
## [4,] 16 6684 1420 8435 7095 5072 7591 7774 91 9796
## [5,] 1689 7229 6087 9705 2005 1631 7749 7936 3207 1884
## [6,] 9204 927 9150 6082 6049 1387 8998 8774 5476 594
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.8453864 0.9845556 0.9958283 0.9987408 1.0193715 1.0363405 1.0557041
## [2,] 0.9878499 0.9936281 1.0081013 1.0154102 1.0196419 1.0238649 1.0242124
## [3,] 0.8661217 0.8865915 0.9120462 0.9668598 0.9695144 0.9791747 0.9809772
## [4,] 0.9447711 0.9782320 0.9908288 1.0288252 1.0380146 1.0383513 1.0462680
## [5,] 0.9546012 1.0278247 1.0389017 1.0738137 1.0761576 1.0795276 1.0879467
## [6,] 0.9203438 0.9577621 0.9885243 1.0343908 1.0391960 1.0404812 1.0429814
## [,8] [,9] [,10]
## [1,] 1.063379 1.072954 1.076492
## [2,] 1.030410 1.046778 1.050966
## [3,] 1.012097 1.014960 1.034394
## [4,] 1.067507 1.074392 1.075207
## [5,] 1.133786 1.142623 1.143561
## [6,] 1.061910 1.064141 1.068496
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] 4331 5857 4317 3943 1700 6106 3121 2275 2570 6008
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.8661217 0.8865915 0.9120462 0.9668598 0.9695144 0.9791747 0.9809772
## [8] 1.0120971 1.0149601 1.0343943
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,] 886 3074 1910 1209 1581
## [2,] 2877 3665 6785 9930 5617
## [3,] 8495 2328 1093 3135 1322
## [4,] 9170 8626 5376 8486 5494
## [5,] 8059 713 7889 8145 7313
## [6,] 7309 5981 9142 681 5515
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8966448 0.9132506 0.9198101 0.9382225 0.9481371
## [2,] 0.9651522 1.0382222 1.0599402 1.0878937 1.0984991
## [3,] 0.9205865 0.9772678 0.9785464 0.9849385 0.9946565
## [4,] 0.8516627 0.9124987 0.9773794 0.9796759 0.9957898
## [5,] 0.7683655 0.8681020 0.8904500 0.9252921 0.9831553
## [6,] 0.8516892 0.8987283 0.9889174 1.0024226 1.0292419
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] 8495 2328 1093 3135 1322
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.9205865 0.9772678 0.9785464 0.9849385 0.9946565
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,] 4331 5857 4317 3943 1700
## [2,] 16 6684 1420 8435 7095
## [3,] 1689 7229 6087 9705 2005
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8661217 0.8865915 0.9120462 0.9668598 0.9695144
## [2,] 0.9447711 0.9782320 0.9908288 1.0288252 1.0380146
## [3,] 0.9546012 1.0278247 1.0389017 1.0738137 1.0761576
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)
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.0 (2019-04-26)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: OS X El Capitan 10.11.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/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.18.0 BiocNeighbors_1.2.0 knitr_1.22
## [4] BiocStyle_2.12.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.1 bookdown_0.9 digest_0.6.18
## [4] stats4_3.6.0 magrittr_1.5 evaluate_0.13
## [7] stringi_1.4.3 S4Vectors_0.22.0 rmarkdown_1.12
## [10] tools_3.6.0 stringr_1.4.0 parallel_3.6.0
## [13] xfun_0.6 yaml_2.2.0 compiler_3.6.0
## [16] BiocGenerics_0.30.0 BiocManager_1.30.4 htmltools_0.3.6
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.