queryKNN: Query k-nearest neighbors

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Find the k-nearest neighbors in one data set for each point in another query data set, using exact or approximate algorithms.

Usage

1
queryKNN(X, query, k, ..., BNINDEX, BNPARAM)

Arguments

X

A numeric data matrix where rows are points and columns are dimensions.

query

A numeric query matrix where rows are points and columns are dimensions.

k

An integer scalar for the number of nearest neighbors.

...

Further arguments to pass to specific methods. This is guaranteed to include subset, get.index, get.distance, last, transposed, warn.ties, raw.index and BPPARAM. See ?"queryKNN-methods" for more details.

BNINDEX

A BiocNeighborIndex object containing precomputed index information. This can be missing if BNPARAM is supplied, see Details.

BNPARAM

A BiocNeighborParam object specifying the algorithm to use. This can be missing if BNINDEX is supplied, see Details.

Details

The class of BNINDEX and BNPARAM will determine dispatch to specific methods. Only one of these arguments needs to be defined to resolve dispatch. However, if both are defined, they cannot specify different algorithms.

If BNINDEX is supplied, X does not need to be specified. In fact, any value of X will be ignored as all necessary information for the search is already present in BNINDEX. Similarly, any parameters in BNPARAM will be ignored.

If both BNINDEX and BNPARAM are missing, the function will default to the KMKNN algorithm by setting BNPARAM=KmknnParam().

Value

A list is returned containing index, an integer matrix of neighbor identities; and distance, a numeric matrix of distances to those neighbors. See ?"queryKNN-methods" for more details.

Author(s)

Aaron Lun

See Also

queryExhaustive, queryKmknn, queryVptree, queryAnnoy and queryHnsw for specific methods.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Y <- matrix(rnorm(100000), ncol=20)
Z <- matrix(rnorm(10000), ncol=20)
str(k.out <- queryKNN(Y, Z, k=10))
str(a.out <- queryKNN(Y, Z, k=10, BNPARAM=AnnoyParam()))

e.dex <- buildExhaustive(Y)
str(k.out2 <- queryKNN(Y,Z,  k=10, BNINDEX=e.dex))
str(k.out3 <- queryKNN(Y,Z,  k=10, BNINDEX=e.dex, BNPARAM=ExhaustiveParam()))

k.dex <- buildKmknn(Y)
str(k.out2 <- queryKNN(Y,Z,  k=10, BNINDEX=k.dex))
str(k.out3 <- queryKNN(Y,Z,  k=10, BNINDEX=k.dex, BNPARAM=KmknnParam()))

a.dex <- buildAnnoy(Y)
str(a.out2 <- queryKNN(Y,Z,  k=10, BNINDEX=a.dex))
str(a.out3 <- queryKNN(Y,Z,  k=10, BNINDEX=a.dex, BNPARAM=AnnoyParam()))

Example output

List of 2
 $ index   : int [1:500, 1:10] 4380 1360 2767 2006 1291 1473 4299 3452 4541 549 ...
 $ distance: num [1:500, 1:10] 4.2 4.2 3.19 4.04 3.61 ...
List of 2
 $ index   : int [1:500, 1:10] 4380 1360 2767 2006 1291 1473 4299 3452 4541 3670 ...
 $ distance: num [1:500, 1:10] 4.2 4.2 3.19 4.04 3.61 ...
List of 2
 $ index   : int [1:500, 1:10] 4380 1360 2767 2006 1291 1473 4299 3452 4541 549 ...
 $ distance: num [1:500, 1:10] 4.2 4.2 3.19 4.04 3.61 ...
List of 2
 $ index   : int [1:500, 1:10] 4380 1360 2767 2006 1291 1473 4299 3452 4541 549 ...
 $ distance: num [1:500, 1:10] 4.2 4.2 3.19 4.04 3.61 ...
List of 2
 $ index   : int [1:500, 1:10] 4380 1360 2767 2006 1291 1473 4299 3452 4541 549 ...
 $ distance: num [1:500, 1:10] 4.2 4.2 3.19 4.04 3.61 ...
List of 2
 $ index   : int [1:500, 1:10] 4380 1360 2767 2006 1291 1473 4299 3452 4541 549 ...
 $ distance: num [1:500, 1:10] 4.2 4.2 3.19 4.04 3.61 ...
List of 2
 $ index   : int [1:500, 1:10] 4380 1360 2767 2006 1291 1473 4299 3452 4541 3670 ...
 $ distance: num [1:500, 1:10] 4.2 4.2 3.19 4.04 3.61 ...
List of 2
 $ index   : int [1:500, 1:10] 4380 1360 2767 2006 1291 1473 4299 3452 4541 3670 ...
 $ distance: num [1:500, 1:10] 4.2 4.2 3.19 4.04 3.61 ...

BiocNeighbors documentation built on Dec. 9, 2020, 2:01 a.m.