Skip to content

Instantly share code, notes, and snippets.

@rohithteja
Last active September 24, 2022 02:56
Show Gist options
  • Save rohithteja/a315072365fcd52f03e0ec3e993d70c6 to your computer and use it in GitHub Desktop.
Save rohithteja/a315072365fcd52f03e0ec3e993d70c6 to your computer and use it in GitHub Desktop.
Graph statistics
import networkx as nx
import numpy as np
import torch
from sklearn.preprocessing import StandardScaler
# 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)
# using degree as embedding
embeddings = np.array(list(dict(G.degree()).values()))
# normalizing degree values
scale = StandardScaler()
embeddings = scale.fit_transform(embeddings.reshape(-1,1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment