5.1 任务说明
- 学习主题:作者关联(数据建模任务),对论文作者关系进行建模,统计最常出现的作者关系;
- 学习内容:构建作者关系图,挖掘作者关系
- 学习成果:论文作者知识图谱、图关系挖掘
5.2 数据处理步骤
将作者列表进行处理,并完成统计。具体步骤如下:
- 将论文第一作者与其他作者(论文非第一作者)构建图;
- 使用图算法统计图中作者与其他作者的联系;
5.3 具体代码
5.3.1 导入数据
import pandas as pd
import json
import matplotlib.pyplot as plt
import networkx as nx
导入数据,选择人工智能领域的论文进行分析:
data = []
with open('arxiv-metadata-oai-snapshot.json','r') as f:
for inx, line in enumerate(f):
d = json.loads(line)
if 'cs.AI' in d['categories']:
d = {'authors_parsed':d['authors_parsed']}
data.append(d)
data = pd.DataFrame(data)
data
5.3.2 绘制图谱
- 为了方便显示,仅选取了前十篇论文
- itertuples用于迭代DataFrame对象
- G.add_edge()向图中添加边
创建作者链接的无向图,将作者关系图进行绘制:
# 创建无向图
G = nx.Graph()
# 选取十篇论文进行构建
for row in data.iloc[:10].itertuples():
authors = row[1]
authors = [' '.join(x[:-1]) for x in authors]
# 第一个作者 与 其他作者链接
for author in authors[1:]:
G.add_edge(authors[0],author) # 添加节点2,3并链接23节点
nx.draw(G,with_labels=True)
5.3.3求作者之间的距离:
try:
print(nx.dijkstra_path(G, 'Kosel T.', 'Gershenson Carlos'))
except:
print('No path')
5.3.4 绘制最大联通子图:
- 选取前500偏论文
G = nx.Graph()
for row in data.iloc[:500].itertuples():
authors = row[1]
authors = [' '.join(x[:-1]) for x in authors]
for author in authors[1:]:
G.add_edge(authors[0],author)
degree_sequence = sorted([d for n, d in G.degree()], reverse=True)
dmax = max(degree_sequence)
plt.loglog(degree_sequence, "b-", marker="o")
plt.title("Degree rank plot")
plt.ylabel("degree")
plt.xlabel("rank")
# draw graph in inset
plt.axes([0.45, 0.45, 0.45, 0.45])
Gcc = G.subgraph(sorted(nx.connected_components(G), key=len, reverse=True)[0])
pos = nx.spring_layout(Gcc)
plt.axis("off")
nx.draw_networkx_nodes(Gcc, pos, node_size=20)
nx.draw_networkx_edges(Gcc, pos, alpha=0.4)
plt.show()
5.3.5 AI领域论文合作数量最多的作者
G = nx.Graph()
for row in data.itertuples():
authors = row[1]
authors = [' '.join(x[:-1]) for x in authors]
for author in authors[1:]:
G.add_edge(authors[0],author)
num_sorted = sorted(dict(G.degree()).items(),key=lambda x :x[1],reverse=True)
for i,(name,num) in enumerate(num_sorted):
if i <3:
print(name,':',num)
Bakas Spyridon : 423
Levine Sergey : 82
Mancas Matei : 73