我正在使用networkx包来分析IMDb数据,以计算中心性(贴近度和介数)。问题是,该图有两种类型的节点-即演员和电影。我只想计算参与者的中心性,而不是整个图的中心性。
代码-
T = nx.Graph()
T.add_nodes_from(demo_df.primaryName,bipartite=1)
T.add_nodes_from(demo_df.primaryTitle,bipartite=0)
T = nx.from_pandas_edgelist(demo_df,'primaryName','primaryTitle')
nx.closeness_centrality(T)
nx.betweenness_centrality(T)
我不想让它计算/显示电影(“欲望之翼”、“笨蛋”、“斯多普斯工作室”)之间的亲密性。我希望它只为演员计算。
发布于 2020-06-03 23:57:58
对于二部图,您可以使用networkx.algorithms.bipartite.centrality
对应的图。例如,对于closeness_centrality
,结果将是一个以节点为关键字的字典,并以二分度中心度作为值。在nodes
参数中,指定一个二分节点集中的节点:
from networkx.algorithms import bipartite
part0_nodes, part1_nodes = bipartite.sets(T)
cs_partition0 = bipartite.centrality.closeness_centrality(T, part0_nodes)
对于断开连接的图,您可以尝试使用以下命令从给定分区获取节点:
partition = nx.get_node_attributes(T, 'bipartite')
part0_nodes = [node for node, p in partition.items() if p==0]
请注意,即使您在nodes
中的一个分区中指定了节点,返回的字典仍将包含所有节点。因此,您只需使用part0_nodes
将它们保存在一个集合中。在notes
部分中提到了这一点:
nodes输入参数必须包含一个二部节点集中的所有节点,但返回的字典包含两个二部节点集中的所有节点。有关如何在NetworkX中处理二部图的更多详情,请参阅:mod:
bipartite documentation <networkx.algorithms.bipartite>
。
https://stackoverflow.com/questions/62176950
复制相似问题