微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

networkx靠近中心性,出了点问题

如何解决networkx靠近中心性,出了点问题

我有一个国家之间贸易的数据集。 我试图从中获得中心地位。

但是,我被困在closeness_centrality中。好吧,如果我必须说的话,加权贴心为中心。

一个国家没有外发优势,只有其他国家的外发优势。所以outdegree_centrality和outward_closeness_centrality给我显示0。这很好。加权的outdegree_centrality也显示0,但weighted_outward_closeness_centrality却不显示0。另一方面,weighted_inward_closeness_centrality却显示0(而其他中心显示的结果不是0,因为它具有入站边缘)。

这是我要做代码

graph = nx.from_pandas_edgelist(df,'rep','par',create_using=nx.DiGraph(),edge_attr='weight')
r_graph = graph.reverse()
graph2 = nx.from_pandas_edgelist(df,edge_attr='weight2')
r_graph2 = graph2.reverse()
#weight is normal weight,and weight2 is "1/weight" to use in distance


ic_c = nx.closeness_centrality(graph)
df2 = pd.DataFrame(list(ic_c.items()),columns =['Nation','Value'])
df2.to_excel("in_closeness_centrality.xlsx")

oc_c = nx.closeness_centrality(r_graph)
df2 = pd.DataFrame(list(oc_c.items()),'Value'])
df2.to_excel("out_closeness_centrality.xlsx")


wi_c_c = nx.closeness_centrality(graph2,distance = 'weight2')
df2 = pd.DataFrame(list(wi_c_c.items()),'Value'])
print(df2.head())
df2.to_excel("weighted_in_closeness_centrality.xlsx")

wo_c_c = nx.closeness_centrality(r_graph2,distance = 'weight2')
df2 = pd.DataFrame(list(wo_c_c.items()),'Value'])
df2.to_excel("weighted_out_closeness_centrality.xlsx")

就像我说的那样,未加权的紧密中心似乎很好。中心值也很有意义。 但是正如我所说,加权的紧密中心似乎是...反转(向外-向内)。而且,该值似乎也不正确。所有值均具有较低的值(大部分在5.37615896249763E-06附近)。在这些排名中,排名也不正确。

我认为这是我对距离的错误处理。就像我说的,我们有一个权重,我们把1 /权重作为距离。应该是相对的...

我做错了什么?欢迎任何启发。 感谢您阅读并对此感兴趣。 祝你有美好的一天。

编辑:我测试的权重为所有边缘都包含1。显然,它给我的价值与未加权的价值完全相同。但这仍然给我一个错误的方向。所以我认为这是一个错误。 networkx表示它计算“向内” closeness_centrality,但是当我使用“距离”选项时,它将计算“向外”。 (也许是在使用djikstra算法时发生)

另外,我正在使用networkx 2.5

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。