-
Notifications
You must be signed in to change notification settings - Fork 0
/
centrality.py
51 lines (40 loc) · 1.49 KB
/
centrality.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
50
51
import numpy as np
import networkx as nx
# put it back into a 2D symmetric array
def to_2d(vector, size):
x = np.zeros((size,size))
c = 0
for i in range(1,size):
for j in range(0,i):
x[i][j] = vector[c]
x[j][i] = vector[c]
c = c + 1
return x
def topological_measures(data, size):
CC = np.empty((0, size), int)
EC = np.empty((0, size), int)
PC = np.empty((0, size), int)
topology = []
max_solver_iterations = 1000000000
for i in range(data.shape[0]):
A = to_2d(data[i], size)
np.fill_diagonal(A, 0)
# create a graph from similarity matrix
G = nx.from_numpy_matrix(A)
U = G.to_undirected()
# # # compute closeness centrality and transform the output to vector
cc = nx.closeness_centrality(U)
closeness_centrality = np.array([cc[g] for g in U])
# # compute egin centrality and transform the output to vector
ec = nx.eigenvector_centrality(U, weight="weight", max_iter=max_solver_iterations)
eigenvector_centrality = np.array([ec[g] for g in U])
# compute pagerank
pr = nx.pagerank(U, alpha=0.85, weight="weight", max_iter=max_solver_iterations)
pagerank = np.array([pr[g] for g in U])
CC = np.vstack((CC, closeness_centrality))
EC = np.vstack((EC, eigenvector_centrality))
PC = np.vstack((PC, pagerank))
topology.append(CC)
topology.append(EC)
topology.append(PC)
return topology