community简单的例子

来源:互联网 发布:阿里云域名口令 编辑:程序博客网 时间:2024/06/14 22:34

1.下载工具包,并安装。
2.import community到编写的.py文件中。

import communityimport networkx as nximport matplotlib.pyplot as plt#better with karate_graph() as defined in networkx example.#erdos renyi don't have true community structureG = nx.erdos_renyi_graph(30, 0.05)#first compute the best partitionpartition = community.best_partition(G)#drawingsize = 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()

这里写图片描述

【1】API官方文档:
http://perso.crans.org/aynaud/communities/api.html#community.best_partition

1 0
原创粉丝点击