特征向量中心性numpy的不同结果 [英] Different results in eigenvector centrality numpy

查看:142
本文介绍了特征向量中心性numpy的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例给出了使用 eigenvector_centralityeigenvector_centrality_numpy 获得的不同结果.有没有办法让这种计算更加健壮?我正在使用 networkx 2.4numpy 1.18.5scipy 1.5.0.

将 numpy 导入为 np将 networkx 导入为 nx邻接矩阵 = {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()对于 AdjacencyMatrix.keys() 中的 nodeID:G.add_node(nodeID)对于 AdjacencyMatrix.keys() 中的 k1:对于 AdjacencyMatrix[k1] 中的 k2:权重 = 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)}打印(特征向量中心性)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)}打印(特征向量_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}

解决方案

edit - 查看 dshult 的回复.他是维护/更新networkx的主要人员之一.


我认为这可能是一个错误,但不是您想的那样.该图是有向无环图.所以对于这个图,我不认为存在非零特征值.

看起来该算法似乎隐含地假设了一个无向图,或者至少如果它是有向的,它就有循环.如果没有循环,我希望算法会崩溃.

我将鼓励 networkx 人员更详细地研究这一点.

对于非 numpy 版本它会收敛,我真的很惊讶.

The following example gives different results obtained with eigenvector_centrality and eigenvector_centrality_numpy. Is there a way to make such calculation more robust? I'm using networkx 2.4, numpy 1.18.5 and 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)

Here's my output:

{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}

解决方案

edit - see the response by dshult. He's one of the main people who maintains/updates networkx.


I think this may be a bug, but not the way you think. This graph is directed and acyclic. So for this graph, I don't think there is a nonzero eigenvalue.

It looks like the algorithm seems to implicitly assume an undirected graph, or at least that if it's directed it has cycles. And I would expect the algorithm to break if there's no cycle.

I'm going to encourage the networkx people to look at this in more detail.

I'm actually surprised that it converges for the non-numpy version.

这篇关于特征向量中心性numpy的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆