(data, k_neighbors, metric, progress_callback=None)
return graph
Convert data matrix to a graph using a nearest neighbors approach with
    the Jaccard similarity as the edge weights.

    Parameters
    ----------
    data : np.ndarray
    k_neighbors : int
    metric : str
        A distance metric supported by sklearn.
    progress_callback : Callable[[float], None]

    Returns
    -------
    nx.Graph
Convert data matrix to a graph using a nearest neighbors approach with
    the Jaccard similarity as the edge weights.
Convert data matrix to a graph using a nearest neighbors approach with the Jaccard similarity as the edge weights .
def matrix_to_knn_graph(data, k_neighbors, metric, progress_callback=None):
    """Convert data matrix to a graph using a nearest neighbors approach with
    the Jaccard similarity as the edge weights.

    Parameters
    ----------
    data : np.ndarray
    k_neighbors : int
    metric : str
        A distance metric supported by sklearn.
    progress_callback : Callable[[float], None]

    Returns
    -------
    nx.Graph

    """
    # We do k + 1 because each point is closest to itself, which is not useful
    if metric == "cosine":
        # Cosine distance on row-normalized data has the same ranking as
        # Euclidean distance, so we use the latter, which is more efficient
        # because it uses ball trees. We do not need actual distances. If we
        # would, the N * k distances can be recomputed later.
        data = data / np.linalg.norm(data, axis=1)[:, None]
        metric = "euclidean"
    knn = NearestNeighbors(n_neighbors=k_neighbors, metric=metric).fit(data)
    nearest_neighbors = knn.kneighbors(data, return_distance=False)
    # Convert to list of sets so jaccard can be computed efficiently
    nearest_neighbors = list(map(set, nearest_neighbors))
    num_nodes = len(nearest_neighbors)

    # Create an empty graph and add all the data ids as nodes for easy mapping
    graph = nx.Graph()
    graph.add_nodes_from(range(len(data)))

    for idx, node in enumerate(graph.nodes):
        if progress_callback:
            progress_callback(idx / num_nodes)

        for neighbor in nearest_neighbors[node]:
            graph.add_edge(
                node,
                neighbor,
                weight=jaccard(
                    nearest_neighbors[node], nearest_neighbors[neighbor]),
            )

    return graph
def matrix_to_knn_graph ( data , k_neighbors , metric , progress_callback = None ) : # We do k + 1 because each point is closest to itself, which is not useful if metric == "cosine" : # Cosine distance on row-normalized data has the same ranking as # Euclidean distance, so we use the latter, which is more efficient # because it uses ball trees. We do not need actual distances. If we # would, the N * k distances can be recomputed later. data = data / np . linalg . norm ( data , axis = 1 ) [ : , None ] metric = "euclidean" knn = NearestNeighbors ( n_neighbors = k_neighbors , metric = metric ) . fit ( data ) nearest_neighbors = knn . kneighbors ( data , return_distance = False ) # Convert to list of sets so jaccard can be computed efficiently nearest_neighbors = list ( map ( set , nearest_neighbors ) ) num_nodes = len ( nearest_neighbors ) # Create an empty graph and add all the data ids as nodes for easy mapping graph = nx . Graph ( ) graph . add_nodes_from ( range ( len ( data ) ) ) for idx , node in enumerate ( graph . nodes ) : if progress_callback : progress_callback ( idx / num_nodes ) for neighbor in nearest_neighbors [ node ] : graph . add_edge ( node , neighbor , weight = jaccard ( nearest_neighbors [ node ] , nearest_neighbors [ neighbor ] ) , ) return graph