利用network构建多个图并计算聚类系数
代码:
import networkx as nx
import sys
import os
import matplotlib.pyplot as plt
import numpy as np ###################################################################################
#def createGraph(filename) :
G = nx.Graph()
with open(‘C:/Users/lfh98/Desktop/毕业论文/54组数据/54组数据/600/1/DD_A.txt’,“r”) as f: #设置文件对象
#line=f.read()
#print(s)x=[]
x=[]
y=[]
for line in f:
strlist = line.split(’,’)
#print(strlist)
x.append(int(strlist[0])) # 第一部分,即文件中的第一列数据逐一添加到list X 中
y.append(int(strlist[1]))
f.close()
读入列表数据(邻接表数据)
for i in range(len(y)-1):
G.add_edges_from([(x[i],y[i])])
print(x[i], y[i])
#print(‘x’)
if y[i]>y[i+1]:
#nx.draw(G)
#print(“s”)
plt.show()
with open(‘C:/Users/lfh98/Desktop/毕业论文/54组数据/54组数据/600/1/cluster_data60.xls’, ‘a’) as f:
# cs=cs.encode()
cs = nx.clustering(G)
print(cs,file=f)
# print(nx.clustering(G))
G.clear( )
continue