Newer
Older
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Graph\" data-toc-modified-id=\"Graph-1\"><span class=\"toc-item-num\">1 </span>Graph</a></span></li><li><span><a href=\"#Comunity-detection\" data-toc-modified-id=\"Comunity-detection-2\"><span class=\"toc-item-num\">2 </span>Comunity detection</a></span></li><li><span><a href=\"#Visualization\" data-toc-modified-id=\"Visualization-3\"><span class=\"toc-item-num\">3 </span>Visualization</a></span></li><li><span><a href=\"#Further-analysis\" data-toc-modified-id=\"Further-analysis-4\"><span class=\"toc-item-num\">4 </span>Further analysis</a></span></li><li><span><a href=\"#Karate-club-library\" data-toc-modified-id=\"Karate-club-library-5\"><span class=\"toc-item-num\">5 </span>Karate club library</a></span><ul class=\"toc-item\"><li><span><a href=\"#Graph\" data-toc-modified-id=\"Graph-5.1\"><span class=\"toc-item-num\">5.1 </span>Graph</a></span></li><li><span><a href=\"#Algorithm\" data-toc-modified-id=\"Algorithm-5.2\"><span class=\"toc-item-num\">5.2 </span>Algorithm</a></span></li><li><span><a href=\"#Evaluation\" data-toc-modified-id=\"Evaluation-5.3\"><span class=\"toc-item-num\">5.3 </span>Evaluation</a></span></li></ul></li></ul></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Community detection in network graph\n",
"\n",
"In this notebook we want to detect communtiy in network graphs. \n",
"\n",
"First, we import all necessary modules.\n",
"\n",
"- `networkx`, `karateclub`: community detection algorithms\n",
"- `matplotlib`: visualization.\n",
"- `ipywidgets`: interactive features \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import networkx as nx\n",
"from networkx.algorithms.community.centrality import girvan_newman\n",
"from networkx.algorithms import community\n",
"from karateclub import EgoNetSplitter\n",
"import itertools\n",
"\n",
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
"import ipywidgets as widgets"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Graph\n",
"\n",
"First, we load a graph."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# see https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.generators.social.karate_club_graph.html\n",
"\n",
"G = nx.karate_club_graph()\n",
"print(G.edges)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# graph information\n",
"\n",
"print(\"count nodes:\" , len(G.nodes))\n",
"print(\"count edges:\" , len(G.edges))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we can draw the graph to get an impression on how it looks like."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig=plt.figure(figsize=(18, 16), dpi= 80, facecolor='w', edgecolor='k')\n",
"nx.draw_spring(G, with_labels=True)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comunity detection\n",
"\n",
"We use the Girvan-Newman algorithm to detect communities:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Girvan-Newman algorithm\n",
"communities = girvan_newman(G)\n",
"\n",
"# save results in a list, each element of the list consists of the communities on one level. \n",
"# necessary to use the interactive slider\n",
"\n",
"com_lvl_lst = [] \n",
"for com_on_lvlk in itertools.islice(communities, len(G.nodes)):\n",
" com_lvl_lst.append(com_on_lvlk)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we have a list of the communities on each level.\n",
"\n",
"Since the algorithm is a hierarchical method and terminates when no edges remain, we have to choose the level ourselves.\n",
"\n",
"For a small graph, we can visualize the results and have a \"sharp look\" which community structures fits best.\n",
"\n",
"## Visualization"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_communities_per_lvl(c,k):\n",
" return c[k]\n",
"def draw_communities(c, k):\n",
" communities = get_communities_per_lvl(c,k)\n",
" values = [0]*len(G.nodes)\n",
" for i,lst in enumerate(communities):\n",
" for el in lst:\n",
" values[el]=i \n",
" fig=plt.figure(figsize=(18, 16), dpi= 80, facecolor='w', edgecolor='k')\n",
" nx.draw_networkx(G, cmap = plt.get_cmap('jet'), node_color = values, with_labels=True)\n",
" return values\n",
"\n",
"# use a slider to see the different levels of the community structures\n",
"w = interactive(draw_communities,c=fixed(com_lvl_lst), k=(0, len(G.nodes)-2, 1))\n",
"display(w)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Further analysis\n",
"\n",
"\n",
"Sometimes it is interesting how communities are connected to each other. To get an overview, we merge all nodes of one community to one new community node and add an edge between two community nodes if at least one pair of nodes from two community nodes are connected by an edge in the original graph.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"G_merged = nx.Graph()\n",
"classified_nodes = w.result\n",
"\n",
"community_count = len(set(classified_nodes))\n",
"# print(community_count)\n",
"for n in G.edges:\n",
" node_1 = classified_nodes[n[0]]\n",
" node_2 = classified_nodes[n[1]]\n",
" G_merged.add_edge(node_1, node_2)\n",
"# print(G.edges)\n",
"# print(G_merged.edges)\n",
"fig=plt.figure(figsize=(18, 16), dpi= 80, facecolor='w', edgecolor='k')\n",
"nx.draw_spring(G_merged, node_color = range(0,community_count),with_labels=True)\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Karate club library\n",
"\n",
"Before, we have used only the `networkx` library. The `karateclub` library has advanced algorithms.\n",
"Let's try it out with a bigger graph.\n",
"\n",
"see: https://github.com/benedekrozemberczki/karateclub\n",
"\n",
"### Graph\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from karateclub import GraphReader\n",
"\n",
"reader = GraphReader(\"facebook\")\n",
"\n",
"graph = reader.get_graph()\n",
"target = reader.get_target()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"count nodes:\" , len(graph.nodes))\n",
"print(\"count edges:\" , len(graph.edges))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The graph consists of 22470 nodes and 171002 edges. This is why we can not visualize the graph, as it is too large.\n",
"\n",
"\n",
"### Algorithm\n",
"\n",
"In the following we use the Label propagation algorithm to detect communities."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from karateclub import LabelPropagation\n",
"\n",
"model = LabelPropagation()\n",
"model.fit(graph)\n",
"cluster_membership = model.get_memberships()\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Evaluation\n",
"\n",
"The nodes are already classified into communities. The labels are stored in the variable `target`.\n",
"With this information, we can compare our prediction with the labels using the normalized mutual information score, which computes the correlation between two clusters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics.cluster import normalized_mutual_info_score\n",
"\n",
"cluster_membership = [cluster_membership[node] for node in range(len(cluster_membership))]\n",
"\n",
"nmi = normalized_mutual_info_score(target, cluster_membership)\n",
"print('NMI: {:.4f}'.format(nmi))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"TODO: \n",
"- Add different algorithms.\n",
"- Add measures (how \"good\" is the community detection\"? How can you measure it?)\n",
"- Use \"real-world\" example (facebook data / metadata / coauthorship)\n",
"- Use karate club library and try out one of the machine learning algorithms\n",
"- Color communities and its nodes in same color in both graphs."
]
}
],
"metadata": {
"language_info": {
"name": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}