BiocNeighbors 1.21.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,] 1261 2632 707 6400 3945 2977 5097 9425 419 4549
## [2,] 9571 9221 4411 5892 1186 4462 4275 8077 7285 7781
## [3,] 6602 4143 2168 3440 3436 3948 9670 5734 3119 6855
## [4,] 5937 9744 9433 9742 6949 9592 5947 645 7808 9151
## [5,] 9717 8354 6861 5232 9442 2331 5152 3528 283 4309
## [6,] 2059 5532 897 3453 6871 3799 3092 6816 5742 9264
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.9910804 1.0385674 1.0826053 1.1431342 1.169077 1.1751503 1.1807024
## [2,] 1.0952815 1.1153050 1.1383911 1.1741918 1.179060 1.1881235 1.1900637
## [3,] 0.8553872 0.9761926 1.0312931 1.0515296 1.054020 1.0646007 1.0811471
## [4,] 0.9495458 0.9652792 0.9765828 1.0229964 1.025160 1.0358518 1.0369666
## [5,] 0.7836070 0.9077030 0.9726108 0.9855530 1.031361 1.0395437 1.0396414
## [6,] 0.9041969 0.9335647 0.9606161 0.9621945 0.963020 0.9772444 0.9947932
## [,8] [,9] [,10]
## [1,] 1.203107 1.210119 1.217164
## [2,] 1.210651 1.264282 1.270305
## [3,] 1.087187 1.117589 1.127392
## [4,] 1.067499 1.068023 1.069090
## [5,] 1.056179 1.074279 1.079579
## [6,] 1.000085 1.000214 1.002006
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] 6602 4143 2168 3440 3436 3948 9670 5734 3119 6855
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.8553872 0.9761926 1.0312931 1.0515296 1.0540201 1.0646007 1.0811471
## [8] 1.0871866 1.1175887 1.1273923
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,] 4866 9045 3740 4466 2788
## [2,] 6932 5581 8774 8316 8112
## [3,] 3739 3557 1291 8113 7482
## [4,] 42 8483 1939 345 7344
## [5,] 7327 8735 9997 6925 4042
## [6,] 8863 2946 5213 5985 747
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0022956 1.0461911 1.0494864 1.0775180 1.0842262
## [2,] 0.8713883 0.9457378 0.9738382 0.9828396 1.0039811
## [3,] 0.9843743 1.0134681 1.0145005 1.0342147 1.0530964
## [4,] 0.8530630 0.8796247 0.8823402 0.9505656 0.9552803
## [5,] 0.8781920 0.9068694 0.9176490 0.9399983 0.9648328
## [6,] 0.9195800 0.9722520 0.9931594 0.9940243 0.9976789
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] 3739 3557 1291 8113 7482
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.9843743 1.0134681 1.0145005 1.0342147 1.0530964
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,] 6602 4143 2168 3440 3436
## [2,] 5937 9744 9433 9742 6949
## [3,] 9717 8354 6861 5232 9442
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8553872 0.9761926 1.0312931 1.051530 1.054020
## [2,] 0.9495458 0.9652792 0.9765828 1.022996 1.025160
## [3,] 0.7836070 0.9077030 0.9726108 0.985553 1.031361
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.4.0 Patched (2024-04-24 r86482)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.6.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.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.37.1 BiocNeighbors_1.21.2 knitr_1.46
## [4] BiocStyle_2.31.0
##
## loaded via a namespace (and not attached):
## [1] cli_3.6.2 rlang_1.1.3 xfun_0.43
## [4] jsonlite_1.8.8 S4Vectors_0.41.7 htmltools_0.5.8.1
## [7] stats4_4.4.0 sass_0.4.9 rmarkdown_2.26
## [10] grid_4.4.0 evaluate_0.23 jquerylib_0.1.4
## [13] fastmap_1.1.1 yaml_2.3.8 lifecycle_1.0.4
## [16] bookdown_0.39 BiocManager_1.30.22 compiler_4.4.0
## [19] codetools_0.2-20 Rcpp_1.0.12 lattice_0.22-6
## [22] digest_0.6.35 R6_2.5.1 parallel_4.4.0
## [25] bslib_0.7.0 Matrix_1.7-0 tools_4.4.0
## [28] BiocGenerics_0.49.1 cachem_1.0.8