queryKNN | R Documentation |
Query a reference dataset for the k-nearest neighbors of each point in a query dataset.
queryKNN(
X,
query,
k,
get.index = TRUE,
get.distance = TRUE,
num.threads = 1,
subset = NULL,
transposed = FALSE,
...,
BNPARAM = NULL
)
X |
The reference dataset to be queried.
This should be a numeric matrix where rows correspond to reference points and columns correspond to variables (i.e., dimensions).
Alternatively, a prebuilt BiocNeighborIndex object from |
query |
A numeric matrix of query points, containing the same number of columns as |
k |
A positive integer scalar specifying the number of nearest neighbors to retrieve. Alternatively, an integer vector of length equal to the number of points in All |
get.index |
A logical scalar indicating whether the indices of the nearest neighbors should be recorded.
Setting this to Alternatively, if |
get.distance |
A logical scalar indicating whether distances to the nearest neighbors should be recorded.
Setting this to Alternatively, if |
num.threads |
Integer scalar specifying the number of threads to use for the search. |
subset |
An integer, logical or character vector indicating the rows of |
transposed |
A logical scalar indicating whether |
... |
Further arguments to pass to |
BNPARAM |
A BiocNeighborParam object specifying how the index should be constructed.
If |
If multiple queries are to be performed to the same X
, it may be beneficial to build the index from X
with buildIndex
.
The resulting pointer object can be supplied as X
to multiple queryKNN
calls, avoiding the need to repeat index construction in each call.
List containing index
(if get.index
is not FALSE
) and distance
(if get.distance
is not FALSE
).
If get.index=TRUE
or "normal"
and k
is an integer scalar,
index
is an integer matrix with k
columns where each row corresponds to a point (denoted here as i
) in query
.
The i
-th row contains the indices of points in X
that are the nearest neighbors to point i
, sorted by increasing distance from i
.
If get.index=FALSE
or "transposed"
and k
is an integer scalar,
index
is as described above but transposed, i.e., the i
-th column contains the indices of neighboring points in X
.
If get.distance=TRUE
or "normal"
and k
is an integer scalar,
distance
is a numeric matrix of the same dimensions as index
.
The i
-th row contains the distances of neighboring points in X
to the point i
, sorted in increasing order.
If get.distance=FALSE
or "transposed"
and k
is an integer scalar,
distance
is as described above but transposed, i.e., the i
-th column contains the distances to neighboring points in X
.
If get.index
is not FALSE
and k
is an integer vector,
index
is a list of integer vectors where each vector corresponds to a point (denoted here as i
) in X
.
The i
-th vector has length k[i]
and contains the indices of points in X
that are the nearest neighbors to point i
, sorted by increasing distance from i
.
If get.distance
is not FALSE
and k
is an integer vector,
distance
is a list of numeric vectors of the same lengths as those in index
.
The i
-th vector contains the distances of neighboring points in X
to the point i
, sorted in increasing order.
Aaron Lun
buildIndex
, to build an index ahead of time.
queryDistance
, to obtain the distance from each query point to its k-th nearest neighbor.
Y <- matrix(rnorm(100000), ncol=20)
Z <- matrix(rnorm(20000), ncol=20)
out <- queryKNN(Y, query=Z, k=5)
head(out$index)
head(out$distance)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.