KeyError 是由网络内错误的属性分配引起的吗?

LdM*_*LdM 6 python networkx pandas

我的数据集格式不正确,因为它具有以下列

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
Run Code Online (Sandbox Code Playgroud)

Label_Source 和 Label_Target 是节点属性,Label_Source 是源的属性,而 Label_Target 是目标的属性。尝试复制以下项目:https://www.fatalerrors.org/a/python-networkx-learning-notes.html,我遇到了一些错误,包括由于 Label_Source 导致的 KeyError。正如这个答案中所解释的:KeyError after re-running the (same) code,问题似乎是由边缘/节点属性中的错误分配引起的,因为代码将 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
Run Code Online (Sandbox Code Playgroud)

如果您可以帮助我了解如何解决问题并复制代码,那就太好了。我想错误还在于尝试迭代字符串而不是索引。

Cor*_*ien 5

创建图表后:

G = nx.from_pandas_edgelist(filtered, 'Source', 'Target',  edge_attr=True)
df_pos = nx.spring_layout(G,k = 0.3) 
Run Code Online (Sandbox Code Playgroud)

你有以下属性:

# 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', {})]
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,节点没有属性。您必须将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})]
Run Code Online (Sandbox Code Playgroud)

现在您可以为图表的每个节点设置颜色:

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']
Run Code Online (Sandbox Code Playgroud)

最后一步:

nx.draw_networkx(G, df_pos, label=True, node_color=node_color)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

更新

我认为为节点分配颜色的代码存在一些问题。有些节点的颜色错误(例如,它们应该是绿色,但它们是蓝色)。

问题在于('A', 'N') -> (1, 0)存储为的边,因为您的图没有方向,所以边是或('N', 'A') -> (1, 0)并不重要。如果这对您的问题有意义,您可以通过使用选项创建图表来解决此问题。('A', 'N')('N', 'A')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})]
Run Code Online (Sandbox Code Playgroud)

Label现在,您拥有每个节点的正确属性:

在此输入图像描述