网络中错误的属性分配导致的 KeyError?
KeyError caused by a wrong attribute assignment within the network?
我的数据集格式不正确,因为它包含以下列
Source Target Label_Source Label_Target
E N 0.0 0.0
A B 1.0 1.0
A C 1.0 0.0
A D 1.0 0.0
A N 1.0 0.0
S G 0.0 0.0
S L 0.0 1.0
S C 0.0 0.0
Label_Source 和 Label_Target 是节点属性 Label_Source 是源属性,而 Label_Target 是目标属性。
尝试复制以下项目:https://www.fatalerrors.org/a/python-networkx-learning-notes.html, I have encountered some errors, including a KeyError due to Label_Source. As explained in this answer: ,问题似乎是由 edge/node 属性中的错误分配引起的,因为代码正在读取 Label_Source 作为边的属性。
正如我所说,我想复制该项目,因此任何可以使其成为可能的格式都是可以接受的。但是,我真的很感激有人可以解释(不仅是展示)如何解决这个问题,因为我不清楚是什么驱动了它。
我到目前为止所做的如下所示:
import networkx as nx
from matplotlib import pyplot as plt
import pandas as pd
G = nx.from_pandas_edgelist(filtered, 'Source', 'Target', edge_attr=True)
df_pos = nx.spring_layout(G,k = 0.3)
nx.draw_networkx(G, df_pos)
plt.show()
node_color = [
'#1f78b4' if G.nodes[v]["Label_Source"] == 0 # actually this assignment should just Label and it should include also Target, so the whole list of nodes and their labels. A way to address this would be to select all distinct nodes in the network and their labels
else '#33a02c' for v in G]
# Iterate through all edges
for v, w in G.edges:
if G.nodes[v]["Label_Source"] == G.nodes[w]["Label_Source"]: # this should refer to all the Labels
G.edges[v, w]["internal"] = True
else:
G.edges[v, w]["internal"] = False
如果您能帮助我了解如何解决问题并复制代码,那就太好了。我猜错误还在于尝试遍历字符串而不是索引。
创建图表后:
G = nx.from_pandas_edgelist(filtered, 'Source', 'Target', edge_attr=True)
df_pos = nx.spring_layout(G,k = 0.3)
您具有以下属性:
# For edges:
print(G.edges(data=True))
[('E', 'N', {'Label_Source': 0.0, 'Label_Target': 0.0}),
('N', 'A', {'Label_Source': 1.0, 'Label_Target': 0.0}), # Problem here
('A', 'B', {'Label_Source': 1.0, 'Label_Target': 1.0}),
('A', 'C', {'Label_Source': 1.0, 'Label_Target': 0.0}),
('A', 'D', {'Label_Source': 1.0, 'Label_Target': 0.0}),
('C', 'S', {'Label_Source': 0.0, 'Label_Target': 0.0}),
('S', 'G', {'Label_Source': 0.0, 'Label_Target': 0.0}),
('S', 'L', {'Label_Source': 0.0, 'Label_Target': 1.0})]
# For nodes:
print(G.nodes(data=True))
[('E', {}), ('N', {}), ('A', {}), ('B', {}),
('C', {}), ('D', {}), ('S', {}), ('G', {}), ('L', {})]
如您所见,节点没有属性。您必须将 Label_xxx
值从边属性复制到右节点:
# Don't use it, check update below
for source, target, attribs in G.edges(data=True):
G.nodes[source]['Label'] = int(attribs['Label_Source'])
G.nodes[target]['Label'] = int(attribs['Label_Target'])
print(G.nodes(data=True))
[('E', {'Label': 0}), ('N', {'Label': 1}), ('A', {'Label': 1}),
('B', {'Label': 1}), ('C', {'Label': 0}), ('D', {'Label': 0}),
('S', {'Label': 0}), ('G', {'Label': 0}), ('L', {'Label': 1})]
现在您可以为图形的每个节点设置颜色:
node_color = ['#1f78b4' if v == 0 else '#33a02c'
for v in nx.get_node_attributes(G, 'Label').values()]
print(node_color)
['#1f78b4', '#33a02c', '#33a02c', '#33a02c',
'#1f78b4', '#1f78b4', '#1f78b4', '#1f78b4', '#33a02c']
最后一步:
nx.draw_networkx(G, df_pos, label=True, node_color=node_color)
plt.show()
更新
I think there is some problem with the code for assigning the color to nodes. Some nodes have a wrong color (e.g., they should be green and they are blue).
问题出在存储为 ('N', 'A') -> (1, 0)
的边 ('A', 'N') -> (1, 0)
上,因为你的图形没有方向,所以边是 ('A', 'N')
还是 [=20 并不重要=].如果这对您的问题有意义,您可以通过使用选项 create_using=nx.DiGraph
创建图形来解决此问题。
另一种解决方案是创建 Label
属性,而不是从边缘属性而是从你的数据框创建,就像我的 建议的那样:
for _, sr in df.iterrows():
G.nodes[sr['Source']]['Label'] = int(sr['Label_Source'])
G.nodes[sr['Target']]['Label'] = int(sr['Label_Target'])
print(G.nodes(data=True))
[('E', {'Label': 0}), ('N', {'Label': 0}), ('A', {'Label': 1}),
('B', {'Label': 1}), ('C', {'Label': 0}), ('D', {'Label': 0}),
('S', {'Label': 0}), ('G', {'Label': 0}), ('L', {'Label': 1})]
现在,每个节点都有正确的 Label
属性:
我的数据集格式不正确,因为它包含以下列
Source Target Label_Source Label_Target
E N 0.0 0.0
A B 1.0 1.0
A C 1.0 0.0
A D 1.0 0.0
A N 1.0 0.0
S G 0.0 0.0
S L 0.0 1.0
S C 0.0 0.0
Label_Source 和 Label_Target 是节点属性 Label_Source 是源属性,而 Label_Target 是目标属性。
尝试复制以下项目:https://www.fatalerrors.org/a/python-networkx-learning-notes.html, I have encountered some errors, including a KeyError due to Label_Source. As explained in this answer:
import networkx as nx
from matplotlib import pyplot as plt
import pandas as pd
G = nx.from_pandas_edgelist(filtered, 'Source', 'Target', edge_attr=True)
df_pos = nx.spring_layout(G,k = 0.3)
nx.draw_networkx(G, df_pos)
plt.show()
node_color = [
'#1f78b4' if G.nodes[v]["Label_Source"] == 0 # actually this assignment should just Label and it should include also Target, so the whole list of nodes and their labels. A way to address this would be to select all distinct nodes in the network and their labels
else '#33a02c' for v in G]
# Iterate through all edges
for v, w in G.edges:
if G.nodes[v]["Label_Source"] == G.nodes[w]["Label_Source"]: # this should refer to all the Labels
G.edges[v, w]["internal"] = True
else:
G.edges[v, w]["internal"] = False
如果您能帮助我了解如何解决问题并复制代码,那就太好了。我猜错误还在于尝试遍历字符串而不是索引。
创建图表后:
G = nx.from_pandas_edgelist(filtered, 'Source', 'Target', edge_attr=True)
df_pos = nx.spring_layout(G,k = 0.3)
您具有以下属性:
# For edges:
print(G.edges(data=True))
[('E', 'N', {'Label_Source': 0.0, 'Label_Target': 0.0}),
('N', 'A', {'Label_Source': 1.0, 'Label_Target': 0.0}), # Problem here
('A', 'B', {'Label_Source': 1.0, 'Label_Target': 1.0}),
('A', 'C', {'Label_Source': 1.0, 'Label_Target': 0.0}),
('A', 'D', {'Label_Source': 1.0, 'Label_Target': 0.0}),
('C', 'S', {'Label_Source': 0.0, 'Label_Target': 0.0}),
('S', 'G', {'Label_Source': 0.0, 'Label_Target': 0.0}),
('S', 'L', {'Label_Source': 0.0, 'Label_Target': 1.0})]
# For nodes:
print(G.nodes(data=True))
[('E', {}), ('N', {}), ('A', {}), ('B', {}),
('C', {}), ('D', {}), ('S', {}), ('G', {}), ('L', {})]
如您所见,节点没有属性。您必须将 Label_xxx
值从边属性复制到右节点:
# Don't use it, check update below
for source, target, attribs in G.edges(data=True):
G.nodes[source]['Label'] = int(attribs['Label_Source'])
G.nodes[target]['Label'] = int(attribs['Label_Target'])
print(G.nodes(data=True))
[('E', {'Label': 0}), ('N', {'Label': 1}), ('A', {'Label': 1}),
('B', {'Label': 1}), ('C', {'Label': 0}), ('D', {'Label': 0}),
('S', {'Label': 0}), ('G', {'Label': 0}), ('L', {'Label': 1})]
现在您可以为图形的每个节点设置颜色:
node_color = ['#1f78b4' if v == 0 else '#33a02c'
for v in nx.get_node_attributes(G, 'Label').values()]
print(node_color)
['#1f78b4', '#33a02c', '#33a02c', '#33a02c',
'#1f78b4', '#1f78b4', '#1f78b4', '#1f78b4', '#33a02c']
最后一步:
nx.draw_networkx(G, df_pos, label=True, node_color=node_color)
plt.show()
更新
I think there is some problem with the code for assigning the color to nodes. Some nodes have a wrong color (e.g., they should be green and they are blue).
问题出在存储为 ('N', 'A') -> (1, 0)
的边 ('A', 'N') -> (1, 0)
上,因为你的图形没有方向,所以边是 ('A', 'N')
还是 [=20 并不重要=].如果这对您的问题有意义,您可以通过使用选项 create_using=nx.DiGraph
创建图形来解决此问题。
另一种解决方案是创建 Label
属性,而不是从边缘属性而是从你的数据框创建,就像我的
for _, sr in df.iterrows():
G.nodes[sr['Source']]['Label'] = int(sr['Label_Source'])
G.nodes[sr['Target']]['Label'] = int(sr['Label_Target'])
print(G.nodes(data=True))
[('E', {'Label': 0}), ('N', {'Label': 0}), ('A', {'Label': 1}),
('B', {'Label': 1}), ('C', {'Label': 0}), ('D', {'Label': 0}),
('S', {'Label': 0}), ('G', {'Label': 0}), ('L', {'Label': 1})]
现在,每个节点都有正确的 Label
属性: