Created
August 1, 2018 21:55
-
-
Save ericmjl/cabf21cba171da0d59a062c0b453cdd1 to your computer and use it in GitHub Desktop.
Generate lots of random graphs with scalar features on each node.
This file contains 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 | |
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