Python-louvain Package

pip install python-louvain

import community
#first compute the best partition
partition = community.best_partition(G)

#Drawing partition

Method 1:

#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()

Method 2:

pos = nx.spring_layout(G)
values = [partition.get(node) for node in G.nodes()]
nx.draw_networkx(G, pos, cmap=plt.get_cmap('magma'), node_color=values, node_size=50, with_labels=False)

Supplementary knowledge:

1. what is the partition of graphs.

partition: dict; {key (nodes_id): values(community_id)}

2.  function : community.best_partition(G)

Returns
-------
partition : dictionnary
The partition, with communities numbered from 0 to number of communities
def best_partition(graph,
partition=None,
weight='weight',
resolution=1.,
randomize=None,
random_state=None):
"""Compute the partition of the graph nodes which maximises the modularity
(or try..) using the Louvain heuristices This is the partition of highest modularity, i.e. the highest partition
of the dendrogram generated by the Louvain algorithm. Parameters
----------
graph : networkx.Graph
the networkx graph which is decomposed
partition : dict, optional
the algorithm will start using this partition of the nodes.
It's a dictionary where keys are their nodes and values the communities
weight : str, optional
the key in graph to use as weight. Default to 'weight'
resolution : double, optional
Will change the size of the communities, default to 1.
represents the time described in
"Laplacian Dynamics and Multiscale Modular Structure in Networks",
R. Lambiotte, J.-C. Delvenne, M. Barahona
randomize : boolean, optional
Will randomize the node evaluation order and the community evaluation
order to get different partitions at each call
random_state : int, RandomState instance or None, optional (default=None)
If int, random_state is the seed used by the random number generator;
If RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance used
by `np.random`. Returns
-------
partition : dictionnary
The partition, with communities numbered from 0 to number of communities

最新文章

  1. 王高利:Linux__apache,安装,报错解决
  2. [CareerCup] 8.2 Call Center 电话中心
  3. sdutoj 2152 Balloons
  4. centos6.5 安装iptables
  5. smarty函数-继承extents
  6. mysql 查询随机条记录的sql语句和php计算概率
  7. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证-2.2身份验证开发]
  8. [Python 3.x 官方文档翻译]The Python Tutorial Python教程
  9. HeadFirst设计模式读书笔记(3)-装饰者模式(Decorator Pattern)
  10. 出现Data Tools 与VS 不兼容问题
  11. iOS:获取图片Alpha图片
  12. js 中采用词法作用域
  13. JAVA虚拟机之对象探秘
  14. 软件测试第一次试验JUnit
  15. react ,ant Design UI中table组件合并单元格并展开详情的问题
  16. 动态设置和访问cxgrid列的Properties
  17. weblogic服务目录迁移记录
  18. application/json 和 application/x-www-form-urlencoded的区别
  19. 关于Hibernate多对多关联关系的更新问题。
  20. AndroidStudio3.0 注解报错Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor.

热门文章

  1. jQuery---钢琴案例 (按下1-9数字键,能触发对应的mouseenter事件)
  2. 基于element-ui 模仿微信聊天页面以及滚动条隐藏在chrome和其他浏览器的处理
  3. Angular修改Port文件一览
  4. 浅谈python的第三方库——numpy(二)
  5. Kali linux中安装字体的一种方法
  6. 论Flaks与Django的区别
  7. docker配置搭建gogs
  8. JN_0017:在当前目录运行CMD
  9. C#加密与解密(DES\RSA)学习笔记
  10. Pycharm有必要改的几个默认设置项