一、几种常见的中心性指标
1.1 度中心性
度中心性分为节点度中心性和网络度中心性。前者指的是节点在其与之直接相连的邻居节点当中的中心程度,二后者则侧重节点在整个网络的中心程度,表征的是整个网络的集中或集权程度,即整个网络围绕一个节点或一组节点来组织运行的程度。
节点的度中心性定义为:
1.2 介数中心性
介数中心性分为节点介数中心性和网络介数中心性。
节点的介数中心性定义为
1.3 接近度中心性
节点的接近度中心性定义为
1.4 特征向量中心性
二、网络基本几何特征实践
import networkx as nxG1 = nx.barabasi_albert_graph(1000,3)
print("网络的直径为:", nx.diameter(G1))#指定某两个节点之间的效率
print(nx.efficiency(G1,1,5))
#指定某两个节点之间的平均效率
print(nx.average_efficiency(G1))
#最短路径长度
print(nx.shortest_path_length(G1,1,5))
#局部效率
print(nx.local_efficiency(G1))
#全局效率
print(nx.global_efficiency(G1))
#求整个网络的平均距离
print(nx.average_shortest_path_length(G1))
#整个网络的集聚系数
print(nx.clustering(G1))
#平均集聚系数
print(nx.average_clustering(G1))
#全局集聚系数
print(nx.transitivity(G1))
三、度-度关联性编程
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd#加载三个数据集
df1 = pd.read_excel("D:/python项目/Symbolic/Net/citation.xlsx")
G1 = nx.from_pandas_edgelist(df1,'source','target',create_using=nx.Graph())df2 = pd.read_excel("D:/python项目/Symbolic/Net/power.xlsx")
G2 = nx.from_pandas_edgelist(df2,'source','target',create_using=nx.Graph())df3 = pd.read_excel("D:/python项目/Symbolic/Net/celegans_metabolic.xlsx")
G3 = nx.from_pandas_edgelist(df3,'source','target',create_using=nx.Graph())#定义求最近邻平均度的函数
def average_neighbor_degree(G):k = set([G.degree(i) for i in G.nodes()]) # 获取所有可能的度值sorted_k = sorted(k)knni = nx.average_neighbor_degree(G)k_nn_k = []for ki in sorted_k:if ki == 0:k_nn_k.append(0.0)else:c = 0s = 0for i in G.nodes():if G.degree(i) == ki:s += knni[i]c += 1k_nn_k.append(s / c)return sorted_k, k_nn_k#基于Pearson相关系数的度-度相关性
r1 = nx.degree_assortativity_coefficient(G1)
r2 = nx.degree_assortativity_coefficient(G2)
r3 = nx.degree_assortativity_coefficient(G3)
print(r1)
print(r2)
print(r3)x1,y1 = average_neighbor_degree(G1)
x2,y2 = average_neighbor_degree(G2)
x3,y3 = average_neighbor_degree(G3)
plt.figure(figsize=(12,4))
plt.subplot(131)
plt.plot(x1, y1, 'ro', label='r = '+'%.4f'%r1)
plt.legend(loc=0)
plt.xlabel("$k$")
plt.ylabel("$k_{nn}(k)$")
plt.xscale("log")
plt.yscale("log")
plt.title('citation')
plt.ylim([1,100])plt.subplot(132)
plt.plot(x2, y2, 'bs', label='r = '+'%.4f'%r2)
plt.legend(loc=0)
plt.xlabel("$k$")
plt.ylabel("$k_{nn}(k)$")
plt.xscale("log")
plt.yscale("log")
plt.title('power')
plt.ylim([1,10])plt.subplot(133)
plt.plot(x3, y3, 'gv', label='r = '+'%.4f'%r3)
plt.legend(loc=0)
plt.xlabel("$k$")
plt.ylabel("$k_{nn}(k)$")
plt.xscale("log")
plt.yscale("log")
plt.title('celegans_metabolic')
plt.ylim([1,100])plt.tight_layout()
plt.show()
四、网络中心性指标编程
import networkx as nx
import matplotlib.pyplot as pltBA = nx.barabasi_albert_graph(20, 2)
#节点介数
bc = nx.betweenness_centrality(BA)
print(bc)#获取介数最大的节点标签
max_id = max(bc,key = bc.get)
print(max_id)
#绘制网络图
nx.draw(BA,node_size = 500,with_labels=True)
#边介数
ebc = nx.edge_betweenness_centrality(BA)
print(ebc)#核度
ks = nx.core_number(BA)
print(ks)
#获取核度最大的节点标签
max_id = max(ks,key = ks.get)
print(max_id)
#绘制网络图
nx.draw(BA,node_size = 500,with_labels=True)
#网络密度
print(nx.density(BA))#几种常用的中心性指标
#生成ER和BA无标度网络,节点数设定为N = 100
GER = nx.erdos_renyi_graph(100,0.08)
GBA = nx.barabasi_albert_graph(100,4)
#度中心性
dc1 = nx.degree_centrality(GER)
dc2 = nx.degree_centrality(GBA)
#介数中心性
bc1 = nx.betweenness_centrality(GER)
bc2 = nx.betweenness_centrality(GBA)
#接近度中心性
cc1 = nx.closeness_centrality(GER)
cc2 = nx.closeness_centrality(GBA)
#特征向量中心性
ec1 = nx.eigenvector_centrality(GER)
ec2 = nx.eigenvector_centrality(GBA)
五、有向网络与加权网络
import networkx as nx
import matplotlib.pyplot as plt#创建一个有向网络
DG = nx.DiGraph()
DG.add_nodes_from([1,2,3,4,5,6])
DG.add_edges_from([(1,2),(1,3),(2,4),(3,5),(4,6),(5,2),(5,4),(5,3),(3,6)])
nx.draw(DG,node_size = 500,with_labels=True)
plt.show()#获得各个结点的入度、出度和总度
print(DG.in_degree)
print(DG.out_degree)
print(DG.degree)#创建一个无向加权网络
WG = nx.Graph()
WG.add_nodes_from([1,2,3,4,5,6])
WG.add_weighted_edges_from([(1,2,1),(1,3,8),(2,4,3),(3,5,9),(4,6,4),(5,2,6),(5,4,0.7),(5,3,0.5),(3,6,0.2)])
w = [WG[e[0]][e[1]]['weight'] for e in WG.edges]
nx.draw(WG,node_size = 500,with_labels=True)
plt.show()