如何使用 Python 中的 Katz 索引找到一对顶点之间的相似性?

How to find the similarity between pair of vertices using Katz Index in Python?

我正在尝试使用 Katz 索引查找节点对之间的相似性分数。 例如,如果我有边(V1,V2),他们的 katz 相似度得分是多少?目前我尝试使用如下的 networkx 函数:

import networkx as mx
G=mx.karate_club_graph()
 katz =mx.katz.katz_centrality(G. alpha=0.01, beta=1)

但我得到了每个节点的中心地位。我怎样才能让每对节点都一样?提前谢谢你

此功能在 networkx 中不可用。另一种方法是使用公式计算此分数。详情请参考这篇论文:https://appliednetsci.springeropen.com/articles/10.1007/s41109-018-0080-5

示例代码如下:

import networkx as nx
import numpy as np
from numpy.linalg import inv

G = nx.karate_club_graph()

#Calculate highest eigenvector
L = nx.normalized_laplacian_matrix(G)
e = np.linalg.eigvals(L.A)
print("Largest eigenvalue:", max(e))
beta = 1/max(e)
I = np.identity(len(G.nodes)) #create identity matrix

#Katz score
inv(I - nx.to_numpy_array(G)*beta) - I