networkx常用操作记录

地铁数据分析

g.remove_node()#删除节点
lst = list(nx.connected_component_subgraphs(G))#提取连通图,返回一个列表
print(nx.number_of_nodes(G))#节点数
print(nx.number_of_edges(G))#边数
print(nx.average_shortest_path_length(G)) #平均最短路径长度
print(nx.average_clustering(G))#平均聚类系数
degree = nx.degree_histogram(G)#度分布--一个list
print(degree)
print(nx.diameter(G))#网络直径
def ave(degree):#平均度计算
    s_um = 0
    for i in range(len(degree)):
        s_um =s_um+i*degree[i]
    return s_um/nx.number_of_nodes(G)
ave(degree)

画图

x = list(range(len(degree)))
y = [i for i in degree]
plt.bar(x, y, align='center')#plot、loglog
plt.ylim(0, 300)
plt.title('Distribution of Nodes')
plt.xlabel('Degree')
plt.ylabel('Number of Nodes')
for a, b in zip(x,y):
    plt.text(a, b+2,'%.0f' % b, ha='center')
#plt.savefig("degree.png")
plt.show()

计算最短路径概率分布

dis = nx.all_pairs_shortest_path_length(G)#所有节点最短路径
def ss():
    for key in dis.keys():
        yield dis[key].values()
freq = [0 for i in range(45)]
for i in ss():
    for j in i:
        freq[j] +=1
print(freq)#freq是所有最短路径的list
#计算累计分布
y_=[]
for i in range(len(freq)):
    j = sum(freq[:i+1])
    y_.append(j)
y_#最短路径累计---list

你可能感兴趣的:(复杂网络,networkx,networkx,基本操作)