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,] 4759 3133 2244 6065 1525 6315 8195 6449 7767 15
## [2,] 4091 5970 5862 1165 6218 3092 9929 1327 5008 8743
## [3,] 7232 4781 9970 9585 4569 6351 8730 6499 6284 2549
## [4,] 4620 109 5513 5378 3000 3028 5172 2624 1167 5557
## [5,] 679 4260 2349 3054 9562 876 1415 4233 8097 5370
## [6,] 9563 5190 9337 7529 7660 2723 6640 4988 3812 3096
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.8335935 0.8846361 0.9270802 0.9540741 0.9613196 0.9775717 0.9791256
## [2,] 0.7964790 0.9067136 0.9088183 0.9360884 0.9618310 1.0823737 1.0904094
## [3,] 0.8514119 0.8659194 0.9142708 0.9478152 1.0066917 1.0106994 1.0207878
## [4,] 0.9543948 0.9686283 0.9838006 0.9964099 1.0214757 1.0280015 1.0390167
## [5,] 1.0403258 1.0769289 1.0847990 1.1116855 1.1131839 1.1190399 1.1367923
## [6,] 0.8983738 0.9067831 0.9154891 0.9494151 0.9627305 0.9701354 1.0189227
## [,8] [,9] [,10]
## [1,] 0.9822307 0.9831564 0.9862723
## [2,] 1.0976625 1.1002049 1.1060225
## [3,] 1.0222027 1.0224671 1.0244051
## [4,] 1.0414891 1.0651461 1.0764236
## [5,] 1.1373877 1.1425400 1.1496971
## [6,] 1.0208295 1.0269854 1.0473276
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] 7232 4781 9970 9585 4569 6351 8730 6499 6284 2549
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.8514119 0.8659194 0.9142708 0.9478152 1.0066917 1.0106994 1.0207878
## [8] 1.0222027 1.0224671 1.0244051
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,] 1164 615 3650 8840 8016
## [2,] 7051 9670 3243 1844 6900
## [3,] 824 6492 1264 3279 7346
## [4,] 9374 4742 897 3138 1946
## [5,] 3303 9412 9707 6265 6111
## [6,] 1456 3908 8231 921 1332
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7440540 0.8559326 0.8821282 0.9197609 0.9198630
## [2,] 0.9504074 0.9667192 0.9760502 0.9850783 0.9879696
## [3,] 0.7615352 0.9442274 1.0031161 1.0090124 1.0127704
## [4,] 0.9633988 1.0387266 1.0644667 1.0769795 1.0867682
## [5,] 1.0336694 1.0593433 1.0612159 1.0625136 1.0703213
## [6,] 0.9163154 0.9461201 0.9533016 0.9810251 0.9859250
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] 824 6492 1264 3279 7346
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.7615352 0.9442274 1.0031161 1.0090124 1.0127704
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,] 7232 4781 9970 9585 4569
## [2,] 4620 109 5513 5378 3000
## [3,] 679 4260 2349 3054 9562
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8514119 0.8659194 0.9142708 0.9478152 1.006692
## [2,] 0.9543948 0.9686283 0.9838006 0.9964099 1.021476
## [3,] 1.0403258 1.0769289 1.0847990 1.1116855 1.113184
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 (2021-05-18)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server x64 (build 17763)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## 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.