Python 如何在Networkx中将节点和边添加到网络分析图中?
我试图学习网络分析,所以我使用希拉里·克林顿的在线电子邮件来了解谁给谁发了电子邮件 我的数据在一本名为hrc_dict的字典中,我有一个发送者和接收者的元组,后面是电子邮件的频率。这是字典的一部分: {('Hillary Clinton','Cheryl Mills'):354,('Hillary Clinton','l'):1,('Linda Dewan','Hillary Clinton'):1,('Hillary Clinton','Capricia Marshall'):9,('Phillip Crowley','Hillary Clinton'):2,('Cheryl Mills','Anne Marie Slaught'):1} 我正在Jupyter中使用Networkx创建一个图形。我的代码如下:Python 如何在Networkx中将节点和边添加到网络分析图中?,python,graph,nodes,networkx,network-analysis,Python,Graph,Nodes,Networkx,Network Analysis,我试图学习网络分析,所以我使用希拉里·克林顿的在线电子邮件来了解谁给谁发了电子邮件 我的数据在一本名为hrc_dict的字典中,我有一个发送者和接收者的元组,后面是电子邮件的频率。这是字典的一部分: {('Hillary Clinton','Cheryl Mills'):354,('Hillary Clinton','l'):1,('Linda Dewan','Hillary Clinton'):1,('Hillary Clinton','Capricia Marshall'):9,('Phil
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from(hrc_dict)
for s, r in hrc_dict:
G.add_edge((s,r), hrc_dict[(s,r)])
G.add_edge((s,r), hrc_dict[(s,r)])
当我调用nx.Graph()时,不会输出任何内容;当我调用G.nodes()时,并不是所有节点都显示出来。我在这里粘贴了一些输出:
[1,
2.
3.
4.
5.
6.
7.
8.
“马克·潘”,
10,
(“托德·斯特恩”、“希拉里·克林顿”),
12,]
当我调用G.edges()时,我得到下面的结果,这似乎是正确的
[(1,('Hillary Clinton','l'),(1,('Linda Dewan','Hillary Clinton'),(1,('Hillary Clinton','Thomas Shannon'),(1,('Cheryl Mills','Anne Marie Slaught'),(1,('Christopher Butzgy','Hillary Clinton'))]
有人知道如何将节点正确添加到图形中吗。我假设每个人都需要是一个节点,那么如何分解元组并分别添加名称呢?边缘显示是否正确,或者我是否需要以不同的方式输入它们?您的问题基本上在这一点上:
G.add_edge((s,r), hrc_dict[(s,r)])
networkx将其解释为“在第一个参数(s,r)
和第二个参数hrc_dict[(s,r)]
之间添加一条边”,因此例如('Hillary Clinton','Cheryl Mills'):354
成为节点('Hillary Clinton','Cheryl Mills')
和节点354
之间的边。相反,尝试
G.add_edge(s, r, count = hrc_dict[(s,r)])
你的问题基本上就在这一点上:
G.add_edge((s,r), hrc_dict[(s,r)])
networkx将其解释为“在第一个参数(s,r)
和第二个参数hrc_dict[(s,r)]
之间添加一条边”,因此例如('Hillary Clinton','Cheryl Mills'):354
成为节点('Hillary Clinton','Cheryl Mills')
和节点354
之间的边。相反,尝试
G.add_edge(s, r, count = hrc_dict[(s,r)])
要将每个人添加为一个节点,还需要从更改
add\u nodes\u的用法
大概是这样的:
srcs, dests = zip(* [(fr, to) for (fr, to) in hrc_dict.keys()])
G.add_nodes_from(srcs+dests)
现在意味着G.nodes()
中的节点列表将是:
['Cheryl Mills',
'Capricia Marshall',
'Anne-Marie Slaughter',
'Phillip Crowley',
'Hillary Clinton',
'l',
'Linda Dewan']
(由于networkx将图形存储为字典,因此不会得到任何重复项)
注意:如果使用下面的方法添加边,则无需首先添加节点——但如果由于某种原因,您可能会有没有邻居的节点(或者只有节点很重要的另一个原因),则此代码将执行此操作
然后根据Joel的回答添加边缘;还要注意属性“weight”的使用,这样布局就可以直接利用信息
import networkx as nx
import matplotlib.pyplot as plt
hrc_dict = {('Hillary Clinton', 'Cheryl Mills'): 355, ('Hillary Clinton', 'l'): 1, ('Linda Dewan', 'Hillary Clinton'): 1, ('Hillary Clinton', 'Capricia Marshall'): 9, ('Phillip Crowley', 'Hillary Clinton'): 2, ('Cheryl Mills', 'Anne-Marie Slaughter'): 1}
G = nx.Graph()
# To add the a node for each of the email parties:
srcs, dests = zip(* [(fr, to) for (fr, to) in hrc_dict.keys()])
G.add_nodes_from(srcs + dests)
# (but it isn't needed IF the following method is used
# to add the edges, since add_edge also creates the nodes if
# they don't yet exist)
# note the use of the attribute "weight" here
for (s,r), count in hrc_dict.items():
G.add_edge(s, r, weight=count)
# produce info to draw:
# a) if weight was used above, spring_layout takes
# into account the edge strengths
pos = nx.spring_layout(G)
# b) specifiy edge labels explicitly
# method from https://groups.google.com/forum/#!topic/networkx-discuss/hw3OVBF8orc
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
# draw it
plt.figure(1);
nx.draw_networkx(G, pos, with_labels=True)
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)
plt.axis('equal') # spring weighting makes more sense this way
plt.show()
这就是我们可能看到的:
要将每个人添加为节点,还需要更改中添加节点的用法
大概是这样的:
srcs, dests = zip(* [(fr, to) for (fr, to) in hrc_dict.keys()])
G.add_nodes_from(srcs+dests)
现在意味着G.nodes()
中的节点列表将是:
['Cheryl Mills',
'Capricia Marshall',
'Anne-Marie Slaughter',
'Phillip Crowley',
'Hillary Clinton',
'l',
'Linda Dewan']
(由于networkx将图形存储为字典,因此不会得到任何重复项)
注意:如果使用下面的方法添加边,则无需首先添加节点——但如果由于某种原因,您可能会有没有邻居的节点(或者只有节点很重要的另一个原因),则此代码将执行此操作
然后根据Joel的回答添加边缘;还要注意属性“weight”的使用,这样布局就可以直接利用信息
import networkx as nx
import matplotlib.pyplot as plt
hrc_dict = {('Hillary Clinton', 'Cheryl Mills'): 355, ('Hillary Clinton', 'l'): 1, ('Linda Dewan', 'Hillary Clinton'): 1, ('Hillary Clinton', 'Capricia Marshall'): 9, ('Phillip Crowley', 'Hillary Clinton'): 2, ('Cheryl Mills', 'Anne-Marie Slaughter'): 1}
G = nx.Graph()
# To add the a node for each of the email parties:
srcs, dests = zip(* [(fr, to) for (fr, to) in hrc_dict.keys()])
G.add_nodes_from(srcs + dests)
# (but it isn't needed IF the following method is used
# to add the edges, since add_edge also creates the nodes if
# they don't yet exist)
# note the use of the attribute "weight" here
for (s,r), count in hrc_dict.items():
G.add_edge(s, r, weight=count)
# produce info to draw:
# a) if weight was used above, spring_layout takes
# into account the edge strengths
pos = nx.spring_layout(G)
# b) specifiy edge labels explicitly
# method from https://groups.google.com/forum/#!topic/networkx-discuss/hw3OVBF8orc
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
# draw it
plt.figure(1);
nx.draw_networkx(G, pos, with_labels=True)
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)
plt.axis('equal') # spring weighting makes more sense this way
plt.show()
这就是我们可能看到的:
你能说说你对G.nodes()
和G.edges()
的输出有什么期望吗?我不太确定。我不熟悉网络分析,但我认为G.nodes()应该只列出节点,因此Hillary Clinton、Mark Penn等G.Edge()应该列出频率。你能说一下G.nodes()
和G.Edge()
的输出是什么吗?我不是很确定。我不熟悉网络分析,但我认为G.nodes()应该只列出节点,所以Hillary Clinton、Mark Penn等。G.edge()应该列出频率。当我用G.add_edge(s,r,count=hrc_dict[(s,r)]添加边时,我会得到一个列表,但没有频率数字:[('Mark Penn',Hillary Clinton'),('Brian Greenspun',Hillary Clinton'),('Esther Brimmer','Hillary Clinton'),('Lee Feinstein','Hillary Clinton'),]仍然没有图形。G.edges(data=True)
将给出边的列表并包含它们的数据。我不知道“仍然没有图形”是什么意思。当我用G.add_edge(s,r,count=hrc_dict[(s,r)]添加边时,我会得到一个列表,但没有频率编号:[('Mark Penn','Hillary Clinton'),('Brian Greenspun','Hillary Clinton'),('Esther Brimmer','Hillary Clinton'),('Lee Feinstein','Hillary Clinton'),]仍然没有图形。G.edges(data=True)
将给出边的列表并包含它们的数据。我不知道你所说的“仍然没有图形”是什么意思。