【发布时间】:2019-01-27 05:52:40
【问题描述】:
我想检测小型网络/图表中的重叠社区。通过重叠,我的意思是在检测算法的输出中,一个节点可以包含在多个社区/集群中。
我查看了igraph 目前提供的各种社区检测算法,但我认为它们都没有处理重叠社区。p>
理想情况下,我希望能够以编程方式在 Python 中利用此类算法的某些实现。但是,其他语言的实现也可以。
【问题讨论】:
我想检测小型网络/图表中的重叠社区。通过重叠,我的意思是在检测算法的输出中,一个节点可以包含在多个社区/集群中。
我查看了igraph 目前提供的各种社区检测算法,但我认为它们都没有处理重叠社区。p>
理想情况下,我希望能够以编程方式在 Python 中利用此类算法的某些实现。但是,其他语言的实现也可以。
【问题讨论】:
我前段时间使用igraph的Python接口实现了Ahn等人的hierarchical link clustering算法;查看其源代码here。
此外,使用 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"])
【讨论】:
如果你不介意使用另一种编程语言,你有 CFinder (Java),它基于 clique 渗透(它基本上寻找紧密连接的 cliques),OSLOM (C++),它优化统计衡量,当然还有其他。
否则,如果你想坚持使用python,你也可以应用Evans & Lambiotte '09的方法:1)将你的图形转换为折线图,2)对其应用常规的社区检测方法,例如使用igraph, 3) 获得重叠社区。将您的图表转换为折线图看起来并不太复杂,而且应该很快,前提是您的原始图表不太大。在任何情况下,它都会比执行社区检测本身更快。
请注意,除了 Evans & Lambiotte 的方法之外,还有其他方法可以从常规(互斥社区)方法中获取重叠社区,例如 Bennet et al. '12 或 Wang et al. '09 的方法。但是,实施它们似乎不那么简单。
【讨论】:
根据blog,networkx 现在可以计算重叠社区。
以下代码用于 clique 渗透方法,可在 Networkx 11.6 中使用。 Githubhere
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))
【讨论】:
实现Clique Percolation Method (CPM) 的CFinder。如果你使用 python,Networkx 已经实现了相同的(see this link)。
>>> 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))
【讨论】:
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
【讨论】: