简体   繁体   English

矩阵熊猫的邻接表

[英]Adjacency list to matrix pandas

I'm trying to get through a toy example of building an adjacency matrix from a list, but already I can't quite figure it out.我正在尝试通过一个从列表构建邻接矩阵的玩具示例,但我已经无法弄清楚了。 I am thinking in terms of .loc() but I'm not sure how to index correctly.我正在考虑 .loc() 但我不确定如何正确索引。

{'nodes':['A', 'B', 'C', 'D', 'E'],
 'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'), 
                      ('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]}

I've started to build the matrix with:我已经开始用以下方法构建矩阵:

n = len(graph['nodes'])
adj_matr = pd.DataFrame(0, columns = graph['nodes'], index = graph['edges'])

but now I'm not sure how to fill it in. I think there's an easy one liner, maybe with a list comprehension?但现在我不知道如何填写它。我认为有一个简单的班轮,也许有列表理解?

Expected output:预期输出:

   A  B  C  D  E
A  0  1  0  1  0
B  0  0  1  0  1
C  0  0  0  1  0
D  0  0  0  0  1
E  1  1  1  0  0

A simple way to obtain the adjacency matrix is by using NetworkX获取邻接矩阵的一种简单方法是使用NetworkX

d = {'nodes':['A', 'B', 'C', 'D', 'E'],
     'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'), 
                      ('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]}

It appears that from your adjacency matrix the graph is directed.从您的邻接矩阵看来,该图是有向的。 You can create a directed graph as shown bellow and define its nodes and edges from the dictionary with:您可以创建如下所示的有向图,并使用以下命令从字典中定义其节点和边:

import networkx as nx
g = nx.DiGraph()
g.add_nodes_from(d['nodes'])
g.add_edges_from(d['edges'])

And then you can obtain the adjacency matrix as a dataframe with nx.to_pandas_adjacency :然后您可以使用nx.to_pandas_adjacency将邻接矩阵作为数据帧nx.to_pandas_adjacency

nx.to_pandas_adjacency(g)

    A    B    C    D    E
A  0.0  1.0  0.0  1.0  0.0
B  0.0  0.0  1.0  0.0  1.0
C  0.0  0.0  0.0  1.0  0.0
D  0.0  0.0  0.0  0.0  1.0
E  1.0  1.0  1.0  0.0  0.0
​

for undirected graph对于无向图

graph = {'nodes': ['A', 'B', 'C', 'D', 'E'],
         'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                   ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]}
n = len(graph['nodes'])
adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
for i in graph['edges']:
    adj_matr.at[i[0], i[1]] = 1
    adj_matr.at[i[1], i[0]] = 1


print(adj_matr)

   A  B  C  D  E
A  0  1  0  1  1
B  1  0  1  0  1
C  0  1  0  1  1
D  1  0  1  0  1
E  1  1  1  1  0

for directed graph:对于有向图:

graph = {'nodes': ['A', 'B', 'C', 'D', 'E'],
         'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                   ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]}
n = len(graph['nodes'])
adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
print(adj_matr)
for i in graph['edges']:
    adj_matr.at[i[0], i[1]] = 1
    # adj_matr.at[i[1], i[0]] = 1

print(adj_matr)

   A  B  C  D  E
A  0  1  0  1  0
B  0  0  1  0  1
C  0  0  0  1  0
D  0  0  0  0  1
E  1  1  1  0  0

For a directed graph you can use:对于有向图,您可以使用:

df = pd.DataFrame(graph['edges'], columns=['From', 'To'])
df['Edge'] = 1
adj = df.pivot(index='From', columns='To', values='Edge').fillna(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM