网络构建期间的关键错误(缺少列)
Key error (missing column) during network build
我正在尝试按照这种方法来识别横向节点。不幸的是我收到了这个错误:
KeyError: 'Source_Attrib'
代码:
import networkx as nx
from matplotlib import pyplot as plt
G = nx.from_pandas_edgelist(df, 'Node', 'Target')
df_pos = nx.spring_layout(G,k = 0.3)
nx.draw_networkx(G, df_pos)
plt.show()
# Iterate through all edges
nx.set_node_attributes(G, labels, "Source_Attrib")
for v, w in G.edges:
print(v)
# Compare `Label` property of edge endpoints
# Set edge `internal` property to True if they match
if G.nodes[v]["Source_Attrib"] == G.nodes[w]["Target_Attrib"]: # Two nodes are directly connected
G.edges[v, w]["internal"] = True
else:
G.edges[v, w]["internal"] = False
数据集:
Source Target Weight Target_Attrib Source_Attrib
143 n1 emerysmith.net 6.9 -0.5 -0.5
155 n2 n3 25.6 -1.0 -1.0
156 n2 n3 15.6 -1.0 -1.0
157 n2 n6 14.5 -1.0 -1.0
158 n3 n2 11.6 -0.5 -1.0
180 n3 n4 25.6 -1.0 -1.0
183 n6 n6 8.6 -0.5 -1.0
184 n6 n3 8.4 -1.0 -1.0
230 n7 n6 11.4 -1.0 -0.5
231 n8 n6 10.9 -1.0 -0.5
我想像这样 post 中的输出可视化:https://www.fatalerrors.org/a/python-networkx-learning-notes.html,其中两个 类 之间的节点被 linked 到不同的 类(在 link 的例子中,这些节点有虚线)。
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-155-25042fae7842> in <module>
6 # Set edge `internal` property to True if they match
----> 7 if G.nodes[v]["Source_Attrib"] == G.nodes[w]["Label_Sim_urls"]: # Two nodes are directly connected
8 G.edges[v, w]["internal"] = True
9 else:
KeyError: 'Source_Attrib'
您目前没有阅读列 source/target 属性。例如,您可以简单地将它们读取为边缘属性,然后在边缘上循环一次:
G = nx.from_pandas_edgelist(df, 'Node', 'Target', edge_attr=True, create_using=nx.DiGraph)
for edge in G.edges:
u, v = edge
G.nodes[u]["Source_Attrib"] = G[u][v]["Source_Attrib"]
G.nodes[v]["Source_Attrib"] = G[u][v]["Target_Attrib"]
假设数据是正确的,因为您可以多次覆盖属性。如果需要,您可能希望将其转换回无向图。
我正在尝试按照这种方法来识别横向节点。不幸的是我收到了这个错误:
KeyError: 'Source_Attrib'
代码:
import networkx as nx
from matplotlib import pyplot as plt
G = nx.from_pandas_edgelist(df, 'Node', 'Target')
df_pos = nx.spring_layout(G,k = 0.3)
nx.draw_networkx(G, df_pos)
plt.show()
# Iterate through all edges
nx.set_node_attributes(G, labels, "Source_Attrib")
for v, w in G.edges:
print(v)
# Compare `Label` property of edge endpoints
# Set edge `internal` property to True if they match
if G.nodes[v]["Source_Attrib"] == G.nodes[w]["Target_Attrib"]: # Two nodes are directly connected
G.edges[v, w]["internal"] = True
else:
G.edges[v, w]["internal"] = False
数据集:
Source Target Weight Target_Attrib Source_Attrib
143 n1 emerysmith.net 6.9 -0.5 -0.5
155 n2 n3 25.6 -1.0 -1.0
156 n2 n3 15.6 -1.0 -1.0
157 n2 n6 14.5 -1.0 -1.0
158 n3 n2 11.6 -0.5 -1.0
180 n3 n4 25.6 -1.0 -1.0
183 n6 n6 8.6 -0.5 -1.0
184 n6 n3 8.4 -1.0 -1.0
230 n7 n6 11.4 -1.0 -0.5
231 n8 n6 10.9 -1.0 -0.5
我想像这样 post 中的输出可视化:https://www.fatalerrors.org/a/python-networkx-learning-notes.html,其中两个 类 之间的节点被 linked 到不同的 类(在 link 的例子中,这些节点有虚线)。
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-155-25042fae7842> in <module>
6 # Set edge `internal` property to True if they match
----> 7 if G.nodes[v]["Source_Attrib"] == G.nodes[w]["Label_Sim_urls"]: # Two nodes are directly connected
8 G.edges[v, w]["internal"] = True
9 else:
KeyError: 'Source_Attrib'
您目前没有阅读列 source/target 属性。例如,您可以简单地将它们读取为边缘属性,然后在边缘上循环一次:
G = nx.from_pandas_edgelist(df, 'Node', 'Target', edge_attr=True, create_using=nx.DiGraph)
for edge in G.edges:
u, v = edge
G.nodes[u]["Source_Attrib"] = G[u][v]["Source_Attrib"]
G.nodes[v]["Source_Attrib"] = G[u][v]["Target_Attrib"]
假设数据是正确的,因为您可以多次覆盖属性。如果需要,您可能希望将其转换回无向图。