(data, graph, dim, random_state, metric="euclidean", metric_kwds={})
Given a graph compute the spectral embedding of the graph. This is
    simply the eigenvectors of the laplacian of the graph. Here we use the
    normalized laplacian.

    Parameters
    ----------
    data: array of shape (n_samples, n_features)
        The source data

    graph: sparse matrix
        The (weighted) adjacency matrix of the graph as a sparse matrix.

    dim: int
        The dimension of the space into which to embed.

    random_state: numpy RandomState or equivalent
        A state capable being used as a numpy random state.

    Returns
    -------
    embedding: array of shape (n_vertices, dim)
        The spectral embedding of the graph.
Given a graph compute the spectral embedding of the graph. This is
    simply the eigenvectors of the laplacian of the graph. Here we use the
    normalized laplacian.
Given a graph compute the spectral embedding of the graph . This is simply the eigenvectors of the laplacian of the graph . Here we use the normalized laplacian .
def spectral_layout(data, graph, dim, random_state, metric="euclidean", metric_kwds={}):
    """Given a graph compute the spectral embedding of the graph. This is
    simply the eigenvectors of the laplacian of the graph. Here we use the
    normalized laplacian.

    Parameters
    ----------
    data: array of shape (n_samples, n_features)
        The source data

    graph: sparse matrix
        The (weighted) adjacency matrix of the graph as a sparse matrix.

    dim: int
        The dimension of the space into which to embed.

    random_state: numpy RandomState or equivalent
        A state capable being used as a numpy random state.

    Returns
    -------
    embedding: array of shape (n_vertices, dim)
        The spectral embedding of the graph.
    """
    n_samples = graph.shape[0]
    n_components, labels = scipy.sparse.csgraph.connected_components(graph)

    if n_components > 1:
        return multi_component_layout(
            data,
            graph,
            n_components,
            labels,
            dim,
            random_state,
            metric=metric,
            metric_kwds=metric_kwds,
        )

    diag_data = np.asarray(graph.sum(axis=0))
    # standard Laplacian
    # D = scipy.sparse.spdiags(diag_data, 0, graph.shape[0], graph.shape[0])
    # L = D - graph
    # Normalized Laplacian
    I = scipy.sparse.identity(graph.shape[0], dtype=np.float64)
    D = scipy.sparse.spdiags(
        1.0 / np.sqrt(diag_data), 0, graph.shape[0], graph.shape[0]
    )
    L = I - D * graph * D

    k = dim + 1
    num_lanczos_vectors = max(2 * k + 1, int(np.sqrt(graph.shape[0])))
    try:
        if L.shape[0] < 2000000:
            eigenvalues, eigenvectors = scipy.sparse.linalg.eigsh(
                L,
                k,
                which="SM",
                ncv=num_lanczos_vectors,
                tol=1e-4,
                v0=np.ones(L.shape[0]),
                maxiter=graph.shape[0] * 5,
            )
        else:
            eigenvalues, eigenvectors = scipy.sparse.linalg.lobpcg(
                L, random_state.normal(size=(L.shape[0], k)), largest=False, tol=1e-8
            )
        order = np.argsort(eigenvalues)[1:k]
        return eigenvectors[:, order]
    except scipy.sparse.linalg.ArpackError:
        warn(
            "WARNING: spectral initialisation failed! The eigenvector solver\n"
            "failed. This is likely due to too small an eigengap. Consider\n"
            "adding some noise or jitter to your data.\n\n"
            "Falling back to random initialisation!"
        )
        return random_state.uniform(low=-10.0, high=10.0, size=(graph.shape[0], dim))
def spectral_layout ( data , graph , dim , random_state , metric = "euclidean" , metric_kwds = { } ) : n_samples = graph . shape [ 0 ] n_components , labels = scipy . sparse . csgraph . connected_components ( graph ) if n_components > 1 : return multi_component_layout ( data , graph , n_components , labels , dim , random_state , metric = metric , metric_kwds = metric_kwds , ) diag_data = np . asarray ( graph . sum ( axis = 0 ) ) # standard Laplacian # D = scipy.sparse.spdiags(diag_data, 0, graph.shape[0], graph.shape[0]) # L = D - graph # Normalized Laplacian I = scipy . sparse . identity ( graph . shape [ 0 ] , dtype = np . float64 ) D = scipy . sparse . spdiags ( 1.0 / np . sqrt ( diag_data ) , 0 , graph . shape [ 0 ] , graph . shape [ 0 ] ) L = I - D * graph * D k = dim + 1 num_lanczos_vectors = max ( 2 * k + 1 , int ( np . sqrt ( graph . shape [ 0 ] ) ) ) try : if L . shape [ 0 ] < 2000000 : eigenvalues , eigenvectors = scipy . sparse . linalg . eigsh ( L , k , which = "SM" , ncv = num_lanczos_vectors , tol = 1e-4 , v0 = np . ones ( L . shape [ 0 ] ) , maxiter = graph . shape [ 0 ] * 5 , ) else : eigenvalues , eigenvectors = scipy . sparse . linalg . lobpcg ( L , random_state . normal ( size = ( L . shape [ 0 ] , k ) ) , largest = False , tol = 1e-8 ) order = np . argsort ( eigenvalues ) [ 1 : k ] return eigenvectors [ : , order ] except scipy . sparse . linalg . ArpackError : warn ( "WARNING: spectral initialisation failed! The eigenvector solver\n" "failed. This is likely due to too small an eigengap. Consider\n" "adding some noise or jitter to your data.\n\n" "Falling back to random initialisation!" ) return random_state . uniform ( low = - 10.0 , high = 10.0 , size = ( graph . shape [ 0 ] , dim ) )