此随笔为储存代码用
首先展示gephi的json文件
{ "attributes": { "creator": "Gephi 0.10.1" }, "options": { "multi": false, "allowSelfLoops": true, "type": "undirected" }, "nodes": [ { "key": "4551", "attributes": { "label": "国贸", "x": 1935.75, "y": -1055.0312, "size": 10.0, "color": "#000000", "stationname": "国贸", "lon": 114.1138257, "lat": 22.54233276 } }, { "key": "4552", "attributes": { "label": "左炮台东", "x": -1881.375, "y": -2363.5312, "size": 10.0, "color": "#000000", "stationname": "左炮台东", "lon": 113.8951198, "lat": 22.47307276 } } ], "edges": [ { "key": "1920", "source": "4551", "target": "4460", "attributes": { "color": "#c0c0c0", "weight": 735.4556665 } }, { "key": "1708", "source": "4461", "target": "4443", "attributes": { "color": "#c0c0c0", "weight": 1149.758641 } } ] }
对这样的结构,使用如下代码进行导入networkx并计算相应指标
import csv import json import networkx as nx import matplotlib.pyplot as plt import pandas as pd import numpy as np #初始化G图 G = nx.Graph() #初始化pos布局属性 pos={} #打开gephi输出的json文件(此处只是由于我在gephi中构建好了....) f = open('Untitled.json', 'r',encoding = 'utf-8') content = f.read() a = json.loads(content) print(type(a)) #读取节点信息到列表 list_1=a['nodes'] #读取边信息到列表 list_2=a['edges'] #我使用索引key值去确定边的两个节点,因此需要构建一个字典储存key值与站点名称的关系 dic={} #将节点添加到G图中 for lis1 in list_1: pid=lis1['key'] label=lis1['attributes']['label'] lon=lis1['attributes']['lon'] lat=lis1['attributes']['lat'] pos[label]=(lon,lat)#将每个节点的名称与其经纬度联系起来,后续绘图时可以根据经纬度布局 dic[pid]=label print(pid,label,lon,lat) G.add_node(label,lon = lon,lat = lat) #print(dic) #这里使用了两站点的人口除以其最小值作为权重,所以读取站点人口做一个字典以便后续调用,没有实际参考意义,可以不要 dic_population={} 打开储存人口数据的csv with open("POPU.csv", "r") as csvfile: reader = csv.reader(csvfile) for row in reader: dic_population[row[0]]=row[1]#创建一个字典,其键为row[0],也就是站点名称;值为row[1],也就是站点周边人口 print(dic_population) #将边添加到G图中 for lis2 in list_2: #读取源节点和目标节点的key值 s=lis2['source'] t=lis2['target'] #从名称字典中查找源节点的名字 source=dic[s] print('source:',source) #从名称字典中查找目标节点的名字 target=dic[t] print('target',target) #计算权重 weight_origin=int(dic_population[source])+int(dic_population[target]) weight=wei/1202 print(source,target,weight) #将源节点,目标节点,边权重添加到G图中 G.add_edge(source,target,weight=weight) #情景A,移除部分节点 G.remove_node('**') G.remove_node('**') #情景B,移除部分边 G.add_edge('**','**',weight=111) G.add_edge('**','**',weight=111) G.add_edge('**','**',weight=111) G.add_edge('**','**',weight=111) #查刊边和节点的信息,可视情况使用 #print(G.nodes(data = True)) #print(G.edges(data=True)) #对G图根据经纬度布局绘图并保存为pdf nx.draw(G,pos,node_shape='s',node_size = 5,node_color='red',edge_color='black',width= 0.3) plt.savefig("plot2.pdf") #计算各点点度中心性并写入文件 c_degree = nx.degree_centrality(G) with open('degree_centrality.csv','a',newline='') as f2: writer = csv.writer(f2) for row in c_degree.items(): writer.writerow(row) #计算各点接近中心性并写入文件 clo_cen = nx.closeness_centrality(G) with open('closeness_centrality.csv','a',newline='') as f3: writer = csv.writer(f3) for row in clo_cen.items(): writer.writerow(row) #计算各点中介中心性并写入文件 between_centra = nx.betweenness_centrality(G) with open('betweenness_centrality.csv','a',newline='') as f4: writer = csv.writer(f4) for row in between_centra.items(): writer.writerow(row) #输出网络特征 print("节点总数:", G.number_of_nodes()) print("边总数:", G.number_of_edges()) aver_clus = nx.average_clustering(G) print('平均聚类系数:',aver_clus) print("网络传递性(transitivity): ", nx.transitivity(G)) aver_shor_path = nx.average_shortest_path_length(subG) print('平均最短路径:',aver_shor_path) diamet = nx.diameter(subG) print('网络直径:',diamet) degree_histo = nx.degree_histogram(subG) print('度分布:',degree_histo) d = dict(nx.degree(G)) print("平均度:", sum(d.values())/len(G.nodes)) global_effic=nx.global_efficiency(G) print('网络效率:',global_effic) largest = max(nx.connected_components(subG),key=len) largest_connected_subgraph = subG.subgraph(largest) print('最大子图规模',largest_connected_subgraph)
标签:weight,nx,导入,gephi,networkx,print,csv,节点,row From: https://www.cnblogs.com/Vicrooor/p/17723397.html