元组知识图谱

knowledge graphs with tuples

我的目标是使用包含源、边缘和目标的 csv 文件创建知识图谱。到目前为止我尝试了什么:

第一张图片是我希望看到的一种格​​式。第二张图片是我的 csv 数据文件的头部,第三张图片显示了由于此代码而失败的图形可视化。

# create a directed-graph from a dataframe
import networkx as nx

G=nx.from_pandas_edgelist(tuple_predictions_IB_for_graph, "source", "target", 
                          edge_attr=True, create_using=nx.MultiDiGraph())
import matplotlib.pyplot as plt


plt.figure(figsize=(12,12))

pos = nx.spring_layout(G)
nx.draw(G, with_labels=True, node_color='skyblue', edge_cmap=plt.cm.Blues, pos = pos)
plt.show()

您应该使用数据框的 explode 方法为行中的每个目标创建一个条目,以便每个目标与其适当的源对齐,然后您将获得所需的节点。

# Make sample data
tuple_predictions_IB_for_graph = pd.DataFrame({'source':['motherboard','screen','keyboard','bluetooth','webcam'],
                                               'edge':['related to']*4+['other_label'],
                                               'target':[['computer','keyboard','mouse','monitor'],
                                                         ['monitor','mouse','computer','tv'],
                                                         ['mouse','keyboard','monitor'],
                                                         ['toothe enamel','tooth decay','tooth fairy'],
                                                         ['webcam','camera','video camera','eyepiece']]})

# Explode the target column
tuple_df_exploded = tuple_predictions_IB_for_graph.explode(column = 'target')
tuple_df_exploded.head()
#         source        edge    target
# 0  motherboard  related to  computer
# 0  motherboard  related to  keyboard
# 0  motherboard  related to     mouse
# 0  motherboard  related to   monitor
# 1       screen  related to   monitor

# Make the graph accepting the 'edge' column as an edge attribute
g = nx.from_pandas_edgelist(tuple_df_exploded,
                            source='source',target='target',edge_attr='edge',
                            create_using=nx.MultiDiGraph())

pos = nx.spring_layout(g)
nx.draw_networkx(g,pos)
# draw the edges to make them a specific color based on the 'edge' attribute from the df
nx.draw_networkx_edges(g,pos,edgelist=g.edges(),
                       edge_color=[{'related to':'black',
                                    'other_label':'red'}[edge_label]
                                   for u,v,edge_label in g.edges(data='edge')]);