中心性指标

文章目录

文章介绍了中心性指标,包括公式,代码。

一、度中心性

概念:度中心性衡量一个节点在网络中的直接连接数量,即节点的度。节点的度中心性高表示它在网络中有更多的直接联系。

公式:

   

表示现有的与节点 i 相连的边的数量 N−1 表示节点 i 与其他节点都相连的边的数量

计算度中心性代码如下:

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from scipy.sparse import csr_matrix
# 创建一个无向图
G = nx.Graph()
# 添加节点
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)

# 添加边
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(3, 1)
G.add_edge(1, 4)
G.add_edge(4, 5)
G.add_edge(5, 3)
G.add_edge(3, 6)
# 计算节点的度中心性
print("----------------度中心性-------------------")
degree_centrality = nx.degree_centrality(G)
for node, centrality in degree_centrality.items():
  print(f"Node {node}: Degree Centrality = {centrality}")
# 绘制图形
pos = nx.spring_layout(G)  # 布局算法
nx.draw(G, pos, with_labels=True, node_size=500, node_color='lightblue', font_size=10, font_color='black')
plt.show()

二、介数中心性

概念:介数中心性度量节点在网络中充当中介者的程度,即它在其他节点之间的最短路径上出现的频率。节点的介数中心性高表示它在网络中具有桥梁的作用。

计算公式:

归一化 计算公式:

代码如下(示例):

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from scipy.sparse import csr_matrix
# 创建一个无向图
G = nx.Graph()
# 添加节点
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)

# 添加边
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(3, 1)
G.add_edge(1, 4)
G.add_edge(4, 5)
G.add_edge(5, 3)
G.add_edge(3, 6)
# 计算节点的介数中心性
print("----------------介数中心性-------------------")
betweenness_centrality = nx.betweenness_centrality(G)

# 打印节点的介数中心性
for node, centrality in betweenness_centrality.items():
  print(f"Node {node}: Betweenness Centrality = {centrality}")
# 绘制图形
pos = nx.spring_layout(G)  # 布局算法
nx.draw(G, pos, with_labels=True, node_size=500, node_color='lightblue', font_size=10, font_color='black')
plt.show()

三、接近中心性

概念:接近度中心性度量节点到其他节点的平均最短路径长度。节点的接近度中心性高表示它在网络中更容易与其他节点快速交流和传播信息。

某个节点的 接近中心性 CCi为:

其中       表示节点 i 到其余各点的平均距离,平均距离的倒数就是接近中心度

代码如下(示例):

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from scipy.sparse import csr_matrix
# 创建一个无向图
G = nx.Graph()
# 添加节点
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)

# 添加边
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(3, 1)
G.add_edge(1, 4)
G.add_edge(4, 5)
G.add_edge(5, 3)
G.add_edge(3, 6)
print("----------------接近中心性-------------------")
# 计算节点的接近中心性
closeness_centrality = nx.closeness_centrality(G)

# 打印节点的接近中心性
for node, centrality in closeness_centrality.items():
  print(f"Node {node}: Closeness Centrality = {centrality}")
# 绘制图形
pos = nx.spring_layout(G)  # 布局算法
nx.draw(G, pos, with_labels=True, node_size=500, node_color='lightblue', font_size=10, font_color='black')
plt.show()

四、特征向量中心性

概念:特征向量中心性的基本思想是,一个节点的中心性是相邻节点中心性的函数。也就是说,与你连接的人越重要,你也就越重要。

计算公式:

代码如下(示例):

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from scipy.sparse import csr_matrix
# 创建一个无向图
G = nx.Graph()
# 添加节点
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)

# 添加边
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(3, 1)
G.add_edge(1, 4)
G.add_edge(4, 5)
G.add_edge(5, 3)
G.add_edge(3, 6)
print("----------------特征向量中心性-------------------")
# 获取邻接矩阵
adjacency_matrix = nx.to_numpy_array(G)
# 打印邻接矩阵
print("Adjacency Matrix:")
print(adjacency_matrix)
# 计算特征值和特征向量
eigenvalues, eigenvectors = np.linalg.eig(adjacency_matrix)
# 打印特征向量
print("Eigenvectors of the adjacency matrix:")
print(eigenvectors)
print("Eigenvalues of the adjacency matrix:")
print(eigenvalues)
# 计算节点的特征向量中心性
eigenvector_centrality = nx.eigenvector_centrality(G)
# 绘制图形
pos = nx.spring_layout(G)  # 布局算法
nx.draw(G, pos, with_labels=True, node_size=500, node_color='lightblue', font_size=10, font_color='black')
plt.show()

五、PageRank中心性

概念:PageRank 中心性是一种用于衡量网络中节点重要性的方法,用于对网页进行排名。它基于链接分析的思想,将一个网络视为一个有向图,节点之间的链接表示为有向边。PageRank 中心性的核心思想是,一个网页(或节点)的重要性取决于指向该网页的其他网页的重要性,以及这些网页的重要性值。

代码如下(示例):

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from scipy.sparse import csr_matrix
# 创建一个无向图
G = nx.Graph()
# 添加节点
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)

# 添加边
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(3, 1)
G.add_edge(1, 4)
G.add_edge(4, 5)
G.add_edge(5, 3)
G.add_edge(3, 6)
print("----------------PageRank中心性-------------------")
# 计算节点的 PageRank 中心性
pagerank_centrality = nx.pagerank(G)

# 打印节点的 PageRank 中心性
for node, centrality in pagerank_centrality.items():
  print(f"Node {node}: PageRank Centrality = {centrality}")
# 绘制图形
pos = nx.spring_layout(G)  # 布局算法
nx.draw(G, pos, with_labels=True, node_size=500, node_color='lightblue', font_size=10, font_color='black')
plt.show()

六、Katz中心性

概念:Katz中心性(Katz Centrality)是一种用于衡量网络中节点重要性的中心性指标,它基于节点与其邻居节点之间的关系,以及邻居节点之间的关系来评估节点的重要性。Katz中心性的计算考虑了节点之间的直接连接以及节点之间通过不同路径传播信息的能力。

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from scipy.sparse import csr_matrix
# 创建一个无向图
G = nx.Graph()
# 添加节点
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)

# 添加边
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(3, 1)
G.add_edge(1, 4)
G.add_edge(4, 5)
G.add_edge(5, 3)
G.add_edge(3, 6)

# 计算节点的Katz中心性
katz_centrality = nx.katz_centrality(G, alpha=alpha, beta=beta)
# 打印节点的Katz中心性
for node, centrality in katz_centrality.items():
    print(f"Node {node}: Katz Centrality = {centrality}")

# 绘制图形
pos = nx.spring_layout(G)  # 布局算法
nx.draw(G, pos, with_labels=True, node_size=500, node_color='lightblue', font_size=10, font_color='black')
plt.show()

总结

参考文章:图或网络中的中心性:点度中心性、中介中心性、接近中心性、特征向量中心性、PageRank-CSDN博客

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值