Skip to content

Instantly share code, notes, and snippets.

@ericmjl
Created August 1, 2018 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericmjl/cabf21cba171da0d59a062c0b453cdd1 to your computer and use it in GitHub Desktop.
Save ericmjl/cabf21cba171da0d59a062c0b453cdd1 to your computer and use it in GitHub Desktop.
Generate lots of random graphs with scalar features on each node.
import networkx as nx
import numpy as np
def generate_graph():
num_nodes = np.random.randint(low=3, high=20)
G = nx.erdos_renyi_graph(n=num_nodes, p=0.3)
for n in G.nodes():
value = np.random.randint(low=1, high=20)
G.node[n]['value'] = value
return G
G = generate_graph()
def features(G):
values = [d['value'] for n, d in G.nodes(data=True)]
return np.array(values).reshape(-1, 1)
n_graphs = 30
graphs = []
amats = []
feats = []
sums = []
for i in range(n_graphs):
g = generate_graph()
graphs.append(g)
a = nx.adjacency_matrix(g).todense()
amats.append(a)
f = features(g)
feats.append(f)
s = np.ones(shape=(1, a.shape[0]))
sums.append(s)
import scipy.sparse as sp
a = sp.block_diag(amats)
f = np.vstack(feats)
s = sp.block_diag(sums)
sum([d['value'] for n, d in graphs[0].nodes(data=True)])
sums[0] @ feats[0]
s @ f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment