BiocNeighbors 1.8.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,] 8708 7021 5565 9037 992 4396 1707 652 5224 7976
## [2,] 5373 2712 4 8653 1484 1250 5545 5915 8115 4370
## [3,] 7087 2697 1565 8380 6880 19 5057 715 1189 6387
## [4,] 5915 4820 1484 2 503 117 2497 8303 7074 6943
## [5,] 8820 806 8591 7137 8397 8585 4527 6733 3527 4062
## [6,] 4259 1416 7324 6614 1801 272 1676 6098 9456 4536
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.8079059 0.9309740 0.9384599 0.9553266 0.9580868 0.9590141 0.976230
## [2,] 0.8975691 0.9666076 0.9929248 1.0044424 1.0087993 1.0138086 1.015813
## [3,] 0.8869864 0.9483988 0.9586840 0.9676536 0.9800829 0.9826444 1.018355
## [4,] 0.9598088 0.9763788 0.9817408 0.9929248 1.0278447 1.0583429 1.062305
## [5,] 0.8612950 0.9214730 0.9469593 0.9646069 0.9782430 0.9971411 1.009585
## [6,] 0.7957266 0.8182894 0.9591528 0.9821667 0.9906658 1.0120220 1.018320
## [,8] [,9] [,10]
## [1,] 1.012826 1.025948 1.028755
## [2,] 1.045712 1.048363 1.059129
## [3,] 1.023567 1.045185 1.054341
## [4,] 1.093749 1.121147 1.123251
## [5,] 1.024542 1.039176 1.040992
## [6,] 1.026984 1.041685 1.043859
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] 7087 2697 1565 8380 6880 19 5057 715 1189 6387
… with the following distances to those neighbors:
fout$distance[3,]
## [1] 0.8869864 0.9483988 0.9586840 0.9676536 0.9800829 0.9826444 1.0183545
## [8] 1.0235666 1.0451848 1.0543407
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,] 4506 3825 4744 7273 5782
## [2,] 4032 546 5013 374 8486
## [3,] 8273 2828 7627 5435 2201
## [4,] 5370 1597 7100 4944 6578
## [5,] 5606 9826 3654 4710 6586
## [6,] 6420 9462 7554 4846 2482
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0165089 1.0700092 1.0918931 1.1158737 1.1198008
## [2,] 0.9457919 0.9483921 0.9527775 0.9716764 1.0070311
## [3,] 0.8027708 0.8511580 0.8758288 0.9620654 0.9699954
## [4,] 0.8757220 0.9075918 0.9090505 0.9363837 0.9406441
## [5,] 0.8563240 0.9028457 0.9057712 0.9393270 0.9538742
## [6,] 0.9487179 0.9576210 0.9660384 0.9676200 0.9776511
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] 8273 2828 7627 5435 2201
… with the following distances to those neighbors:
qout$distance[3,]
## [1] 0.8027708 0.8511580 0.8758288 0.9620654 0.9699954
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,] 7087 2697 1565 8380 6880
## [2,] 5915 4820 1484 2 503
## [3,] 8820 806 8591 7137 8397
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8869864 0.9483988 0.9586840 0.9676536 0.9800829
## [2,] 0.9598088 0.9763788 0.9817408 0.9929248 1.0278447
## [3,] 0.8612950 0.9214730 0.9469593 0.9646069 0.9782430
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.0.3 (2020-10-10)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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.24.1 BiocNeighbors_1.8.2 knitr_1.30
## [4] BiocStyle_2.18.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.5 bookdown_0.21 lattice_0.20-41
## [4] digest_0.6.27 grid_4.0.3 stats4_4.0.3
## [7] magrittr_2.0.1 evaluate_0.14 rlang_0.4.9
## [10] stringi_1.5.3 S4Vectors_0.28.0 Matrix_1.2-18
## [13] rmarkdown_2.5 tools_4.0.3 stringr_1.4.0
## [16] parallel_4.0.3 xfun_0.19 yaml_2.2.1
## [19] compiler_4.0.3 BiocGenerics_0.36.0 BiocManager_1.30.10
## [22] htmltools_0.5.0
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.