| knn-utilities | R Documentation |
Truncate or reorder the neighbors in a graph, or adjust self neighbors in a
graph of a dataset against itself. Each function returns a list containing
only idx and dist matrices.
truncate_knn(graph, k)
sort_knn(graph, by = NULL)
remove_self_knn(graph, k = max(0L, ncol(graph$idx) - 1L))
self_first_knn(graph, k = ncol(graph$idx), self_dist = NULL)
graph |
A nearest neighbor graph with |
k |
Number of columns to retain. For |
by |
Optional numeric matrix of scores with the same dimensions as the graph matrices. Defaults to the stored distances. |
self_dist |
Finite numeric scalar used when a row has no self entry.
The default |
truncate_knn() keeps the first k columns. sort_knn() orders each row
by ascending distance, or by the supplied score matrix. Ties retain their
original order. Missing scores follow other scores, and missing neighbors
(idx = 0) always come last. Distances remain paired with their indices.
remove_self_knn() keeps the first k present, nonself neighbors in each
row (by default, one fewer than the stored width, with a minimum of zero).
self_first_knn() puts self first, retaining the distance of the first
existing self entry, and follows it with the first k - 1 present, nonself
neighbors. If self is absent, supply self_dist to insert it. Both functions
identify self by the row number, skip missing entries and other self entries,
and error if a row has too few nonself neighbors. They are for self graphs.
Do not use them on query results, where row numbers identify queries rather
than reference observations.
A list containing the resulting idx and dist matrices.
graph <- brute_force_knn(iris[1:8, 1:4], k = 6)
truncate_knn(graph, k = 3)
remove_self_knn(graph, k = 5)
sort_knn(graph)
self_first_knn(graph)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.