Networkx中的社区检测

时间:2014-02-27 13:35:38

标签: python igraph networkx modularity

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

我使用igraph和Python

关于模块化度量的最佳社区数量:

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

供应所需数量的社区:

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

但是,我喜欢使用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]

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

2 个答案:

答案 0 :(得分:4)

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

  

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

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

  • 使用Gephi。您可以使用gephi,并且有一个名为resolution的参数可以改变您获得的社区的大小。
  • 使用NetworkX。这一次,我们可能不再使用best_partition(G)。但是使用partition_at_level(dendrogram, level),我想这可能会有所帮助。

点击此处source code了解详情。

答案 1 :(得分:1)

也许我误解了你,但是如果你想通过best_partition算法的NetworkX实现输出的社区数量,请​​注意best_partition(G)给出一个字典,其中节点作为键,其分区号作为值。

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

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)

结果

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