-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpa.py
205 lines (137 loc) · 5.11 KB
/
lpa.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import sys
import random
import networkx as nx
import matplotlib.pyplot as plt
import community
import math
import timeit
import numpy as np
import scipy as sp
def read_graph_from_file(path):
# read edge-list from file
graph = nx.read_edgelist(path, data = (('weight', float), ))
# initial graph node's attribute 'label' with its id
for node, data in graph.nodes_iter(True):
data['label'] = node
return graph
def lpa(graph):
def estimate_stop_cond():
for node in graph.nodes_iter():
count = {}
for neighbor in graph.neighbors_iter(node):
neighbor_label = graph.node[neighbor]['label']
neighbor_weight = 1
count[neighbor_label] = count.setdefault(neighbor_label, 0.0) + neighbor_weight
count_items = count.items()
count_items.sort(key = lambda x: x[1], reverse = True)
labels = [k for k,v in count_items if v == count_items[0][1]]
if graph.node[node]['label'] not in labels:
return False
return True
loop_count = 0
while True:
loop_count += 1
for node in graph.nodes_iter():
count = {}
for neighbor in graph.neighbors_iter(node):
neighbor_label = graph.node[neighbor]['label']
neighbor_weight = 1
count[neighbor_label] = count.setdefault(neighbor_label, 0.0) + neighbor_weight
# find out labels with maximum count
count_items = count.items()
count_items.sort(key = lambda x: x[1], reverse = True)
labels = [(k, v) for k, v in count_items if v == count_items[0][1]]
label = random.sample(labels, 1)[0][0]
graph.node[node]['label'] = label
if estimate_stop_cond() is True or loop_count >= 10:
return
def revert_graph_info(graph):
game_info = {}
info = {}
num=0
result = {}
for node, data in graph.nodes_iter(True):
info.setdefault(graph.node[node]['label'], []).append(game_info.get(node, node))
for clazz in info:
for label in info[clazz]:
result[label] = num
num=num+1
return result
def calculate_NMI(clust1, clust2):
#N matrix
N = get_N_matrix(clust1, clust2)
Ntotal = N.sum()
#number of communities
[Ca,Cb] = N.shape
#upper equation - sum of i and j
v_ij = 0
for i in range(0, Ca):
for j in range(0, Cb):
#sum of N for i and j
Ni = N[i,:].sum()
Nj = N[:,j].sum()
#to avoid log(0)
if (N[i,j] != 0):
#equation calculation
v_ij = v_ij + (N[i,j] * math.log10( (N[i,j] * Ntotal) / (Ni * Nj) ))
v_i = 0
for i in range(0, Ca):
Ni = N[i,:].sum()
v_i = v_i + Ni * math.log10( Ni / Ntotal)
v_j = 0
for j in range(0, Cb):
Nj = N[:,j].sum()
v_j = v_j + Nj * math.log10( Nj / Ntotal)
v = -2 * (v_ij / (v_i + v_j))
return v
#gets the N matrix between two clusterings
def get_N_matrix(clust1, clust2):
#the set of unique communities in clustering
communitiesInClust1 = set(clust1.values())
communitiesInClust2 = set(clust2.values())
#initialize N matrix
N = np.empty([len(communitiesInClust1), len(communitiesInClust2)])
for i in communitiesInClust1:
c1 = dict((key,value) for key, value in clust1.iteritems() if value == i).keys()
for j in communitiesInClust2:
c2 = dict((key,value) for key, value in clust2.iteritems() if value == j).keys()
n=0
for k in c2:
for m in c1:
if k == m:
n = n + 1
N[i,j] = n
return N
if __name__ == '__main__':
g = read_graph_from_file('Data/a.data')
#timer start c1
startC1 = timeit.default_timer()
lpa(g)
#print run time for c1
runtimeC1 = timeit.default_timer() - startC1
cluster1 = revert_graph_info(g)
mod1 = community.modularity(cluster1, g)
graph = nx.read_edgelist("Data/a.data")
#timer start c1
startC2 = timeit.default_timer()
#best partition calculation
cluster2 = community.best_partition(graph)
#print run time for c1
runtimeC2 = timeit.default_timer() - startC2
mod2 = community.modularity(cluster2, graph)
graph = nx.read_edgelist("Data/a.data")
#timer start c1
startC3 = timeit.default_timer()
tmp = community.generate_dendogram(graph)
cluster3 = community.partition_at_level(tmp, 0)
#print run time for c1
runtimeC3 = timeit.default_timer() - startC3
mod3 = community.modularity(cluster3, graph)
print "modularity: 1:%f; 2:%f; 3:%f" % (mod1,mod2, mod3)
nmi1 = calculate_NMI(cluster1, cluster2)
print "nmi between cluster1 and cluster 2: %.10f" % nmi1
nmi2 = calculate_NMI(cluster1, cluster3)
print "nmi between cluster1 and cluster 3: %.10f" % nmi2
nmi3 = calculate_NMI(cluster2, cluster3)
print "nmi between cluster2 and cluster 3: %.10f" % nmi3
print "Clustering Run Time: 1:%f; 2:%f; 3:%f" %(runtimeC1,runtimeC2,runtimeC3)