knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(rnndescent)

If your dataset is sufficiently "small" (either in terms of number of points or number of dimensions) you can use brute force search to find the nearest neighbors: just calculate the distances between all pairs of points.

This has the advantages of simplicity and accuracy. But it is of course very slow. That said as it can be easily parallelized, you can get a surprisingly long way with it, especially if you only need the k-nearest neighbors and won't be running a neighbor search multiple times.

Let's calculate the exact 15 nearest neighbors for the iris dataset:

iris_nbrs <- brute_force_knn(iris, k = 15)
lapply(iris_nbrs, function(x) {
  head(x[, 1:5], 3)
})

And here's an example of querying the odd items in the iris data against the even items (i.e. the indices in iris_nbrs will refer to the odd items):

iris_even <- iris[seq_len(nrow(iris)) %% 2 == 0, ]
iris_odd <- iris[seq_len(nrow(iris)) %% 2 == 1, ]
iris_query_nbrs <-
  brute_force_knn_query(
    query = iris_even,
    reference = iris_odd,
    k = 15
  )
lapply(iris_query_nbrs, function(x) {
  head(x[, 1:5], 3)
})


jlmelville/rnndescent documentation built on April 19, 2024, 8:26 p.m.