[Python, networkx] community detecting using the modularity
파이썬에는 community라는 package가 있는데, 이 패키지가 아닌 python-louvain이 만든 community라는 패키지를 쓸 것이다.
# 설치 in terminal
pip install python-louvain
# 실행 in notebook
from community import community_louvain import networkx as nx import matplotlib.pyplot as plt #better with karate_graph() as defined in networkx example. #erdos renyi don't have true community structure G = nx.erdos_renyi_graph(30, 0.05) #first compute the best partition partition = community_louvain.best_partition(G) #drawing size = float(len(set(partition.values()))) pos = nx.spring_layout(G) count = 0. for com in set(partition.values()) : count = count + 1. list_nodes = [nodes for nodes in partition.keys() if partition[nodes] == com] nx.draw_networkx_nodes(G, pos, list_nodes, node_size = 20, node_color = str(count / size)) nx.draw_networkx_edges(G, pos, alpha=0.5) plt.show()
참고 :
https://python-louvain.readthedocs.io/en/latest/
https://github.com/taynaud/python-louvain