我有一个庞大的数据集,包含80,000行,我想使用2个数据框(节点和边)在networkx中绘制一个有意义的图
在“节点”中,我有:actor1,category_id(int:从0到7的数字值描述了类型和死亡人数(浮点数代表受伤或丧生的人数))
在“ edges”中:“ actor1”,“ actor2”,“ interaction:float 64”
我的目的是根据category_id和基于死亡人数的不同大小绘制不同颜色的图形
我开始运行完美的代码,直到尝试按以下方式检索交互和死亡事件以计算节点权重
nodes = ACLED_to_graph[['actor1','category_id','fatalities']]
edges = ACLED_to_graph[['actor1','actor2','interaction']]
# Initiate the graph
G4 = nx.Graph()
for index, row in nodes.iterrows():
G4.add_node(row['actor1'], category_id=row['category_id'], nodesize=row['fatalities'])
for index, row in edges.iterrows():
G4.add_weighted_edges_from([(row['actor1'], row['actor2'], row['interaction'])])
#greater_than_ = [x for x in G.nodes(data=True) if x[2]['nodesize']>15]
# Sort nodes by degree
sorted(G4.degree, key=lambda x: x[1], reverse=True)
# remove anonymous nodes whose degree are <2 and <200
cond1 = [node for node,degree in G4.degree() if degree>=200]
cond2 = [node for node,degree in G4.degree() if degree<4]
remove = cond1+cond2
G4.remove_nodes_from(remove)
G4.remove_edges_from(remove)
# Customize the layout
pos=nx.spring_layout(G4, k=0.25, iterations=50)
# Define color map for classes
color_map = {0:'#f1f0c0',1:'#f09494', 2:'#eebcbc', 3:'#72bbd0', 4:'#91f0a1', 5:'#629fff', 6:'#bcc2f2',
7:'#eebcbc' }
plt.figure(figsize=(25,25))
options = {
'edge_color': '#FFDEA2',
'width': 1,
'with_labels': True,
'font_weight': 'regular',
}
colors = [color_map[G4.node[node]['category_id']] for node in G4.node]
#sizes = [G.node[node]['interaction'] for node in G]
"""
Using the spring layout :
- k controls the distance between the nodes and varies between 0 and 1
- iterations is the number of times simulated annealing is run
default k=0.1 and iterations=50
"""
#node_color=colors,
#node_size=sizes,
nx.draw(G4,node_color=colors, **options,cmap=plt.get_cmap('jet'))
ax = plt.gca()
ax.collections[0].set_edgecolor("#555555")
我还将删除一些度数大于200且小于3的节点,以简化图形并使之更具吸引力。
我遇到以下错误:
colors = [color_map[G4.node[node]['category_id']] for node in G4.node]
KeyError: 'category_id'
答案 0 :(得分:0)
在没有输入数据的情况下,很难确定,但是看起来好像您没有在构造带有“ category_id”属性的图节点。在for index, row in nodes.iterrows():
中,您在节点字典中分配数据,将“ category_id”键指定给属性“ group”。
您可以通过检查为图形中的示例节点设置了哪些键来确认情况是否如此。 print(G4.node['actor1 '].keys())
。
要解决此问题,可以 a)更改任务
for index, row in nodes.iterrows():
G4.add_node(row['actor1'], category_id=row['category_id'], nodesize=row['interaction'])
或b)更改查找
colors = [color_map[G4.node[node]['group']] for node in G4.node]
答案 1 :(得分:0)
使用节点属性求解数学运算可总结如下:
nodes = ACLED_to_graph[['actor1','category_id','interaction']]
edges = ACLED_to_graph[['actor1','actor2','fatalities']]
# Initiate the graph
G8 = nx.Graph()
for index, row in edges.iterrows():
G8 = nx.from_pandas_edgelist(edges, 'actor1', 'actor2', ['fatalities'])
for index, row in nodes.iterrows():
G8.add_node(row['actor1'], category_id=row['category_id'], interaction=row['interaction'])
sorted(G8.degree, key=lambda x: x[1], reverse=True)
# remove anonymous nodes whose degree are <2 and <200
cond1 = [node for node,degree in G8.degree() if degree>=200]
cond2 = [node for node,degree in G8.degree() if degree<3]
remove = cond1+cond2
G8.remove_nodes_from(remove)
G8.remove_edges_from(remove)
node_color = [G8.degree(v) for v in G8]
edge_width = [0.15*G8[u][v]['fatalities'] for u,v in G8.edges()]
node_size = [list(nx.get_node_attributes(G8, 'interaction').values()) for v in G8]
我用get.node_attribute代替了pandas来访问功能,这些功能使我可以列出字典并将其转换为值矩阵,以进行计算。
large_edges = [x for x in G8.edges(data=True) if x[2]['fatalities']>=3.0]
nx.draw_networkx(G8, pos, node_size=node_size,node_color=node_color, alpha=0.7, with_labels=False, width=edge_width, edge_color='.4', cmap=plt.cm.Blues)
nx.draw_networkx_edges(G8, pos, edgelist=large_edges, edge_color='r', alpha=0.4, width=6)