Skip to contents

prepare_tsne_knn() strips self-neighbors, trims to the requested non-self width, and stores compact KNN matrices once. Pass the returned object to tsne_knn() for repeated seeds or backend comparisons without repeating KNN normalization/materialization.

Usage

prepare_tsne_knn(
  indices,
  distances = NULL,
  n_neighbors = NULL,
  perplexity = NULL
)

Arguments

indices

A list containing KNN indices and distances, or an integer KNN index matrix.

distances

Numeric KNN distance matrix matching indices. Leave NULL when indices is a KNN list.

n_neighbors

Optional number of non-self neighbor columns to use from the supplied KNN graph. This lets you compute a wide KNN once and reuse its first columns. If omitted, the compact affinity policy uses ceiling(perplexity) columns. An explicit value must equal that width.

perplexity

t-SNE perplexity. If NULL, the optimizer chooses a safe value from the supplied KNN width and sample size.

Value

A prepared t-SNE KNN object.

Examples

x <- scale(as.matrix(iris[, 1:4]))
d <- as.matrix(stats::dist(x))
diag(d) <- Inf
k <- 15L
idx <- t(apply(d, 1L, order))[, seq_len(k), drop = FALSE]
dst <- matrix(d[cbind(rep(seq_len(nrow(d)), each = k), as.vector(t(idx)))],
    nrow = nrow(d), byrow = TRUE
)
prep <- prepare_tsne_knn(idx, dst, perplexity = 5)
y1 <- tsne_knn(prep, seed = 1, early_exaggeration_iter = 5, n_iter = 10)
y2 <- tsne_knn(prep, seed = 2, early_exaggeration_iter = 5, n_iter = 10)