数据分析-任务1:论文数据统计.md

论文数据统计

1.1 任务说明

  • 任务主题:论文数量统计,即统计2019年全年计算机各个方向论文数量;
  • 任务内容:赛题的理解、使用 Pandas 读取数据并进行统计;
  • 任务成果:学习 Pandas 的基础操作;
  • 可参考的学习资料:开源组织Datawhale joyful-pandas项目

1.2 数据集介绍

  • 数据集来源:数据集链接

  • 数据集的格式如下:

    • id:arXiv ID,可用于访问论文;
    • submitter:论文提交者;
    • authors:论文作者;
    • title:论文标题;
    • comments:论文页数和图表等其他信息;
    • journal-ref:论文发表的期刊的信息;
    • doi:数字对象标识符,https://www.doi.org;
    • ``report-no:报告编号;
    • categories:论文在 arXiv 系统的所属类别或标签;
    • license:文章的许可证;
    • abstract:论文摘要;
    • versions:论文版本;
    • authors_parsed:作者的信息。
  • 数据集实例:

"root":{
   	"id":string"0704.0001"
   	"submitter":string"Pavel Nadolsky"
   	"authors":string"C. Bal\'azs, E. L. Berger, P. M. Nadolsky, C.-P. Yuan"
   	"title":string"Calculation of prompt diphoton production cross sections at Tevatron and LHC energies"
   	"comments":string"37 pages, 15 figures; published version"
   	"journal-ref":string"Phys.Rev.D76:013009,2007"
   	"doi":string"10.1103/PhysRevD.76.013009"
   	"report-no":string"ANL-HEP-PR-07-12"
   	"categories":string"hep-ph"
   	"license":NULL
   	"abstract":string"  A fully differential calculation in perturbative quantum chromodynamics is presented for the production of massive photon pairs at hadron colliders. All next-to-leading order perturbative contributions from quark-antiquark, gluon-(anti)quark, and gluon-gluon subprocesses are included, as well as all-orders resummation of initial-state gluon radiation valid at next-to-next-to leading logarithmic accuracy. The region of phase space is specified in which the calculation is most reliable. Good agreement is demonstrated with data from the Fermilab Tevatron, and predictions are made for more detailed tests with CDF and DO data. Predictions are shown for distributions of diphoton pairs produced at the energy of the Large Hadron Collider (LHC). Distributions of the diphoton pairs from the decay of a Higgs boson are contrasted with those produced from QCD processes at the LHC, showing that enhanced sensitivity to the signal can be obtained with judicious selection of events."
   	"versions":[
   			0:{
   					"version":string"v1"
   					"created":string"Mon, 2 Apr 2007 19:18:42 GMT"
   				}
   			1:{
   					"version":string"v2"
   					"created":string"Tue, 24 Jul 2007 20:10:27 GMT"
   				}]
   	"update_date":string"2008-11-26"
   	"authors_parsed":[
   			0:[
   					0:string"Balázs"
   					1:string"C."
   					2:string""]
   			1:[
   					0:string"Berger"
   					1:string"E. L."
   					2:string""]
   			2:[
   					0:string"Nadolsky"
   					1:string"P. M."
   					2:string""]
   			3:[
   					0:string"Yuan"
   					1:string"C. -P."
   					2:string""]]
}

1.3 arxiv论文类别介绍

我们从arxiv官网,查询到论文的类别名称以及其解释如下。

链接:https://arxiv.org/help/api/user-manual 的 5.3 小节的 Subject Classifications 的部分,或 https://arxiv.org/category_taxonomy, 具体的153种paper的类别部分如下:

'astro-ph': 'Astrophysics',
'astro-ph.CO': 'Cosmology and Nongalactic Astrophysics',
'astro-ph.EP': 'Earth and Planetary Astrophysics',
'astro-ph.GA': 'Astrophysics of Galaxies',
'cs.AI': 'Artificial Intelligence',
'cs.AR': 'Hardware Architecture',
'cs.CC': 'Computational Complexity',
'cs.CE': 'Computational Engineering, Finance, and Science',
'cs.CV': 'Computer Vision and Pattern Recognition',
'cs.CY': 'Computers and Society',
'cs.DB': 'Databases',
'cs.DC': 'Distributed, Parallel, and Cluster Computing',
'cs.DL': 'Digital Libraries',
'cs.NA': 'Numerical Analysis',
'cs.NE': 'Neural and Evolutionary Computing',
'cs.NI': 'Networking and Internet Architecture',
'cs.OH': 'Other Computer Science',
'cs.OS': 'Operating Systems',

1.4 具体代码实现以及讲解

# 导入所需的package
import seaborn as sns #用于画图
from bs4 import BeautifulSoup #用于爬取arxiv的数据
import re #用于正则表达式,匹配字符串的模式
import requests #用于网络连接,发送网络请求,使用域名获取对应信息
import json #读取数据,我们的数据为json格式的
import pandas as pd #数据处理,数据分析
import matplotlib.pyplot as plt #画图工具

这里使用的package的版本如下(python 3.7.4):

  • seaborn:0.9.0
  • BeautifulSoup:4.8.0
  • requests:2.22.0
  • json:0.8.5
  • pandas:0.25.1
  • matplotlib:3.1.1

检查自己版本是否满足要求,很多时候转而挠腮检查了180遍代码和别人的一样但是结果不对的情况都是由于版本不同,配置不同,所以我们有必要事先花一点时间检查相应环境是否满足:

这里一开始我直接使用print(BeautifulSoup .__version__)但是显示失败,因为BeautifulSoup是bs4下面的,不能直接打印版本。需要import bs4 再使用print(bs4.__version__)才行。类似地matplotlib也是这样,检查版本的小伙伴可以注意一下。

 print(sns.__version__)  # 0.10.0
 print(bs4.__version__)  # 4.8.2
 print(requests.__version__)  # 2.22.0
 print(json.__version__)  # 2.0.9
 print(pd.__version__)  # 1.1.0
 print(matplotlib.__version__)  # 3.1.3

读取数据

# 读入数据
data  = [] #初始化
#使用with语句优势:1.自动关闭文件句柄;2.自动显示(处理)文件读取数据异常
with open("./data/arxiv-metadata-oai-snapshot.json", 'r') as f: 
    for line in f: 
        data.append(json.loads(line))
        
data = pd.DataFrame(data) #将list变为dataframe格式,方便使用pandas进行分析
data.shape #显示数据大小

(1796911, 14)
其中的1796911表示数据总量,14表示特征数,对应我们1.2节说明的论文的14种信息。

data.head(3) #显示数据的前3行

在这里插入图片描述

'''
count:一列数据的元素个数;
unique:一列数据中元素的种类;
top:一列数据中出现频率最高的元素;
freq:一列数据中出现频率最高的元素的个数;
'''

data["categories"].describe()
# count      1796911
# unique       62055
# top       astro-ph
# freq         86914
# Name: categories, dtype: object

# 所有的种类(独立的)

unique_categories = set([i for l in [x.split(' ') for x in data["categories"]] for i in l])
len(unique_categories)
unique_categories

在这里插入图片描述

data["year"] = pd.to_datetime(data["update_date"]).dt.year #将update_date从例如2019-02-20的str变为datetime格式,并提取处year
del data["update_date"] #删除 update_date特征,其使命已完成
data = data[data["year"] >= 2019] #找出 year 中2019年以后的数据,并将其他数据删除
# data.groupby(['categories','year']) #以 categories 进行排序,如果同一个categories 相同则使用 year 特征进行排序
data.reset_index(drop=True, inplace=True) #重新编号
data #查看结果

在这里插入图片描述

#爬取所有的类别
website_url = requests.get('https://arxiv.org/category_taxonomy').text #获取网页的文本数据
soup = BeautifulSoup(website_url,'lxml') #爬取数据,这里使用lxml的解析器,加速
root = soup.find('div',{'id':'category_taxonomy_list'}) #找出 BeautifulSoup 对应的标签入口
tags = root.find_all(["h2","h3","h4","p"], recursive=True) #读取 tags

#初始化 str 和 list 变量
level_1_name = ""
level_2_name = ""
level_2_code = ""
level_1_names = []
level_2_codes = []
level_2_names = []
level_3_codes = []
level_3_names = []
level_3_notes = []

#进行
for t in tags:
    if t.name == "h2":
        level_1_name = t.text    
        level_2_code = t.text
        level_2_name = t.text
    elif t.name == "h3":
        raw = t.text
        level_2_code = re.sub(r"(.*)\((.*)\)",r"\2",raw) #正则表达式:模式字符串:(.*)\((.*)\);被替换字符串"\2";被处理字符串:raw
        level_2_name = re.sub(r"(.*)\((.*)\)",r"\1",raw)
    elif t.name == "h4":
        raw = t.text
        level_3_code = re.sub(r"(.*) \((.*)\)",r"\1",raw)
        level_3_name = re.sub(r"(.*) \((.*)\)",r"\2",raw)
    elif t.name == "p":
        notes = t.text
        level_1_names.append(level_1_name)
        level_2_names.append(level_2_name)
        level_2_codes.append(level_2_code)
        level_3_names.append(level_3_name)
        level_3_codes.append(level_3_code)
        level_3_notes.append(notes)

#根据以上信息生成dataframe格式的数据
df_taxonomy = pd.DataFrame({
    'group_name' : level_1_names,
    'archive_name' : level_2_names,
    'archive_id' : level_2_codes,
    'category_name' : level_3_names,
    'categories' : level_3_codes,
    'category_description': level_3_notes
    
})

#按照 "group_name" 进行分组,在组内使用 "archive_name" 进行排序
df_taxonomy.groupby(["group_name","archive_name"])
df_taxonomy

在这里插入图片描述

_df = data.merge(df_taxonomy, on="categories", how="left").drop_duplicates(["id","group_name"]).groupby("group_name").agg({"id":"count"}).sort_values(by="id",ascending=False).reset_index()

_df

在这里插入图片描述

fig = plt.figure(figsize=(15,12))
explode = (0, 0, 0, 0.2, 0.3, 0.3, 0.2, 0.1) 
plt.pie(_df["id"],  labels=_df["group_name"], autopct='%1.2f%%', startangle=160, explode=explode)
plt.tight_layout()
plt.show()

在这里插入图片描述

group_name="Computer Science"
cats = data.merge(df_taxonomy, on="categories").query("group_name == @group_name")
cats.groupby(["year","category_name"]).count().reset_index().pivot(index="category_name", columns="year",values="id") 

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值