Last active
August 21, 2021 12:54
-
-
Save rohithteja/bcd312fec609f1551d33550aecf13427 to your computer and use it in GitHub Desktop.
Data Preparation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import numpy as np | |
import torch | |
# load graph from networkx library | |
G = nx.karate_club_graph() | |
# retrieve the labels for each node | |
labels = np.asarray([G.nodes[i]['club'] != 'Mr. Hi' for i in G.nodes]).astype(np.int64) | |
# create edge index from | |
adj = nx.to_scipy_sparse_matrix(G).tocoo() | |
row = torch.from_numpy(adj.row.astype(np.int64)).to(torch.long) | |
col = torch.from_numpy(adj.col.astype(np.int64)).to(torch.long) | |
edge_index = torch.stack([row, col], dim=0) | |
# prepare the embeddings | |
nodes = pd.DataFrame(list(H.nodes())) | |
nodes.columns = ['nodes'] | |
nodes['embeddings'] = nodes['nodes'].map(embeddings) | |
embeddings = torch.from_numpy(np.stack(nodes.embeddings.values)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment