我怎样才能增加 networkx 边缘的权重?
how can i add weight to networkx edges?
我有基本的数据框,看起来像这样
A B C D ... Z
foo1 1 0 1 0 ... 0
foo2 0 0 0 1 ... 0
foo3 0 1 0 0 ... 1
foo4 1 0 1 1 ... 0
(实际形状 = 330, 1113)
我将其转换为邻接矩阵
A B C D ... Z
A
B
C
D
..
Z
(实际形状 = 1113, 1113)
这个矩阵只有二进制值,
我可以使用 networkx
获得图形的几个中心性(度数、紧密度、中间度)
然后,我给数据框一些值,比如
A B C D ... Z
foo1 3 0 3 0 ... 0
foo2 0 0 0 2 ... 0
foo3 0 5 0 0 ... 5
foo4 4 0 4 4 ... 0
(实际形状 = 330, 1113 和一行中的值都相同)
我也将其转换为邻接矩阵并计算中心性
但我对二进制值有相同的结果。
这种情况正常吗?
我认为这些中心性会因为重量而有所不同,但事实并非如此。
我希望高值的列(例如A)更居中,但两者的结果相同。
为什么会这样?我该如何解决?
当您使用 nx.from_numpy_array
时,来自 fed 邻接数组的值被设置为边权重,因此应该如您所愿。使用一些示例数组:
df.values
array([[6, 0, 4, 0],
[0, 0, 0, 1],
[0, 2, 0, 0],
[1, 0, 4, 8]])
G = nx.from_numpy_array(df.values)
G.edges(data=True)
#EdgeDataView([(0, 0, {'weight': 1}), (0, 2, {'weight': 1})...
根据 Centrality, for weights to be considered you have to modify the attribute names, so that they are taken into account. For instance, for the closeness_centrality
中可用的各种中心算法,来自文档:
If the ‘distance’ keyword is set to an edge attribute key then the shortest-path length will be computed using Dijkstra’s algorithm with that edge attribute as the edge weight.
下面是我如何计算加权图的介数,其中 G 是使用来自@yatu 的方向创建的,networkx 已导入为 nx:
BC = nx.betweenness_centrality(G, normalized=False, weight='weight')
这是一个使用简单条形图计算和比较 BC 值的函数。参数 "labels" 是标签字典,其中值是字符串,例如某种形式
labels={0:'A',1:'B',2:'C',3:'D'}
def network_betweenness_bar(G, labels):
# Compute Betweenness Centrality Values and plot as bar graph
BC = nx.betweenness_centrality(G, normalized=False, weight='weight')
BC = list(BC.values())
BC = [round(elem, 2) for elem in BC]
x = np.arange(len(labels))
axis_labels = list(labels.values());
plt.bar(x, BC)
plt.xticks(x, axis_labels)
plt.title('Betweenness Centrality', fontsize=18)
return BC
我有基本的数据框,看起来像这样
A B C D ... Z
foo1 1 0 1 0 ... 0
foo2 0 0 0 1 ... 0
foo3 0 1 0 0 ... 1
foo4 1 0 1 1 ... 0
(实际形状 = 330, 1113) 我将其转换为邻接矩阵
A B C D ... Z
A
B
C
D
..
Z
(实际形状 = 1113, 1113)
这个矩阵只有二进制值, 我可以使用 networkx
获得图形的几个中心性(度数、紧密度、中间度)然后,我给数据框一些值,比如
A B C D ... Z
foo1 3 0 3 0 ... 0
foo2 0 0 0 2 ... 0
foo3 0 5 0 0 ... 5
foo4 4 0 4 4 ... 0
(实际形状 = 330, 1113 和一行中的值都相同)
我也将其转换为邻接矩阵并计算中心性 但我对二进制值有相同的结果。
这种情况正常吗? 我认为这些中心性会因为重量而有所不同,但事实并非如此。
我希望高值的列(例如A)更居中,但两者的结果相同。
为什么会这样?我该如何解决?
当您使用 nx.from_numpy_array
时,来自 fed 邻接数组的值被设置为边权重,因此应该如您所愿。使用一些示例数组:
df.values
array([[6, 0, 4, 0],
[0, 0, 0, 1],
[0, 2, 0, 0],
[1, 0, 4, 8]])
G = nx.from_numpy_array(df.values)
G.edges(data=True)
#EdgeDataView([(0, 0, {'weight': 1}), (0, 2, {'weight': 1})...
根据 Centrality, for weights to be considered you have to modify the attribute names, so that they are taken into account. For instance, for the closeness_centrality
中可用的各种中心算法,来自文档:
If the ‘distance’ keyword is set to an edge attribute key then the shortest-path length will be computed using Dijkstra’s algorithm with that edge attribute as the edge weight.
下面是我如何计算加权图的介数,其中 G 是使用来自@yatu 的方向创建的,networkx 已导入为 nx:
BC = nx.betweenness_centrality(G, normalized=False, weight='weight')
这是一个使用简单条形图计算和比较 BC 值的函数。参数 "labels" 是标签字典,其中值是字符串,例如某种形式
labels={0:'A',1:'B',2:'C',3:'D'}
def network_betweenness_bar(G, labels):
# Compute Betweenness Centrality Values and plot as bar graph
BC = nx.betweenness_centrality(G, normalized=False, weight='weight')
BC = list(BC.values())
BC = [round(elem, 2) for elem in BC]
x = np.arange(len(labels))
axis_labels = list(labels.values());
plt.bar(x, BC)
plt.xticks(x, axis_labels)
plt.title('Betweenness Centrality', fontsize=18)
return BC