findKNN {BiocNeighbors}R Documentation

Find k-nearest neighbors

Description

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

Usage

findKNN(..., BNINDEX=NULL, BNPARAM=NULL)

Arguments

...

Further arguments to pass to specific methods, including:

  • X, a numeric data matrix where rows are points and columns are dimensions.

  • k, an integer scalar for the number of nearest neighbors. This is the only strictly mandatory parameter.

  • subset, a vector specifying the subset of points in X to search.

  • get.index, a logical scalar indicating whether to return row indices of the neighbors.

  • get.distance, a logical scalar indicating whether to return distances to neighbors.

  • BPPARAM, a BiocParallelParam class for parallelization.

BNINDEX

A BiocNeighborIndex object, or NULL.

BNPARAM

A BiocNeighborParam object, or NULL if BININDEX is supplied.

Details

The class of BNINDEX and BNPARAM will determine whether dispatch is performed to findKmknn or findAnnoy. 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.

Value

A list is returned containing:

If subset is not NULL, each row of the above matrices refers to a point in the subset, in the same order as supplied in subset.

Author(s)

Aaron Lun

See Also

findKmknn and findAnnoy for specific methods.

Examples

Y <- matrix(rnorm(100000), ncol=20)
str(k.out <- findKNN(Y, k=10))
str(a.out <- findKNN(Y, k=10, BNPARAM=AnnoyParam()))

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

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

[Package BiocNeighbors version 1.0.0 Index]