find_knn: Find approximate k-nearest neighbors

View source: R/find_knn.R

find_knnR Documentation

Find approximate k-nearest neighbors

Description

Implements RcppAnnoy's approximate nearest neighbor search (much faster than precise neighbors). Random search is made reproducible using set.seed(seed). Hint: If you pass find_knn's output directly to uwot::umap via the nn_method argument, make sure to set umap's argument n_sgd_threads to <=1 to ensure the UMAP embedding is reproducible.

Usage

find_knn(featureMatrix, k = 50, n_trees = 50, seed = 42)

Arguments

featureMatrix

Numeric matrix with features in rows, cells in columns. Rows could be normalized genes or latent dimensions such as principal components.

k

Number of neighbors to find.

n_trees

RccpAnnoy builds a forest of n_trees trees. More trees gives higher precision when querying. Default: 50.

seed

Random seed for neighbor search, default: 42.

Value

List with two slots:

  • idx A NxK matrix (N cells, K neighbors) containing the integer indexes of the approximate nearest neighbors in featureMatrix. Each cell is considered to be its own nearest neighbor, next to K-1 other neighbors.

  • dist A NxK matrix containing the distances of the nearest neighbors.

Inspired by uwot::umap's return value when setting ret_nn=TRUE.

Examples

  # Imagine we have 30 cells and 100 features:
 fmat <- matrix(rnorm(3000), ncol=30)
 nn <- find_knn(fmat,k=15)
 # nn$idx has 30 rows and 15 columns.

cellpypes documentation built on May 29, 2024, 1:24 a.m.