网络中的社区检测

2024-04-28 18:50:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我用的是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有什么算法吗?


Tags: fromcommunityimportnetworkxnexusget度量cl
2条回答

我也是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)

你无法获得理想数量的社区,我知道,有两种方法值得尝试:

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

查看这里的source code了解更多信息。

也许我误解了您的意思,但是如果您希望NetworkX实现的best_partition算法输出的社区数,请注意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

相关问题 更多 >