BiocNeighbors 1.22.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,] 8384 7866 4411 604 136 308 3227 2884 9008 1192
## [2,] 6838 8390 7593 8709 5601 5637 2499 6396 8914 183
## [3,] 1543 4175 2458 6311 9234 5223 1063 1934 7895 8650
## [4,] 5346 4603 5560 6338 7160 75 2844 3410 1734 697
## [5,] 409 8058 9275 3193 9009 3881 9032 8540 458 8570
## [6,] 7375 4286 6408 1253 6895 3306 9114 6062 8360 1343
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.7844151 0.8729679 0.8933728 0.9055999 0.9065885 0.9131854 0.9275019
## [2,] 0.8712118 0.9036691 0.9711109 1.0247568 1.0353801 1.0394481 1.0419114
## [3,] 1.0136756 1.0148672 1.0191917 1.0277016 1.0456724 1.0658040 1.0728107
## [4,] 0.8142410 0.8268386 0.8961637 0.9003218 0.9445161 0.9653873 0.9757273
## [5,] 0.8516895 1.0075383 1.0296012 1.0359010 1.0367167 1.0515337 1.0595795
## [6,] 0.8846022 0.9229060 0.9713848 0.9995220 1.0025699 1.0197325 1.0244118
## [,8] [,9] [,10]
## [1,] 0.9355670 0.9473748 0.9483618
## [2,] 1.0726376 1.0980111 1.1053231
## [3,] 1.0732871 1.0771780 1.0886589
## [4,] 0.9907385 0.9941287 1.0063451
## [5,] 1.0716348 1.0839496 1.0850306
## [6,] 1.0397412 1.0446744 1.0546361
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] 1543 4175 2458 6311 9234 5223 1063 1934 7895 8650
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 1.013676 1.014867 1.019192 1.027702 1.045672 1.065804 1.072811 1.073287
## [9] 1.077178 1.088659
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,] 4797 7831 7372 198 3445
## [2,] 7394 8328 2054 6615 7632
## [3,] 4518 1209 7593 6367 7426
## [4,] 3089 4222 7667 3900 1238
## [5,] 1616 6594 1738 7894 493
## [6,] 7279 4462 3803 9389 3655
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9793095 0.9871160 1.0201950 1.0312260 1.0314296
## [2,] 0.7946513 1.0306398 1.0649509 1.0651269 1.0926190
## [3,] 0.8205489 0.9212602 0.9332941 0.9491972 0.9492272
## [4,] 0.8948363 0.9375497 0.9578410 0.9658331 1.0072924
## [5,] 0.9203762 1.0136006 1.0382907 1.0388502 1.0438780
## [6,] 0.8117749 0.8723576 0.9135050 0.9557226 0.9766687
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] 4518 1209 7593 6367 7426
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.8205489 0.9212602 0.9332941 0.9491972 0.9492272
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,] 1543 4175 2458 6311 9234
## [2,] 5346 4603 5560 6338 7160
## [3,] 409 8058 9275 3193 9009
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0136756 1.0148672 1.0191917 1.0277016 1.0456724
## [2,] 0.8142410 0.8268386 0.8961637 0.9003218 0.9445161
## [3,] 0.8516895 1.0075383 1.0296012 1.0359010 1.0367167
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 alpha (2024-03-27 r86216)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.6.5
##
## 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] en_US.UTF-8/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.38.0 BiocNeighbors_1.22.0 knitr_1.45
## [4] BiocStyle_2.32.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.42.0 htmltools_0.5.8
## [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.38 BiocManager_1.30.22 compiler_4.4.0
## [19] codetools_0.2-19 Rcpp_1.0.12 lattice_0.22-6
## [22] digest_0.6.35 R6_2.5.1 parallel_4.4.0
## [25] bslib_0.6.2 Matrix_1.7-0 tools_4.4.0
## [28] BiocGenerics_0.50.0 cachem_1.0.8