-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlouvain_algorithm_modularity.py
More file actions
34 lines (27 loc) · 1 KB
/
louvain_algorithm_modularity.py
File metadata and controls
34 lines (27 loc) · 1 KB
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
"""This file implements functions to return a graph
on which the modularity-maximizing communities are indicated
by using the Louvain algorithm.
@source https://stackoverflow.com/questions/29897243/graph-modularity-in-python-networkx"""
import community
import matplotlib.pyplot as plt
import networkx as nx
import graphing
def main():
G = nx.Graph()
G = graphing.create_graph('115_house.csv')
nx.transitivity(G)
# Find modularity
part = community.best_partition(G)
print(part)
mod = community.modularity(part,G)
# Plot, color nodes using community structure
values = [part.get(node) for node in G.nodes()]
nx.draw_spring(G, cmap=plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
plt.show()
def louvainCluster(g):
clustering = community.best_partition(g)
mod = community.modularity(clustering, g)
num_clusters = max([clustering[key] for key in clustering]) + 1
return clustering, num_clusters, mod
if __name__ == '__main__':
main()