def _fit_transform(self, graph: Graph, return_dataframe: bool = True, verbose: bool = True) -> EmbeddingResult: """Return node embedding.""" number_of_nodes = graph.get_number_of_nodes() embedding = eigh(graph.get_dense_modularity_matrix(), eigvals=(number_of_nodes - self._embedding_size, number_of_nodes - 1))[1] if return_dataframe: node_names = graph.get_node_names() embedding = pd.DataFrame(embedding, index=node_names) return EmbeddingResult(embedding_method_name=self.model_name(), node_embeddings=embedding)
def _fit_transform(self, graph: Graph, return_dataframe: bool = True, verbose: bool = True) -> EmbeddingResult: """Return node embedding.""" matrix = None if self._metric == "Jaccard": edges, weights = graph.get_jaccard_coo_matrix() elif self._metric == "Laplacian": edges, weights = graph.get_laplacian_coo_matrix() elif self._metric == "Modularity": matrix = graph.get_dense_modularity_matrix() elif self._metric == "Left Normalized Laplacian": edges, weights = graph.get_left_normalized_laplacian_coo_matrix() elif self._metric == "Right Normalized Laplacian": edges, weights = graph.get_right_normalized_laplacian_coo_matrix() elif self._metric == "Symmetric Normalized Laplacian": edges, weights = graph.get_symmetric_normalized_laplacian_coo_matrix( ) elif self._metric == "Neighbours Intersection size": edges, weights = graph.get_neighbours_intersection_size_coo_matrix( ) elif self._metric == "Ancestors Jaccard": matrix = graph.get_shared_ancestors_jaccard_adjacency_matrix( graph.get_breadth_first_search_from_node_names( src_node_name=self._root_node_name, compute_predecessors=True), verbose=verbose) elif self._metric == "Ancestors size": matrix = graph.get_shared_ancestors_size_adjacency_matrix( graph.get_breadth_first_search_from_node_names( src_node_name=self._root_node_name, compute_predecessors=True), verbose=verbose) elif self._metric == "Adamic-Adar": edges, weights = graph.get_adamic_adar_coo_matrix() elif self._metric == "Adjacency": edges, weights = graph.get_directed_edge_node_ids(), np.ones( graph.get_number_of_directed_edges()) else: raise NotImplementedError(f"The provided metric {self._metric} " "is not currently supported.") if matrix is None: matrix = coo_matrix((weights, (edges[:, 0], edges[:, 1])), shape=(graph.get_number_of_nodes(), graph.get_number_of_nodes()), dtype=np.float32) U, sigmas, Vt = sparse_svds(matrix, k=int(self._embedding_size / 2)) else: U, sigmas, Vt = randomized_svd(matrix, n_components=int( self._embedding_size / 2)) sigmas = np.diagflat(np.sqrt(sigmas)) left_embedding = np.dot(U, sigmas) right_embedding = np.dot(Vt.T, sigmas) if return_dataframe: node_names = graph.get_node_names() left_embedding = pd.DataFrame(left_embedding, index=node_names) right_embedding = pd.DataFrame(right_embedding, index=node_names) return EmbeddingResult( embedding_method_name=self.model_name(), node_embeddings=[left_embedding, right_embedding])