我在加权网络中成功计算Katz中心性时遇到了一些问题,我想看看是否有人在NetworkX中实现了它的例子。我得到负数作为输出。这是我迄今为止的代码:
#katz centrality
G = nx.from_numpy_matrix(network_matrix)
katz_centrality = nx.katz_centrality_numpy(G, weight = 'weight')
for x in range(16):
print(katz_centrality[x])
哪个输出:
-0.0884332150881479
-0.32425466748018883
-0.3110317711173531
-0.3242546674801888
-0.04185470336734943
0.09838584696311473
0.09838584696311474
0.059865838163826485
0.16708256470211458
0.3491707134096127
0.3033563463599785
0.1478838644009215
0.329818599950434
0.3771672006736501
0.35188750514365186
0.1478838644009215
作为参考,network_matrix如下所示:
[[0. 5. 5. 5. 9. 3. 3. 3. 2. 3. 0. 0. 2. 0. 0. 0.]
[5. 0. 7. 9. 4. 2. 2. 2. 1. 0. 1. 0. 0. 0. 0. 0.]
[5. 7. 0. 7. 4. 1. 1. 1. 0. 0. 1. 0. 0. 0. 0. 0.]
[5. 9. 7. 0. 4. 2. 2. 2. 1. 0. 1. 0. 0. 0. 0. 0.]
[9. 4. 4. 4. 0. 2. 2. 2. 1. 4. 0. 0. 2. 0. 0. 0.]
[3. 2. 1. 2. 2. 0. 5. 2. 3. 1. 0. 0. 0. 0. 0. 0.]
[3. 2. 1. 2. 2. 5. 0. 2. 3. 1. 0. 0. 0. 0. 0. 0.]
[3. 2. 1. 2. 2. 2. 2. 0. 2. 1. 0. 0. 0. 0. 0. 0.]
[2. 1. 0. 1. 1. 3. 3. 2. 0. 1. 0. 0. 0. 0. 0. 0.]
[3. 0. 0. 0. 4. 1. 1. 1. 1. 0. 1. 0. 3. 1. 1. 0.]
[0. 1. 1. 1. 0. 0. 0. 0. 0. 1. 0. 0. 1. 3. 2. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[2. 0. 0. 0. 2. 0. 0. 0. 0. 3. 1. 0. 0. 1. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 3. 0. 1. 0. 2. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 2. 0. 1. 2. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
这很奇怪,因为根据我的预测,我的数据显示,输出顶部的集中度应该大于底部的集中度。我认为我错误地实现了Katz中心性,如果有任何帮助,我将不胜感激。
看看这里:https://centrality.mimuw.edu.pl/centrality/katz-代码的问题(以及是什么导致了另一个问题中的错误:Katz Centrality错误:PowerIterationFailedConvergence';功率迭代未能在1000次迭代内收敛';(就是在计算Katz中心性时没有考虑比例因子alpha。由于默认的alpha值是0.1,矩阵的最大特征值是27.88,因此您正在计算的Katz Centralities没有收敛到稳定点(使用迭代方法(,并且在正空间中没有解。如果调整函数的alpha,使其小于1/(最大特征值(,则它将收敛为正向量(请参见Networkx文档(。
请参阅下面的代码示例,了解使用numpy函数的分析方法和使用标准网络函数的迭代方法:
import numpy as np
import networkx as nx
A=np.array([[0, 5, 5, 5, 9, 3, 3, 3, 2, 3, 0, 0, 2, 0, 0, 0,],
[5, 0, 7, 9, 4, 2, 2, 2, 1, 0, 1, 0, 0, 0, 0, 0,],
[5, 7, 0, 7, 4, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0,],
[5, 9, 7, 0, 4, 2, 2, 2, 1, 0, 1, 0, 0, 0, 0, 0,],
[9, 4, 4, 4, 0, 2, 2, 2, 1, 4, 0, 0, 2, 0, 0, 0,],
[3, 2, 1, 2, 2, 0, 5, 2, 3, 1, 0, 0, 0, 0, 0, 0,],
[3, 2, 1, 2, 2, 5, 0, 2, 3, 1, 0, 0, 0, 0, 0, 0,],
[3, 2, 1, 2, 2, 2, 2, 0, 2, 1, 0, 0, 0, 0, 0, 0,],
[2, 1, 0, 1, 1, 3, 3, 2, 0, 1, 0, 0, 0, 0, 0, 0,],
[3, 0, 0, 0, 4, 1, 1, 1, 1, 0, 1, 0, 3, 1, 1, 0,],
[0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 3, 2, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[2, 0, 0, 0, 2, 0, 0, 0, 0, 3, 1, 0, 0, 1, 1, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 1, 0, 2, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 2, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]])
eig, V = np.linalg.eig(A)
max_eig=max(eig)
G = nx.from_numpy_matrix(A)
print('alpha < ', 1/max_eig)
katz_centrality = nx.katz_centrality(G, weight= 'weight', alpha = 1/(max_eig+1),max_iter = 100000)
katz_centrality_numpy = nx.katz_centrality_numpy(G, weight= 'weight', alpha = 1/(max_eig+1))
print("Iteration:",katz_centrality)
print("Analytical:",katz_centrality_numpy)