snapatac2.pp.knn#
- snapatac2.pp.knn(adata, n_neighbors=50, use_dims=None, use_rep='X_spectral', method='kdtree', inplace=True, random_state=0)[source]#
Build a Euclidean k-nearest-neighbor graph for observations.
Use this function after dimensionality reduction to construct the graph used by downstream clustering, embedding, or graph-based analysis. When
adatais an AnnData-like object, the input matrix is read fromadata.obsm[use_rep]. Whenadatais a NumPy array, the array itself is used and the result is always returned.Anti-Patterns#
Do NOT pass a raw count matrix unless Euclidean distances on counts are the intended analysis; use a reduced representation such as
X_spectral.Do NOT expect
random_stateto makemethod="hora"deterministic; the HNSW backend currently ignores this value.
- type adata:
AnnData|AnnDataSet|ndarray- param adata:
AnnData-like object with
use_repin.obsm, AnnDataSet-like object, or a NumPy array of shapen_obsxn_features.- type n_neighbors:
- param n_neighbors:
Number of nearest neighbors to store for each observation.
- type use_dims:
- param use_dims:
Dimensions of
use_repor the input array to use. If an integer, use the firstuse_dimscolumns. If a list, use those column indices.- type use_rep:
- param use_rep:
Key in
.obsmcontaining the representation to search.- type method:
Literal['kdtree','hora','pynndescent']- param method:
Neighbor-search backend. Use
"kdtree"for exact search,"hora"for approximate HNSW search, or"pynndescent"for approximate NNDescent.- type inplace:
- param inplace:
If
Trueandadatais AnnData-like, store the graph in.obsp["distances"]. Ignored for NumPy input.- type random_state:
- param random_state:
Random seed used only by
method="pynndescent".- returns:
Sparse distance matrix of shape
n_obsxn_obswheninplace=Falseor whenadatais a NumPy array. ReturnsNonewheninplace=Trueand stores the matrix in.obsp["distances"].- rtype:
Examples
>>> import numpy as np >>> import snapatac2 as snap >>> X = np.array([[0.0, 0.0], [0.1, 0.0], [2.0, 2.0], [2.1, 2.0]]) >>> graph = snap.pp.knn(X, n_neighbors=2, method="kdtree") >>> graph.shape (4, 4)