<h1>Table of Contents<spanclass="tocSkip"></span></h1>
<divclass="toc"><ulclass="toc-item"><li><span><ahref="#Graph"data-toc-modified-id="Graph-1"><spanclass="toc-item-num">1 </span>Graph</a></span></li><li><span><ahref="#Comunity-detection"data-toc-modified-id="Comunity-detection-2"><spanclass="toc-item-num">2 </span>Comunity detection</a></span></li><li><span><ahref="#Visualization"data-toc-modified-id="Visualization-3"><spanclass="toc-item-num">3 </span>Visualization</a></span></li><li><span><ahref="#Further-analysis"data-toc-modified-id="Further-analysis-4"><spanclass="toc-item-num">4 </span>Further analysis</a></span></li><li><span><ahref="#Karate-club-library"data-toc-modified-id="Karate-club-library-5"><spanclass="toc-item-num">5 </span>Karate club library</a></span><ulclass="toc-item"><li><span><ahref="#Graph"data-toc-modified-id="Graph-5.1"><spanclass="toc-item-num">5.1 </span>Graph</a></span></li><li><span><ahref="#Algorithm"data-toc-modified-id="Algorithm-5.2"><spanclass="toc-item-num">5.2 </span>Algorithm</a></span></li><li><span><ahref="#Evaluation"data-toc-modified-id="Evaluation-5.3"><spanclass="toc-item-num">5.3 </span>Evaluation</a></span></li></ul></li></ul></div>
%% Cell type:markdown id: tags:
# Community detection in network graph
In this notebook we want to detect communtiy in network graphs.
First, we import all necessary modules.
-`networkx`, `karateclub`: community detection algorithms
# use a slider to see the different levels of the community structures
w = interactive(draw_communities,c=fixed(com_lvl_lst), k=(0, len(G.nodes)-2, 1))
display(w)
```
%% Cell type:markdown id: tags:
## Further analysis
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.
The graph consists of 22470 nodes and 171002 edges. This is why we can not visualize the graph, as it is too large.
### Algorithm
In the following we use the Label propagation algorithm to detect communities.
%% Cell type:code id: tags:
```
from karateclub import LabelPropagation
model = LabelPropagation()
model.fit(graph)
cluster_membership = model.get_memberships()
```
%% Cell type:markdown id: tags:
### Evaluation
The nodes are already classified into communities. The labels are stored in the variable `target`.
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 id: tags:
```
from sklearn.metrics.cluster import normalized_mutual_info_score
cluster_membership = [cluster_membership[node] for node in range(len(cluster_membership))]