python生成一个图以及计算图的一些网络指标

生成一个图

test.csv文件有两列
第一列的表头是id
第二列的表头是rb
`

import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import csv 
filepath="C:\\Users\\t\\Desktop\\test.csv"
df = pd.read_csv(filepath) #返回一个DataFrame的对象,这个是pandas的一个数据结构
df.columns=["id","rb"]
F=df["id"] 
T=df["rb"]
F = np.array(F) 
T = np.array(T)
df = pd.DataFrame({
      'from':F, 'to':T})
 
# Build your graph
G=nx.from_pandas_edgelist(df, 'from', 'to')
# Fruchterman Reingold Fruchterman Reingold引导布局算法布局
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.fruchterman_reingold_layout(G))
plt.title("fruchterman_reingold")
plt.show()

网络指标

test.csv文件有两列
第一列的表头是id
第二列的表头是rb

import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import csv 
filepath="C:\\Users\\t\\Desktop\\test.csv"
df = pd.read_csv(filepath) #返回一个DataFrame的对象,这个是pandas的一个数据结构
df.columns=["id","rb"]
F=df["id"] 
T=df["rb"]
F = np.array(F) 
T = np.array(T)
df = pd.DataFrame({
      'from':F, 'to':T})
G=nx.from_pandas_edgelist(df, 'from', 'to',create_using = nx.DiGraph())
#print(nx.average_clustering(G))#整个图的聚集系数
#print(nx.average_shortest_path_length(G))#图的平均路径长度
#print(nx.to_numpy_matrix(G))#打印图的邻接矩阵
#print(G.degree()) #节点的度
#print(nx.clustering(G))#节点的聚类系数
#print(nx.number_connected_components(G))#图的连通分支
#print("节点度中心系数",nx.degree_centrality(G))#节点度中心系数  已归一化
#print("---------------------------------------")
#print("节点距离中心系数",nx.closeness_centrality(G))#节点距离中心系数
#print("---------------------------------------")
#print("节点介数中心系数",nx.betweenness_centrality(G))#节点介数中心系数
#print(nx.transitivity(G))#图或网络的传递性。
#-----------------------------------------------------------------------
#degree =  nx.degree_histogram(G)          #返回图中所有节点的度分布序列
#x = range(len(degree))                             #生成x轴序列,从1到最大度
#y = [z / float(sum(degree)) for z in degree]  
#将频次转换为频率
#plt.loglog(x,y,color="blue",linewidth=2)           #在双对数坐标轴上绘制度分布曲线  
#plt.show()                                                          #显示图表
#---------------------------------------------------------------------------

强烈建议访问该网站
https://networkx.org/documentation/stable/auto_examples/index.html#basic

你可能感兴趣的:(python生成一个图以及计算图的一些网络指标)