Python Networkx 中的社区检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22070196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:10:22  来源:igfitidea点击:

Community detection in Networkx

pythonigraphnetworkxmodularity

提问by Alan Valejo

I'm studying about detection communities in networks.

我正在研究网络中的检测社区。

I'm use igraph and Python

我使用 igraph 和 Python

For the optimal number of communities in terms of the modularity measure:

对于模块化度量方面的最佳社区数量:

from igraph import *
karate = Nexus.get("karate")
cl = karate.community_fastgreedy()
cl.as_clustering().membership

For supply the desired number of communities:

为了提供所需数量的社区:

from igraph import *
karate = Nexus.get("karate")
cl = karate.community_fastgreedy()
k=2
cl.as_clustering(k).membership

However, I like to do this using networkx. I know get optimal number of communities in terms of the modularity measure:

但是,我喜欢使用 networkx 来做到这一点。我知道在模块化度量方面获得最佳社区数量:

import community # --> http://perso.crans.org/aynaud/communities/
import fastcommunity as fg # --> https://networkx.lanl.gov/trac/ticket/245
import networkx as nx

g = nx.karate_club_graph()
partition = community.best_partition(g)
print "Louvain Modularity: ", community.modularity(partition, g)
print "Louvain Partition: ", partition

cl = fg.communityStructureNewman(g)
print "Fastgreed Modularity: ", cl[0]
print "Fastgreed Partition: ", cl[1]

But I can not get the desired number of communities. Are there some algorithm for this, using Networkx?

但我无法获得所需数量的社区。是否有一些算法,使用 Networkx?

回答by zihaolucky

I'm also new to networkx and igraph, I used Gephi, an data visualization tool/software. And it has the same community detection algorithm as the one in networkx you are now using. Specifically, in http://perso.crans.org/aynaud/communities/

我也是 networkx 和 igraph 的新手,我使用了 Gephi,一种数据可视化工具/软件。它具有与您现在使用的 networkx 中相同的社区检测算法。具体来说,在http://perso.crans.org/aynaud/communities/

It uses the louvain method described in Fast unfolding of communities in large networks, Vincent D Blondel, Jean-Loup Guillaume, Renaud Lambiotte, Renaud Lefebvre, Journal of Statistical Mechanics: Theory and Experiment 2008(10), P10008 (12pp)

它使用在大型网络中社区的快速展开、Vincent D Blondel、Jean-Loup Guillaume、Renaud Lambiotte、Renaud Lefebvre、Journal of Statistical Mechanics: Theory and Experiment 2008(10)、P10008 (12pp) 中描述的 louvain 方法

You can not get desired number of communities, as I know, there're two ways worth to try:

您无法获得所需数量的社区,据我所知,有两种方法值得尝试:

  • Use Gephi. You can use gephi and there's a parameter called resolutionthat would change the size of the community you get.
  • Use NetworkX. This time, we may not use best_partition(G)any more. But use partition_at_level(dendrogram, level), I guess this might help.
  • 使用 Gephi。您可以使用 gephi 并且有一个名为的参数resolution可以改变您获得的社区的规模。
  • 使用 NetworkX。这一次,我们可能不再使用best_partition(G)了。但是使用partition_at_level(dendrogram, level),我想这可能会有所帮助。

Check the source codehere for more info.

在此处查看源代码以获取更多信息。

回答by Johannes Wachs

Perhaps I am misunderstanding you, but if you would like the number of communities output by the NetworkX implementation of the best_partition algorithm, just note that best_partition(G) gives a dictionary with nodes as keys and their partition number as value.

也许我误解了您的意思,但是如果您想知道 best_partition 算法的 NetworkX 实现输出的社区数量,请​​注意 best_partition(G) 给出了一个以节点为键、分区号为值的字典。

You can count the number of unique values in a dictionary like this (likely not optimal):

您可以像这样计算字典中唯一值的数量(可能不是最佳的):

dict = {'a':1,'b':1,'c':2,'d':1,'e':3,'f':4,'g':5}
count=list(set([i for i in dict.values()]))
print count
print len(count)

With result

结果

[1, 2, 3, 4, 5]
5