Source code for karateclub.node_embedding.neighbourhood.laplacianeigenmaps

import numpy as np
import networkx as nx
import scipy.sparse as sps
from karateclub.estimator import Estimator

[docs]class LaplacianEigenmaps(Estimator): r"""An implementation of `"Laplacian Eigenmaps" <>`_ from the NIPS '01 paper "Laplacian Eigenmaps and Spectral Techniques for Embedding and Clustering". The procedure extracts the eigenvectors corresponding to the largest eigenvalues of the graph Laplacian. These vectors are used as the node embedding. Args: dimensions (int): Dimensionality of embedding. Default is 128. seed (int): Random seed value. Default is 42. """ def __init__(self, dimensions: int = 128, seed: int = 42): self.dimensions = dimensions self.seed = seed
[docs] def fit(self, graph: nx.classes.graph.Graph): """ Fitting a Laplacian EigenMaps model. Arg types: * **graph** *(NetworkX graph)* - The graph to be embedded. """ self._set_seed() graph = self._check_graph(graph) number_of_nodes = graph.number_of_nodes() L_tilde = nx.normalized_laplacian_matrix(graph, nodelist=range(number_of_nodes)) _, self._embedding = sps.linalg.eigsh( L_tilde, k=self.dimensions, which="SM", return_eigenvectors=True )
[docs] def get_embedding(self) -> np.array: r"""Getting the node embedding. Return types: * **embedding** *(Numpy array)* - The embedding of nodes. """ return self._embedding