Python 与igraph或其他图书馆重叠的社区检测
我想在小型网络/图形中检测重叠社区。通过重叠,我的意思是一个节点可以包含在检测算法输出中的多个社区/集群中 我已经研究了当前由igraph提供的各种社区检测算法,但我认为它们都不能处理重叠的社区Python 与igraph或其他图书馆重叠的社区检测,python,graph,igraph,Python,Graph,Igraph,我想在小型网络/图形中检测重叠社区。通过重叠,我的意思是一个节点可以包含在检测算法输出中的多个社区/集群中 我已经研究了当前由igraph提供的各种社区检测算法,但我认为它们都不能处理重叠的社区 理想情况下,我希望能够以编程方式利用Python中此类算法的一些实现。但是,用其他语言实现也可以。如果您不介意使用另一种编程语言,您可以使用(Java),它基于派系渗透(它基本上寻找紧密连接的派系),(C++),它优化了统计度量,当然还有其他 否则,如果您想坚持使用python,还可以通过以下方式应用该
理想情况下,我希望能够以编程方式利用Python中此类算法的一些实现。但是,用其他语言实现也可以。如果您不介意使用另一种编程语言,您可以使用(Java),它基于派系渗透(它基本上寻找紧密连接的派系),(C++),它优化了统计度量,当然还有其他 否则,如果您想坚持使用python,还可以通过以下方式应用该方法:1)将图形转换为线图,2)对其应用常规社区检测方法,例如使用IGRAPHE,以及3)获得重叠社区。将图形转换为线图看起来并不太复杂,而且应该很快,前提是原始图形不太大。无论如何,它将比执行社区检测本身更快 注意,除了Evans&Lambiotte的方法外,还有其他方法可以从常规(互斥社区)方法中获得重叠社区,例如or的方法。然而,实现它们似乎不那么简单。我不久前使用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"])
根据这一点,networkx现在可以计算重叠社区
下面的代码是用于派克渗流方法的,可在Networkx 11.6中获得。Github
实现的CFinder。如果使用python,Networkx已经实现了相同的()
Python
networkx
库现在有更广泛的社区检测算法。卡拉现在给出的例子是:
>>来自networkx.algorithms.community导入k_集团_社区
>>>G=nx.完整的_图(5)
>>>K5=nx。将节点标签转换为整数(G,第一个标签=2)
>>>G.从(K5.edges())添加边
>>>c=名单(k_集团_社区(G,4))
>>>列表(c[0])
[0, 1, 2, 3, 4, 5, 6]
>>>名单(k_集团_社区(G,6))
[]
社区文档如下:
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))
>>> 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))