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

巧了我就是萌 提交于 2019-12-25 03:20:49

问题


I am currently using igraph to get the traid census of a given directed graph usingtriad_census(g). This returns the count of triads in each of the 16 classes.

e.g., 16 3 0 10 1 0 0 0 0 0 0 0 0 0 0 0

However, I would like to know more details of the triads than these summary statistics.

i.e. given that the network has 16 of 003, what they are? given that the network has 3 012, what they are?

Example: The 3 traids of 012 are (john -> emi, jenne), (cena -> ally, john), (emi -> peter, david)

Is there a way of doing this in r or python?

MWE

Graph data: http://docs.google.com/viewer?a=v&pid=sites&srcid=ZGVmYXVsdGRvbWFpbnxkYWlzaGl6dWthfGd4OmFmZTI0NjhlMjQ0ZDQ5MQ

Code:

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)

So, my actual graph would look like as follows.

I am happy to provide more details if needed.


回答1:


There doesn't seem to be a builtin method to accomplish what you want with Networkx. However, you can manually go through each triad and define which class it belongs to:

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]

If you'd rather have a dictionary with the classes as the keys, you can try something like this:

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)


来源:https://stackoverflow.com/questions/54706742/how-to-get-the-details-of-triads-in-r-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!