def get_embeddings(self, inst, th=1): G = nx.parse_edgelist(self._compose_edge_list(inst.dist_mat, th), create_using=nx.DiGraph(), nodetype=None, data=[('weight', float)]) if self._embedding == 'deepwalk': model = DeepWalk(G, walk_length=10, num_walks=80, workers=1) model.train(window_size=5, iter=3) elif self._embedding == 'node2vec': model = Node2Vec(G, walk_length=10, num_walks=80, p=0.25, q=4, workers=1) # init model model.train(window_size=5, iter=3) # train model elif self._embedding == 'line': model = LINE(G, embedding_size=128, order='second') # init model,order can be ['first','second','all'] model.train(batch_size=1024, epochs=50, verbose=2) # train model elif self._embedding == 'sdne': model = SDNE(G, hidden_size=[256, 128]) # init model model.train(batch_size=3000, epochs=40, verbose=2) # train model elif self._embedding == 'struc2vec': model = Struc2Vec(G, 10, 80, workers=4, verbose=40, ) # init model model.train(window_size=5, iter=3) # train model else: return self._normalise(inst) ebds = model.get_embeddings() coords = [] for i in range(inst.n): coords.append(ebds[str(i)]) return np.array(coords)
model = TSNE(n_components=2) node_pos = model.fit_transform(emb_list) color_idx = {} for i in range(len(X)): color_idx.setdefault(Y[i][0], []) color_idx[Y[i][0]].append(i) for c, idx in color_idx.items(): plt.scatter(node_pos[idx, 0], node_pos[idx, 1], label=c) # c=node_colors) plt.legend() plt.show() if __name__ == "__main__": # G = nx.read_edgelist('../data/wiki/Wiki_edgelist.txt', # create_using=nx.DiGraph(), nodetype=None, data=[('weight', int)]) G = nx.read_edgelist( '../data/ETH/Phishing node classification/TransEdgelist.txt', create_using=nx.MultiDiGraph(), delimiter=',', nodetype=None, data=[('amount', float), ('weight', int)]) model = SDNE(G, hidden_size=[256, 128]) model.train(batch_size=3000, epochs=40, verbose=2) embeddings = model.get_embeddings() evaluate_embeddings(embeddings) plot_embeddings(embeddings)
# # model = SDNE(G, hidden_size=[256, 64],) # model.train(batch_size=3000, epochs=40, verbose=2) # embeddings = model.get_embeddings() # print(embeddings) # evaluate_embeddings(embeddings) # plot_embeddings(embeddings) import pandas as pd df = pd.DataFrame() df['source'] = [str(i) for i in [0, 1, 2, 3, 4, 4, 6, 7, 7, 9]] df['target'] = [str(i) for i in [1, 4, 4, 4, 6, 7, 5, 8, 9, 8]] G = nx.from_pandas_edgelist(df, create_using=nx.Graph()) model = SDNE( G, hidden_size=[4, 2], ) # model.train(batch_size=3000, epochs=40, verbose=2) embeddings = model.get_embeddings() # print(embeddings) x, y = [], [] print(sorted(embeddings.items(), key=lambda x: x[0])) for k, i in embeddings.items(): x.append(i[0]) y.append(i[1]) plt.scatter(x, y) plt.show()
G = nx.read_edgelist("data/" + d_set[i] + "/" + d_set[i] + ".edges", create_using=nx.DiGraph(), nodetype=None, data=[('weight', int)]) if (mdl[j] == "Node2Vec"): model = Node2Vec(G, walk_length=10, num_walks=80, p=0.25, q=4, workers=1) model.train(window_size=5, iter=3) elif (mdl[j] == "SDNE"): model = SDNE( G, hidden_size=[256, 128], ) model.train(batch_size=3000, epochs=40, verbose=2) elif (mdl[j] == "Struc2Vec"): model = Struc2Vec( G, 10, 80, workers=4, verbose=40, ) model.train(window_size=5, iter=3) elif (mdl[j] == "DeepWalk"): model = DeepWalk(G, walk_length=10, num_walks=80, workers=1) model.train(window_size=5, iter=3) elif (mdl[j] == "LINE"):