-
Notifications
You must be signed in to change notification settings - Fork 111
/
create_Graph.py
executable file
·61 lines (41 loc) · 1.28 KB
/
create_Graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
import pickle as pkl
import scipy.sparse as sp
import sys
import os
import networkx as nx
from utils import *
import json
from networkx.readwrite import json_graph
# 'cora', 'citeseer', 'pubmed'
if __name__=="__main__":
data_name = 'cora'
adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(data_name)
G = nx.from_scipy_sparse_matrix(adj)
val_index = np.where(val_mask)[0]
test_index = np.where(test_mask)[0]
y = y_train+y_val+y_test
y = np.argmax(y,axis=1)
for i in range(len(y)):
if i in val_index:
G.node[i]['val']=True
G.node[i]['test']=False
elif i in test_index:
G.node[i]['test']=True
G.node[i]['val']=False
else:
G.node[i]['test'] = False
G.node[i]['val'] = False
data = json_graph.node_link_data(G)
with open("cora/cora-G.json","wb") as f:
json.dump(data,f)
classMap = {}
idMap = {}
for i in range(len(y)):
classMap[i]=y[i]
idMap[i] = i
with open("cora/cora-id_map.json","wb") as f:
json.dump(idMap,f)
with open("cora/cora-class_map.json","wb") as f:
json.dump(classMap,f)
np.save(open("cora/cora-feats.npy","wb"), features.todense())