用networkx计算加权网络的节点度和邻居节点平均度的时候,节点度计算正确,邻居节点平均度不正确,已经验证过矩阵导入没错。
我计算的加权网络中,
邻居节点平均度average_neighbor_degree是根据二元网络计算的;
邻居节点加权平均度average_degree_connectivity并没有得到字典。
excel中是邻接矩阵
import networkx as nx
import matplotlib.pyplot as plt
import csv
import xlwt
import pandas as pd
df=pd.read_excel('D:\\Documents\\文档\\2009年计算\\2009.xlsx',index_col=0)
#df=df.fillna(0).astype(int)
matrix=df.values
print(matrix)
nodes=list(df.columns)
weight={}
G=nx.Graph()
for i in range(len(matrix)):
for j in range(len(nodes)):
if matrix[i,j]!=0:
G.add_edge(nodes[i],nodes[j],weight=matrix[i,j])
#weight.update({(nodes[i],nodes[j]):matrix[i,j]})
nx.draw_networkx(G, pos=nx.spring_layout(G), node_size=20, node_shape='o', width=1, style='solid', font_size=8)
plt.show()
# 计算度中心度
print("Degree centrality")
d = nx.degree(G,weight="weight")
print(d)
for v in G.nodes():
print(v, d[v])
print("average_neighbor Streng")
c = nx.average_neighbor_degree(G,weight="weight")
print(c)
for v in G.nodes():
print(v, c[v])
print("average_degree Connectivity")
e=nx.average_degree_connectivity(G,weight="weight")
for v in G.nodes():
print(v,e[v])