python网络

 1 import community
 2 import networkx as nx
 3 import matplotlib.pyplot as plt
 4 
 5 #better with karate_graph() as defined in networkx example.
 6 #erdos renyi don't have true community structure
 7 G = nx.erdos_renyi_graph(30, 0.05)
 8 
 9 #first compute the best partition
10 partition = community.best_partition(G)
11 
12 #drawing
13 size = float(len(set(partition.values())))
14 pos = nx.spring_layout(G)
15 count = 0.
16 for com in set(partition.values()) :
17     count = count + 1.
18     list_nodes = [nodes for nodes in partition.keys()
19                                 if partition[nodes] == com]
20     nx.draw_networkx_nodes(G, pos, list_nodes, node_size = 20,
21                                 node_color = str(count / size))
22 
23 
24 nx.draw_networkx_edges(G,pos, alpha=0.5)
25 plt.show()

首先一定要先安装好这三个库,community,networkx,matplotlib,这个很简单使用pip install就好。

但是在运行的过程中可能会出现这种错误“module 'community' has no attribute 'best_partition'。”

原因是缺少python-louvain.

再pip 安装就好。

结果如下图所示。

 

 

转载于:https://www.cnblogs.com/lfsometimes/p/10197165.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值