Skip to main content
 首页 » 编程设计

python之如何从 tsv 文件中找到所有节点三元组(大小为 3 的连通分量)

2024年11月24日12落叶

根据下面给出的矩阵,我必须构建一个网络并找到所有大小为 3 的连通分量。 我使用的数据集是:

0   1   1   0   0   0   0   0   0   0   0 
1   0   0   0   0   0   0   0   0   0   0 
1   0   0   1   0   0   0   0   0   0   0 
0   0   1   0   0   0   0   0   0   0   0 
0   0   0   0   0   0   0   0   0   0   1 
0   0   0   0   0   0   0   0   0   0   0 
0   0   0   0   0   0   0   0   0   0   0 
0   0   0   0   0   0   0   0   0   0   0 
0   0   0   0   0   0   0   0   0   0   0 
0   0   0   0   0   0   0   0   0   0   0 
0   0   0   0   1   0   0   0   0   0   0 

有人可以帮忙吗? 预期的连接三元组将是:-

1   2   3 
1   3   4 
2   1   3 
3   1   4 

我的代码:

import networkx as nx 
from itertools import chain 
import csv 
import numpy as np 
import pandas as pd 
 
adj = [] 
infile="2_mutual_info_adjacency.tsv" 
df=pd.read_csv(infile,delimiter="\t",header=None) 
arr=np.array(df.iloc[0:10,:]) 
arr1=np.array(df.iloc[:,0:10]) 
 
for i in range(arr): 
  for j in range(arr1): 
    if (i,j)==1: 
      for k in range(j+1,arr1): 
        if (i,k)==1: 
          adj.append(i,j,k) 
      for l in range(i+1,arr): 
        if(l,j)==1: 
          adj.append(i,j,l) 

我们将不胜感激。提前谢谢你。

请您参考如下方法:

您可以使用函数 connected_componets() 找到所有连接的组件。随后您可以过滤出由三个节点组成的组件:

import networkx as nx 
import pandas as pd 
from itertools import chain 
 
adj_matrix = [ 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0], 
[0, 0, 1, 0, 0, 1, 0, 0, 0, 0], 
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
] 
 
df = pd.DataFrame(adj_matrix) 
 
G = nx.from_pandas_adjacency(df) 
 
# filter components of size 3 
triplets = [c for c in nx.connected_components(G) if len(c) == 3] 
 
triplets = set(chain.from_iterable(triplets)) 
color = ['lime' if n in triplets else 'pink' for n in  G.nodes()] 
 
# jupyter notebook 
%matplotlib inline 
nx.draw(G, with_labels=True, node_color=color, node_size=1000)