networkx 加权图中的中心性
Centralities in networkx weighted graph
我无法计算简单 NetworkX 加权图的中心性。
这是正常的还是我做错了什么?
我用简单的 add_edge(c[0],c[1],weight = my_values)
添加边,其中
c[0],c[1]
是字符串(节点的名称)和 my_values
整数,在 for 循环中。这是生成的边缘的示例:
('first node label', 'second node label', {'weight': 14})
(节点的数量并不重要——现在我只保留 20 个)
我的图表的边缘列表是一个元组列表,带有 (string_node1,string_node2,weight_dictionary) - 一切看起来都很好,因为我也能够 draw/save/read/图表...
为什么?:
nx.degree_centrality
给我所有的 1 ?
nx.closeness_centrality
给我全 1 ?
示例:
{'first node name': 1.0,
...
'last node name': 1.0}
感谢您的帮助。
很简单:
而不是使用 nx.degree_centrality()
我使用
my_graph.degree(weight='weight')
- 我仍然认为这是模块中的基本缺陷...
...但是,nx.closeness_centrality
的问题 仍未解决
为了 closeness_centrality
考虑权重,您必须将 1 / weight
的 distance
属性添加到图形边缘,如 this issue 中所建议。
这是执行此操作的代码(图表是 g
):
g_distance_dict = {(e1, e2): 1 / weight for e1, e2, weight in g.edges(data='weight')}
nx.set_edge_attributes(g, g_distance_dict, 'distance')
我知道这是一个很老的问题,但只是想指出你的度中心性值全为 1 的原因可能是因为你的图是完整的(即所有节点都连接到每个其他节点) , 度中心性指的是图中某个节点所连接的节点所占的比例。
The degree centrality for a node v is the fraction of nodes it is connected to.
The degree centrality values are normalized by dividing by the maximum possible degree in a simple graph n-1 where n is the number of nodes in G.
我无法计算简单 NetworkX 加权图的中心性。
这是正常的还是我做错了什么?
我用简单的 add_edge(c[0],c[1],weight = my_values)
添加边,其中
c[0],c[1]
是字符串(节点的名称)和 my_values
整数,在 for 循环中。这是生成的边缘的示例:
('first node label', 'second node label', {'weight': 14})
(节点的数量并不重要——现在我只保留 20 个)
我的图表的边缘列表是一个元组列表,带有 (string_node1,string_node2,weight_dictionary) - 一切看起来都很好,因为我也能够 draw/save/read/图表...
为什么?:
nx.degree_centrality
给我所有的 1 ?nx.closeness_centrality
给我全 1 ?
示例:
{'first node name': 1.0,
...
'last node name': 1.0}
感谢您的帮助。
很简单:
而不是使用 nx.degree_centrality()
我使用
my_graph.degree(weight='weight')
- 我仍然认为这是模块中的基本缺陷...
...但是,nx.closeness_centrality
为了 closeness_centrality
考虑权重,您必须将 1 / weight
的 distance
属性添加到图形边缘,如 this issue 中所建议。
这是执行此操作的代码(图表是 g
):
g_distance_dict = {(e1, e2): 1 / weight for e1, e2, weight in g.edges(data='weight')}
nx.set_edge_attributes(g, g_distance_dict, 'distance')
我知道这是一个很老的问题,但只是想指出你的度中心性值全为 1 的原因可能是因为你的图是完整的(即所有节点都连接到每个其他节点) , 度中心性指的是图中某个节点所连接的节点所占的比例。
The degree centrality for a node v is the fraction of nodes it is connected to.
The degree centrality values are normalized by dividing by the maximum possible degree in a simple graph n-1 where n is the number of nodes in G.