导入基本的包
除了之前运用的一些包,还需要导入创建网络专用的包:networkx 以及community用于团体预测即聚类【 未安装:pip3 install python-louvain】
import metaknowledge as mk
import matplotlib.pyplot as plt
import seaborn as sns
import networkx as ntx
import pandas as pd
import community
合著网络的构建
folder_collec = mk.RecordCollection(r'F:\metaknow\example data')
coauth_net = folder_collec.networkCoAuthor()
print(mk.graphStats(coauth_net))
_________________________________________
Nodes: 2451
Edges: 4864
Isolates: 263
Self loops: 2
Density: 0.00162
Transitivity: 0.940295 #图或网络的传递性。即图或网络中,认识同一个节点的两个节点也可能认识双方,计算公式为3*图中三角形的个数/三元组个数
对网络节点进行筛选
——去掉自循环的点
——只保留最大网络
mk.dropEdges(coauth_net, minWeight = 2, dropSelfLoops = True)
print(mk.graphStats(coauth_net))
___________________________________
Nodes: 2451
Edges: 502
Isolates: 2224
Self loops: 0
Density: 0.000167195
Transitivity: 0.942647
—————————————————————
giant_coauth = max(ntx.connected_component_subgraphs(coauth_net), key=len)
print(mk.graphStats(giant_coauth))
——————————————————————
Nodes: 16
Edges: 28
Isolates: 0
Self loops: 0
Density: 0.233333
Transitivity: 0.19797
计算网络的指标
度中心性(degree)
特征向量中心性(eigenvector_centrality)
中介中心性(betweenness_centrality)
接近中心性(closeness_centrality)
deg = ntx.degree_centrality(coauth_net)
eig = ntx.eigenvector_centrality(coauth_net)
closeness = ntx.closeness_centrality(coauth_net)
btw=ntx.betweenness_centrality(coauth_net)
转换为pandas的数据帧
cent_df = pd.DataFrame.from_dict([deg, eig,closeness,btw])
cent_df = pd.DataFrame.transpose(cent_df)
cent_df.columns = ['degree', 'eigenvector','close_centra',"betweeness_centra"]
print(cent_df.sort_values('degree', ascending = False)[:10])
_________________________________________________________
degree eigenvector close_centra betweeness_centra
Goldhill, David 0.005714 0.182574 0.005714 0.0
Morrison, Laurie J. 0.005714 0.182574 0.005714 0.0
Benson, W 0.005714 0.182574 0.005714 0.0
Biaggioni, I 0.005714 0.182574 0.005714 0.0
Heidenreich, PA 0.005714 0.182574 0.005714 0.0
Myerburg, RJ 0.005714 0.182574 0.005714 0.0
Nolan, Jerry P. 0.005714 0.182574 0.005714 0.0
Nichol, Graham 0.005714 0.182574 0.005714 0.0
Klein, GJ 0.005714 0.182574 0.005714 0.0
Kloeck, Walter 0.005714 0.182574 0.005714 0.0
画网络图
——整体
size = [2000 * eig[node] for node in coauth_net]
ntx.draw_spring(coauth_net, node_size = size, with_labels = True, font_size = 5,
node_color = "#FFFF00", edge_color = "#D4D5CE", alpha = .95)
plt.savefig(r'F:\metaknow\example data\network_coauthors.png')
plt.show()
整体作者合著图
——整体网络中最大的网络
eig_giant = ntx.eigenvector_centrality(giant_coauth)
size = [2000 * eig_giant[node] for node in giant_coauth]
ntx.draw_spring(giant_coauth, node_size = size#节点大小,
with_labels = False,#是否显示节点标签
font_size = 5,#标签大小
node_color = "#FFFF00", #节点颜色
edge_color = "#D4D5CE", #边颜色
alpha = .95#文本透明度)
plt.savefig(r'F:\metaknow\example data\giant_network_coauthors.png')
plt.show()
(有标签).png
无标签图.png
团体探测
partition = community.best_partition(coauth_net)
modularity = community.modularity(partition, coauth_net)
print('Modularity:', modularity)
colors = [partition[n] for n in coauth_net.nodes()]
my_colors = plt.cm.Set1 # 色集
ntx.draw(coauth_net, node_color=colors, cmap = my_colors, edge_color = "#D4D5CE")
plt.savefig(r'F:\metaknow\example data\coauthors_community.png')
plt.show()
团体探测图_整体
边缘的基本都是孤立的点
部分
同被引网络
journal_cocite = folder_collec.networkCoCitation(coreOnly = True)
mk.dropEdges(journal_cocite , minWeight = 3)
print(mk.graphStats(journal_cocite))
giantJournal = max(ntx.connected_component_subgraphs(journal_cocite), key=len)
ntx.draw_spring(giantJournal, with_labels = False, node_size = 15,
node_color = "#77787B", edge_color = "#D4D5CE", alpha = .95)
plt.savefig(r'F:\metaknow\example data\network_journal_cocite.png')
plt.show()
无标签
局部(有标签)
节点是单篇文章,标签是由文章的作者、时间和来源期刊组成
保存为其它文件格式以供其它软件分析
——pajeck
——gephi
ntx.write_pajek(coauth_net,r'F:\metaknow\example data\coauthor.net') #pajek可用此进行分析
ntx.write_graphml(coauth_net,r'F:\metaknow\example data\coauthor.graphml') #gephi用于进一步分析