-
Notifications
You must be signed in to change notification settings - Fork 0
/
animals.py
40 lines (34 loc) · 1.24 KB
/
animals.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
import numpy as np
from sgl import LearnGraphTopolgy
from metrics import ModelSelection, Metrics
import networkx as nx
import matplotlib.pyplot as plt
with open('data/animals.txt', 'r') as f:
data = [[int(num) for num in line.split(',')] for line in f]
data = np.asarray(data).T
n = data.shape[0]
p = data.shape[1]
with open('data/animals_names.txt', 'r') as f:
names = [[str(num) for num in line.split(',')] for line in f]
names = names[0]
cov_emp = (data.T @ data)/ n
sgl = LearnGraphTopolgy(cov_emp + (1/3)*np.eye(p), maxiter=400, record_objective = True, record_weights = True)
graph = sgl.learn_k_component_graph(w0 = 'qp', k=5, beta=1)
# build network
A = graph['adjacency']
G = nx.from_numpy_matrix(A)
print('Graph statistics:')
print('Nodes: ', G.number_of_nodes(), 'Edges: ', G.number_of_edges() )
# normalize edge weights to plot edges strength
all_weights = []
for (node1,node2,data) in G.edges(data=True):
all_weights.append(data['weight'])
max_weight = max(all_weights)
norm_weights = [3* w / max_weight for w in all_weights]
norm_weights = norm_weights
labeldict = {}
for i, name in enumerate(names):
labeldict[i] = name
pos = nx.spring_layout(G)
nx.draw(G, labels=labeldict, pos=pos, width=norm_weights, with_labels = True)
plt.show()