如何获取r/python中三合会的详细信息?

How to get the details of triads in r/python?

我目前正在使用 igraph 使用 triad_census(g) 获取给定有向图的 traid 普查。这 returns 16 类.

中每一个的三合会计数

例如,16 3 0 10 1 0 0 0 0 0 0 0 0 0 0 0

不过,除了这些汇总统计数据,我还想知道更多关于三合会的细节。

即鉴于网络有 16 个 003,它们是什么?假设网络有 3 个 012,它们是什么?

例子: 012 的 3 个 traids 是 (john -> emi, jenne), (cena -> ally, john), (emi -> peter, david)

有没有办法在 r 或 python 中做到这一点?

MWE

图表数据:http://docs.google.com/viewer?a=v&pid=sites&srcid=ZGVmYXVsdGRvbWFpbnxkYWlzaGl6dWthfGd4OmFmZTI0NjhlMjQ0ZDQ5MQ

代码:

library(igraph)
#import the sample_dw_adj.csv file:
dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=FALSE) # read .csv file
m=as.matrix(dat)
net=graph.adjacency(m,mode="directed",weighted=TRUE,diag=FALSE)
plot.igraph(net,vertex.label=V(net)$name,layout=layout.fruchterman.reingold, vertex.label.color="black",edge.color="black",edge.width=E(net)$weight/3, edge.arrow.size=1.5)

因此,我的实际图表如下所示。

如果需要,我很乐意提供更多详细信息。

似乎没有内置方法可以用 Networkx 完成您想要的事情。但是,您可以手动遍历每个三元组并定义它属于哪个class:

from itertools import combinations

triad_class = {}
for nodes in combinations(G.nodes, 3):
    triad_class[nodes] = [k for k, v in nx.triads.triadic_census(G.subgraph(nodes)).items() if v][0]

如果你想要一本以 classes 作为键的字典,你可以尝试这样的事情:

from itertools import combinations

triad_class = {}
for nodes in combinations(G.nodes, 3):
    tc = [k for k, v in nx.triads.triadic_census(G.subgraph(nodes)).items() if v][0]
    triad_class.setdefault(tc, []).append(nodes)