파이썬에는 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
'Programing Language > Python' 카테고리의 다른 글
python(인터프리팅언어)와 C(컴파일언어) 비교 (0) | 2018.09.27 |
---|---|
클러스터링 알고리즘 5가지 비교[링크] (0) | 2018.08.22 |
list와 array의 메모리 효율성 (0) | 2018.06.19 |
json data 저장시 numpy array 저장하는 법 (0) | 2018.05.29 |
pandas dataframe에 multiple condition on multiple column (0) | 2018.04.18 |