【问题标题】:Differentiate/Partition between nodes of a graph to compute centrality measures in Python在图的节点之间进行区分/分区以计算 Python 中的中心性度量
【发布时间】:2020-09-22 09:27:33
【问题描述】:

我正在使用 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)

我不希望它计算/显示电影的中间性和接近性(欲望之翼、笨蛋迪克斯、Studio Stoops)。我希望它只为演员计算。

【问题讨论】:

    标签: python-3.x networkx imdb


    【解决方案1】:

    对于二分图,您有 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 输入参数必须包含一个二分节点集中的所有节点, 但返回的字典包含来自两个二分节点的所有节点 套。见:mod:bipartite documentation <networkx.algorithms.bipartite> 有关如何在 NetworkX 中处理二分图的更多详细信息。

    【讨论】:

    • 我尝试使用:demo_df.primaryName, demo_df.primaryTitle = bipartite.sets(T) bipartite.centrality.closeness_centrality(T, demo_df.primaryName) 它说“断开连接的图:二分的模糊解决方案集。”
    • 是的 bipartite.sets 如果我没记错的话确实需要连接图表。也许过滤一个分区的节点有效@ninja
    • 它仍然显示电影,但奇怪的是数字不同 - partition = nx.get_node_attributes(T, 'bipartite') actor1 = [node for node, p in partition.items() if p= = 0] Bipartite.Centrality.closenies_Centality(T,Actor1){'Bruno Ganz':1.1428571428571428,'Christine McIntyre':0.9999999999999999,'Chert Bois':1.1428571428571428,'Dopey Dicks':1.7142857142857142,'Larry Feed':0.99999999999999999999999999999999999 Moe Howard:1.2,“Otto Sander”:1.1428571428571428,“Philip Van Zandt”:0.9999999999999999,“Shemp Howard”:0.9999999999999999 ...}
    • 是的,它有点不直观。它在文档中提到,请参阅更新@ninja
    • 谢谢,那么有没有办法过滤结果中的节点? ://
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    相关资源
    最近更新 更多