如何从字典在 networkx 中创建有向图?
How do I create a directed graph in networkx from a dictionary?
我有大约 1 万篇出版物有入站 or/and 出站引用。
数据采用以下格式(以两个条目为例):
# each 'number' is a 'paper_id'
citations = {
'157553241': {
'inbound_citations': [],
'outbound_citations': [
'141919793',
'158546657',
'156580052',
'159778536',
'157021328',
'158546657',
'157021328',
'141919793',
'153005744',
'159778536',
'112335878',
'156580052'
]
},
'54196724': {
'inbound_citations': ['204753337', '55910675'],
'outbound_citations': ['153776751', '141060228', '33718066', '158233543']
},
}
如何将此格式转换为我可以提供给 networkx
的内容?
我想找到最多 'central' 篇论文并发现一些派系(首先)。
我试过了
G = nx.DiGraph(citations)
但我不认为它是那样工作的...
您需要像这样构建边列表:
import networkx as nx
import matplotlib.pyplot as plt
edges = []
for node in citations:
for parent in citations[node]['inbound_citations']:
edges.append((parent, node))
for child in citations[node]['outbound_citations']:
edges.append((node, child))
G = nx.DiGraph()
G.add_edges_from(edges)
nx.draw(G, with_labels=True)
plt.show()
我有大约 1 万篇出版物有入站 or/and 出站引用。
数据采用以下格式(以两个条目为例):
# each 'number' is a 'paper_id'
citations = {
'157553241': {
'inbound_citations': [],
'outbound_citations': [
'141919793',
'158546657',
'156580052',
'159778536',
'157021328',
'158546657',
'157021328',
'141919793',
'153005744',
'159778536',
'112335878',
'156580052'
]
},
'54196724': {
'inbound_citations': ['204753337', '55910675'],
'outbound_citations': ['153776751', '141060228', '33718066', '158233543']
},
}
如何将此格式转换为我可以提供给 networkx
的内容?
我想找到最多 'central' 篇论文并发现一些派系(首先)。
我试过了
G = nx.DiGraph(citations)
但我不认为它是那样工作的...
您需要像这样构建边列表:
import networkx as nx
import matplotlib.pyplot as plt
edges = []
for node in citations:
for parent in citations[node]['inbound_citations']:
edges.append((parent, node))
for child in citations[node]['outbound_citations']:
edges.append((node, child))
G = nx.DiGraph()
G.add_edges_from(edges)
nx.draw(G, with_labels=True)
plt.show()