k_nearest_neighbors#
- tangles.util.graph.similarity.k_nearest_neighbors(X: ndarray, metric: str = 'precomputed', k: int = 1, ties_all: bool = False) csr_matrix #
Creates a k-nearest neighbor graph (or something like a k-nearest neighbor graph) from distances.
Parameters#
- Xnp.ndarray
If metric is ‘precomputed’ X is a condensed distance matrix (see
scipy.spatial.distance.squareform()
). Otherwise X is the data andscipy.spatial.distance.pdist()
is called taking X as its argument.- metricstr
Either ‘precomputed’ or a metric from
scipy.spatial.distance.pdist()
. If not ‘precomputed’, metric is forwarded to thescipy.spatial.distance.pdist()
call.- kint
Number of neighbors.
- ties_allbool
If True, every node gets a connection to all other nodes that have a distance smaller than the k-nearest neighbor, i.e. if there are multiple k-nearest neighbors, we connect them all, resulting in a graph that might have degrees greater than k (so it is not really a k-nn graph, but something similar where ties are broken in a special way). If False, only one of possibly multiple k-nearest neighbors is connected.
Returns#
- sparse.csr_matrix
Sparse adjacency matrix of a k-nn graph (note that this graph is directed).