使用igraph或其他库进行重叠社区检测

6
我希望能够检测小型网络/图中的重叠社区。所谓重叠是指在检测算法的输出中,一个节点可以包含在多个社区/簇中。
我已经查看了igraph目前提供的各种社区检测算法,但我认为它们都不能处理重叠的社区。
理想情况下,我希望能够以编程方式利用一些此类算法的Python实现。但使用其他语言也可以。
5个回答

10
我之前使用igraph的Python接口实现了Ahn等人提出的层次链接聚类算法,源代码在这里
此外,使用igraph在Python中实现CFinder相当容易;以下是我编写的代码:
#!/usr/bin/env python
from itertools import combinations

import igraph
import optparse

parser = optparse.OptionParser(usage="%prog [options] infile")
parser.add_option("-k", metavar="K", default=3, type=int,
        help="use a clique size of K")

options, args = parser.parse_args()

if not args:
    parser.error("Required input file as first argument")

k = options.k
g = igraph.load(args[0], format="ncol", directed=False)
cls = map(set, g.maximal_cliques(min=k))

edgelist = []
for i, j in combinations(range(len(cls)), 2):
    if len(cls[i].intersection(cls[j])) >= k-1:
        edgelist.append((i, j))

cg = igraph.Graph(edgelist, directed=False)
clusters = cg.clusters()
for cluster in clusters:
    members = set()
    for i in cluster:
        members.update(cls[i])
    print "\t".join(g.vs[members]["name"])

哎呀,我有同样的问题,有时候真希望我用的是 Python 而不是 R... - user13822027

4
如果您不介意使用另一种编程语言,您可以选择CFinder(Java),它基于团伙渗透(基本上是寻找紧密连接的团伙),OSLOM(C ++)优化统计量以及其他方法。
否则,如果您想坚持使用Python,您也可以应用Evans&Lambiotte'09的方法:1)将图形转换为线图,2)在其中应用常规社区检测方法,例如使用igraph,3)获取重叠社区。将图形转换为线图并不太复杂,并且应该很快,前提是您的原始图形不太大。在任何情况下,这比执行社区检测本身要快。
请注意,还有其他方法可以从常规(相互独立的社区)方法获取重叠社区,例如Bennet等人的方法或Wang等人的方法。但是,实现它们似乎不太直接。

2
根据这篇博客,networkx现在可以计算重叠社区。
以下代码是用于团伙渗透方法的,可在Networkx 11.6中使用。Github 链接
import networkx as nx
from itertools import combinations

def get_percolated_cliques(G, k):
perc_graph = nx.Graph()
cliques = list(frozenset(c) for c in nx.find_cliques(G) if len(c) >= k)
perc_graph.add_nodes_from(cliques)

# Add an edge in the clique graph for each pair of cliques that percolate
for c1, c2 in combinations(cliques, 2):
    if len(c1.intersection(c2)) >= (k - 1):
        perc_graph.add_edge(c1, c2)

for component in nx.connected_components(perc_graph):
    yield(frozenset.union(*component))

1
CFinder 实现了团聚社区法(Clique Percolation Method (CPM))。如果您使用 Python,Networkx 已经实现了相同的功能(请参阅此链接)。
>>> G = nx.complete_graph(5)
>>> K5 = nx.convert_node_labels_to_integers(G,first_label=2)
>>> G.add_edges_from(K5.edges())
>>> c = list(nx.k_clique_communities(G, 4))
>>> list(c[0])
[0, 1, 2, 3, 4, 5, 6]
>>> list(nx.k_clique_communities(G, 6))

1
Python的networkx库现在具有更广泛的社区检测算法。 Carla给出的示例现在是:
>>> from networkx.algorithms.community import k_clique_communities
>>> G = nx.complete_graph(5)
>>> K5 = nx.convert_node_labels_to_integers(G,first_label=2)
>>> G.add_edges_from(K5.edges())
>>> c = list(k_clique_communities(G, 4))
>>> list(c[0])
[0, 1, 2, 3, 4, 5, 6]
>>> list(k_clique_communities(G, 6))
[]

社区文档在这里: https://networkx.github.io/documentation/latest/reference/algorithms/community.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接