brute_force_knn_query | R Documentation |
Returns the exact nearest neighbors of query data to the reference data. A brute force search is carried out: all possible pairs of reference and query points are compared, and the nearest neighbors are returned.
brute_force_knn_query(
query,
reference,
k,
metric = "euclidean",
use_alt_metric = TRUE,
n_threads = 0,
verbose = FALSE,
obs = "R"
)
query |
Matrix of |
reference |
Matrix of |
k |
Number of nearest neighbors to return. |
metric |
Type of distance calculation to use. One of:
For non-sparse data, the following variants are available with preprocessing: this trades memory for a potential speed up during the distance calculation. Some minor numerical differences should be expected compared to the non-preprocessed versions:
For non-sparse binary data passed as a
|
use_alt_metric |
If |
n_threads |
Number of threads to use. |
verbose |
If |
obs |
set to |
This is accurate but scales poorly with dataset size, so use with caution with larger datasets. Having the exact neighbors as a ground truth to compare with approximate results is useful for benchmarking and determining parameter settings of the approximate methods.
the nearest neighbor graph as a list containing:
idx
an n by k matrix containing the nearest neighbor indices in
reference
.
dist
an n by k matrix containing the nearest neighbor distances to the
items in reference
.
# 100 reference iris items
iris_ref <- iris[iris$Species %in% c("setosa", "versicolor"), ]
# 50 query items
iris_query <- iris[iris$Species == "versicolor", ]
# For each item in iris_query find the 4 nearest neighbors in iris_ref
# If you pass a data frame, non-numeric columns are removed
# set verbose = TRUE to get details on the progress being made
iris_query_nn <- brute_force_knn_query(iris_query,
reference = iris_ref,
k = 4, metric = "euclidean", verbose = TRUE
)
# Manhattan (l1) distance
iris_query_nn <- brute_force_knn_query(iris_query,
reference = iris_ref,
k = 4, metric = "manhattan"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.