我正在尝试通过一个玩具示例,从列表中构建邻接矩阵,但现在我还不太清楚。我在考虑.loc()的问题,但不确定如何正确编制索引。
<!DISCODE HTML>
<html lang="pl">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<link rel="stylesheet" href="style.css" type="text/css" />
<title>Piotr Nowak - Portfolio</title>
<meta name="description" content="piotr nowak, programista backendowy freelancer" />
<meta name="keywords" content="programista, backend, freelancer, portfolio, web" />
<link href="https://fonts.googleapis.com/css?family=Abel" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
</head>
<body>
<div class="flexbox-container">
<header>
<div class="topbar">
<h1><a href="index.html">ZlotyKot Trafika</a></h1>
</div>
</header>
<nav>
<div class="menu">
<ul>
<li><a href="aktualnosci.html" alt="_blank">Aktualności</a></li>
<li><a href="oferta.html" alt="_blank">Oferta</a></li>
<li><a href="nowosci.html" alt="_blank">Nowości</a></li>
<li><a href="kurier.html" alt="_blank">Kurier</a></li>
<li><a href="fotogaleria.html" alt="_blank">Fotogaleria</a></li>
<li><a href="kontakt.html" alt="_blank">Kontakt</a></li>
</ul>
</div>
</nav>
<main>
<div class="content">
<h1>Lorem ipsum. </h1>
<br> </br>
<br> </br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In at augue sollicitudin, malesuada lorem id, gravida arcu. Nam metus arcu, elementum vel vulputate in, hendrerit et turpis. Morbi mattis purus ipsum, vitae blandit orci accumsan vel. Aliquam varius eleifend euismod. Curabitur varius fermentum massa, in finibus lacus cursus rutrum. Aenean quis semper nisi. In id fringilla mauris. Proin et massa lobortis, blandit massa sit amet, congue ante. Aliquam erat volutpat. <br> </br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In at augue sollicitudin, malesuada lorem id, gravida arcu. Nam metus arcu, elementum vel vulputate in, hendrerit et turpis. Morbi mattis purus ipsum, vitae blandit orci accumsan vel. Aliquam varius eleifend euismod. Curabitur varius fermentum massa, in finibus lacus cursus rutrum. Aenean quis semper nisi. In id fringilla mauris. Proin et massa lobortis, blandit massa sit amet, congue ante. Aliquam erat volutpat. <br> </br>
Interdum et malesuada fames ac ante ipsum primis in faucibus. Maecenas fermentum malesuada orci. Suspendisse nec quam vitae ex lacinia tincidunt in sit amet felis. Cras ac varius neque. Donec ullamcorper, diam id blandit blandit, mi massa consectetur nibh, eget porttitor nibh diam quis felis. Donec arcu neque, vulputate quis felis a, tincidunt tempus sapien. Aliquam tortor nulla, porta vitae eros sit amet, fermentum maximus mi. Morbi ac tincidunt lacus, maximus ultrices eros. In vehicula tempus dictum.
Mauris quis aliquam magna. Morbi diam nisl, pharetra sed suscipit vel, dictum eu enim. Sed eu risus non ex porta venenatis elementum ultrices augue. Nam non vulputate ante, eget porttitor ante. Fusce eget lectus in orci ullamcorper sollicitudin a id erat. Morbi vestibulum risus sed iaculis dictum. Aliquam id malesuada est. Donec venenatis commodo turpis sed placerat. Curabitur vulputate varius ligula non feugiat. Cras vulputate faucibus odio a posuere.
</div>
</main>
<div class="sidebar">
</div>
</div>
</body>
</html>
我开始使用以下方法构建矩阵:
{'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['edges'])
答案 0 :(得分:2)
获取邻接矩阵的一种简单方法是使用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')]}
从您的邻接矩阵看来,该图是有向的。您可以创建如下所示的有向图,并使用以下命令从字典中定义其节点和边:
import networkx as nx
g = nx.DiGraph()
g.add_nodes_from(d['nodes'])
g.add_edges_from(d['edges'])
然后您可以使用nx.adjacency_matrix
从网络中获得邻接矩阵:
m = nx.adjacency_matrix(g)
m.todense()
matrix([[0, 1, 0, 1, 0],
[0, 0, 1, 0, 1],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1],
[1, 1, 1, 0, 0]], dtype=int64)
对于以相应节点为列的数据框,您可以执行以下操作:
pd.DataFrame(m.todense(), columns=nx.nodes(g))
A B C D E
0 0 1 0 1 0
1 0 0 1 0 1
2 0 0 0 1 0
3 0 0 0 0 1
4 1 1 1 0 0
答案 1 :(得分:1)
用于无向图
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
有向图:
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
答案 2 :(得分:0)
对于有向图,您可以使用:
df = pd.DataFrame(graph['edges'], columns=['From', 'To'])
df['Edge'] = 1
adj = df.pivot(index='From', columns='To', values='Edge').fillna(0)