BiocNeighbors 1.20.2
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,] 6547 3075 3126 8829 4510 56 2615 8393 8383 3936
## [2,] 5950 3599 5532 9281 8316 2029 9775 4490 7330 3988
## [3,] 1932 9769 1444 8999 2856 5286 5961 5106 6635 6650
## [4,] 6081 7916 1957 4199 5276 1298 2274 8763 5629 5783
## [5,] 9703 4162 7683 875 3784 7535 6603 8205 4770 1415
## [6,] 8764 5835 1233 305 3693 9685 854 8887 1004 7876
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.8642916 0.9000104 0.9186685 0.9255799 0.9263756 0.9807586 0.9808160
## [2,] 0.8066234 0.8575543 0.9807713 0.9850861 1.0078605 1.0083168 1.0136914
## [3,] 0.9889905 0.9907167 1.0335222 1.0539592 1.0571829 1.0582639 1.0696846
## [4,] 0.7194142 0.9368981 0.9783971 0.9850416 0.9928981 0.9963897 0.9970733
## [5,] 0.9064599 0.9570495 0.9945895 1.0005104 1.0096896 1.0135483 1.0246952
## [6,] 0.9349082 0.9755720 0.9835460 1.0046476 1.0110976 1.0238387 1.0669779
## [,8] [,9] [,10]
## [1,] 0.9867056 0.9915124 0.9953573
## [2,] 1.0431133 1.0580635 1.0695312
## [3,] 1.1023363 1.1067879 1.1076678
## [4,] 1.0022547 1.0305605 1.0461364
## [5,] 1.0325762 1.0325805 1.0389836
## [6,] 1.0679529 1.0697774 1.0700541
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] 1932 9769 1444 8999 2856 5286 5961 5106 6635 6650
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.9889905 0.9907167 1.0335222 1.0539592 1.0571829 1.0582639 1.0696846
## [8] 1.1023363 1.1067879 1.1076678
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,] 8127 8930 6035 3832 1027
## [2,] 5724 7864 5901 6833 3204
## [3,] 2543 594 3269 1733 5629
## [4,] 6473 7138 9373 8794 8573
## [5,] 5578 9707 9774 8302 2488
## [6,] 8726 7551 725 5275 1558
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8635395 0.8802107 0.9237601 0.9326544 1.0148150
## [2,] 1.0175553 1.0366905 1.0536942 1.1099842 1.1526509
## [3,] 0.8216993 0.9759498 0.9892327 1.0278184 1.0398799
## [4,] 0.8810431 1.0375515 1.0471382 1.0666021 1.0750774
## [5,] 0.8210158 0.8351281 0.8714133 0.8854754 0.9549504
## [6,] 1.0318305 1.0803295 1.0999231 1.1042599 1.1047508
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] 2543 594 3269 1733 5629
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.8216993 0.9759498 0.9892327 1.0278184 1.0398799
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,] 1932 9769 1444 8999 2856
## [2,] 6081 7916 1957 4199 5276
## [3,] 9703 4162 7683 875 3784
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9889905 0.9907167 1.0335222 1.0539592 1.0571829
## [2,] 0.7194142 0.9368981 0.9783971 0.9850416 0.9928981
## [3,] 0.9064599 0.9570495 0.9945895 1.0005104 1.0096896
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.3.2 (2023-10-31)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Ventura 13.6.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.36.0 BiocNeighbors_1.20.2 knitr_1.45
## [4] BiocStyle_2.30.0
##
## loaded via a namespace (and not attached):
## [1] cli_3.6.1 rlang_1.1.1 xfun_0.41
## [4] jsonlite_1.8.7 S4Vectors_0.40.2 htmltools_0.5.7
## [7] stats4_4.3.2 sass_0.4.7 rmarkdown_2.25
## [10] grid_4.3.2 evaluate_0.23 jquerylib_0.1.4
## [13] fastmap_1.1.1 yaml_2.3.7 bookdown_0.36
## [16] BiocManager_1.30.22 compiler_4.3.2 codetools_0.2-19
## [19] Rcpp_1.0.11 lattice_0.22-5 digest_0.6.33
## [22] R6_2.5.1 parallel_4.3.2 bslib_0.5.1
## [25] Matrix_1.6-1.1 tools_4.3.2 BiocGenerics_0.48.1
## [28] cachem_1.0.8