def run(self, G): ''' :G: a graph to run on. ''' self.G = G netmf = KarateNetMF(dimensions=128, order=self.window_size) idx_node = dict() node_idx = dict() for i, node in enumerate(G.nodes()): idx_node[i] = node node_idx[node] = i edges = list() for u, v in G.edges(): edges.append((node_idx[u], node_idx[v])) netmf_G = nx.Graph(edges) print('Fitting NetMF.') netmf.fit(netmf_G) embeddings = netmf.get_embedding() print('NetMF Fit.') i = 0 with open(self.emb_path, 'w') as f: f.write('{} 128\n'.format(len(embeddings))) for emb in embeddings: f.write('{} {}\n'.format(idx_node[i], ' '.join(list(str(d) for d in emb)))) i += 1
#------------------------------ g = nx.newman_watts_strogatz_graph(1000, 20, 0.05) model = LaplacianEigenmaps() model.fit(g) embedding = model.get_embedding() #-------------- # NEU example #-------------- g = nx.newman_watts_strogatz_graph(1000, 20, 0.05) model = NetMF() meta_model = NEU() meta_model.fit(g, model) #----------------------------------- # NetLSD example #----------------------------------- graphs = [nx.newman_watts_strogatz_graph(50, 5, 0.3) for _ in range(100)] model = NetLSD() model.fit(graphs) model.get_embedding()
"""NetMF illustrative example.""" import networkx as nx from karateclub.node_embedding.neighbourhood import NetMF g = nx.newman_watts_strogatz_graph(100, 20, 0.05) model = NetMF() model.fit(g)
from karateclub.community_detection.overlapping import EgoNetSplitter, NNSED, DANMF, MNMF, BigClam from karateclub.node_embedding.neighbourhood import GraRep, DeepWalk, Walklets, NMFADMM, Diff2Vec, BoostNE, NetMF from karateclub.community_detection.non_overlapping import EdMot, LabelPropagation from karateclub.node_embedding.structural import GraphWave from karateclub.node_embedding.attributed import BANE, TENE from karateclub.graph_embedding import Graph2Vec from karateclub.dataset import GraphReader, GraphSetReader #----------------------------------- # NetMF example #----------------------------------- g = nx.newman_watts_strogatz_graph(100, 20, 0.05) model = NetMF() model.fit(g) model.get_embedding() #----------------------------------- # GraphSet reader example #----------------------------------- reader = GraphSetReader("reddit10k") graphs = reader.get_graphs() y = reader.get_target() #----------------------------------- # Graph reader example