📜  特征向量中心度(中心度)

📅  最后修改于: 2021-05-20 06:43:20             🧑  作者: Mango

在图论中,特征向量中心性(也称为特征中心性)是对网络中节点影响的度量。它基于与高得分节点的连接比与低得分节点的相等连接贡献更大的概念为网络中的所有节点分配相对得分。

Google的PageRank和Katz中心性是特征向量中心性的变体。

使用邻接矩阵找到特征向量中心性

对于给定的图形G:=(V,E)|V|顶点让A = (a_{v,t})是邻接矩阵,即 a_{v,t} = 1如果顶点 v链接到顶点t , 和a_{v,t} = 0除此以外。顶点的相对中心度得分v可以定义为:

x_{v}={\frac {1}{\lambda }}\sum _{t\in M(v)}x_{t}={\frac {1}{\lambda }}\sum _{t\in G}a_{v,t}x_{t}
在哪里M(v)是…的一组邻居v\lambda 是一个常数。只需很小的重新排列,就可以将其用向量符号形式重写为特征向量方程式

\mathbf {Ax} =\lambda \mathbf {x}
通常,会有许多不同的特征值\lambda为此,存在一个非零特征向量解。但是,附加要求特征向量中的所有项都必须为非负值(根据Perron-Frobenius定理),意味着只有最大特征值才能得出所需的中心度度量。这v^{\text{th}}特征向量的相关分量然后给出顶点的相对中心度得分v在网络中。特征向量仅定义为一个公因数,因此仅定义了顶点中心的比率。为了定义绝对分数,必须对特征向量进行归一化,例如使得所有顶点的总和为1或顶点总数为n。幂迭代是可用于查找该主要特征向量的许多特征值算法之一。此外,可以将其概括化,使得A中的条目可以是代表连接强度的实数,如在随机矩阵中一样。

以下是用于计算图及其各个节点的特征向量中心性的代码。

def eigenvector_centrality(G, max_iter=100, tol=1.0e-6, nstart=None,
                           weight='weight'):
    """Compute the eigenvector centrality for the graph G.
  
    Eigenvector centrality computes the centrality for a node based on the
    centrality of its neighbors. The eigenvector centrality for node `i` is
  
    .. math::
  
        \mathbf{Ax} = \lambda \mathbf{x}
  
    where `A` is the adjacency matrix of the graph G with eigenvalue `\lambda`.
    By virtue of the Perron–Frobenius theorem, there is a unique and positive
    solution if `\lambda` is the largest eigenvalue associated with the
    eigenvector of the adjacency matrix `A` ([2]_).
  
    Parameters
    ----------
    G : graph
      A networkx graph
  
    max_iter : integer, optional
      Maximum number of iterations in power method.
  
    tol : float, optional
      Error tolerance used to check convergence in power method iteration.
  
    nstart : dictionary, optional
      Starting value of eigenvector iteration for each node.
  
    weight : None or string, optional
      If None, all edge weights are considered equal.
      Otherwise holds the name of the edge attribute used as weight.
  
    Returns
    -------
    nodes : dictionary
       Dictionary of nodes with eigenvector centrality as the value.
  
        
    Notes
    ------
    The eigenvector calculation is done by the power iteration method and has
    no guarantee of convergence. The iteration will stop after ``max_iter``
    iterations or an error tolerance of ``number_of_nodes(G)*tol`` has been
    reached.
  
    For directed graphs this is "left" eigenvector centrality which corresponds
    to the in-edges in the graph. For out-edges eigenvector centrality
    first reverse the graph with ``G.reverse()``.
  
     
    """
    from math import sqrt
    if type(G) == nx.MultiGraph or type(G) == nx.MultiDiGraph:
        raise nx.NetworkXException("Not defined for multigraphs.")
  
    if len(G) == 0:
        raise nx.NetworkXException("Empty graph.")
  
    if nstart is None:
  
        # choose starting vector with entries of 1/len(G)
        x = dict([(n,1.0/len(G)) for n in G])
    else:
        x = nstart
  
    # normalize starting vector
    s = 1.0/sum(x.values())
    for k in x:
        x[k] *= s
    nnodes = G.number_of_nodes()
  
    # make up to max_iter iterations
    for i in range(max_iter):
        xlast = x
        x = dict.fromkeys(xlast, 0)
  
        # do the multiplication y^T = x^T A
        for n in x:
            for nbr in G[n]:
                x[nbr] += xlast[n] * G[n][nbr].get(weight, 1)
  
        # normalize vector
        try:
            s = 1.0/sqrt(sum(v**2 for v in x.values()))
  
        # this should never be zero?
        except ZeroDivisionError:
            s = 1.0
        for n in x:
            x[n] *= s
  
        # check convergence
        err = sum([abs(x[n]-xlast[n]) for n in x])
        if err < nnodes*tol:
            return x
  
    raise nx.NetworkXError("""eigenvector_centrality():
power iteration failed to converge in %d iterations."%(i+1))""")

上面的函数是使用networkx库调用的,安装该库后,您最终可以使用它,并且以下代码必须用Python编写,以实现节点的特征向量中心性。

>>> import networkx as nx
>>> G = nx.path_graph(4)
>>> centrality = nx.eigenvector_centrality(G)
>>> print(['%s %0.2f'%(node,centrality[node]) for node in centrality])

上面代码的输出是:

['0 0.37', '1 0.60', '2 0.60', '3 0.37']

以上结果是一个字典,描述了每个节点的特征向量中心值。以上是我关于集中度度量的文章系列的扩展。保持联网!!!

参考
您可以在以下位置阅读更多有关此内容的信息

https://en.wikipedia.org/wiki/Eigenvector_centrality

http://networkx.readthedocs.io/en/networkx-1.10/index.html

图片来源
https://image.slidesharecdn.com/srspesceesposito-150425073726-conversion-gate01/95/network-centrality-measures-and-their-efficiency-28-638.jpg?cb=1429948092