下面的例子给出了用eigenvector_centrality和eigenvector_centrality_numpy得到的不同结果。有没有办法使这种计算更有力?我用的是networkx 2.4,numpy 1.18.5和scipy 1.5.0。
import numpy as np
import networkx as nx
AdjacencyMatrix = {
0: {
1: 0.6,
},
1: {
2: 0,
3: 0,
},
2: {
4: 0.5,
5: 0.5,
},
3: {
6: 0.5,
7: 0.5,
8: 0.5,
},
4: {},
5: {},
6: {},
7: {},
8: {},
}
G = nx.DiGraph()
for nodeID in AdjacencyMatrix.keys():
G.add_node(nodeID)
for k1 in AdjacencyMatrix.keys():
for k2 in AdjacencyMatrix[k1]:
weight = AdjacencyMatrix[k1][k2]
split_factor = len(AdjacencyMatrix[k1])
G.add_edge(k1, k2, weight=weight / split_factor, reciprocal=1.0 / (split_factor * weight) if weight != 0 else np.inf)
eigenvector_centrality = {v[0]: v[1] for v in sorted(nx.eigenvector_centrality(G.reverse() if G.is_directed() else G, max_iter=10000, weight="weight").items(), key=lambda x: x[1], reverse=True)}
print(eigenvector_centrality)
eigenvector_centrality_numpy = {v[0]: v[1] for v in sorted(nx.eigenvector_centrality_numpy(G.reverse() if G.is_directed() else G, max_iter=10000, weight="weight").items(), key=lambda x: x[1], reverse=True)}
print(eigenvector_centrality_numpy)这是我的输出:
{0: 0.6468489798823026, 3: 0.5392481399595738, 2: 0.5392481399595732, 1: 0.0012439403459275048, 4: 0.0012439403459275048, 5: 0.0012439403459275048, 6: 0.0012439403459275048, 7: 0.0012439403459275048, 8: 0.0012439403459275048}
{3: 0.9637027924175013, 0: 0.0031436862826891288, 6: 9.593026373266866e-11, 8: 3.5132785569658154e-11, 4: 1.2627565659784068e-11, 1: 9.433263632036004e-14, 7: -2.6958851817582286e-11, 5: -3.185304797703736e-11, 2: -0.26695888283266833}发布于 2020-06-25 03:24:16
编辑--参见dshult的响应。他是维护/更新networkx的主要人员之一。
我认为这可能是个错误,但不是你想的那样。这个图是有向和无圈的。对于这个图,我不认为有一个非零的特征值。
看起来该算法似乎隐含地假定了一个无向图,或者至少如果它是有向的,它就有循环。如果没有循环的话,算法就会中断。
我将鼓励网络用户更详细地研究这个问题。
实际上,我很惊讶它会聚合到非胖版本。
发布于 2020-06-25 19:00:18
Joel说得对,eigenvector_centrality不是有向无圈图的有用度量。见这种对中心性的精彩描述。对于代码的numpy和非numpy版本来说,这都是无用的。
https://stackoverflow.com/questions/62561548
复制相似问题