2021-01-13

Task1:论文数据统计

目录

1.1任务说明

1.2数据集介绍

1.3arxiv论文类别介绍

1.4具体代码实现

1.4.1导入package并读取原始数据

1.4.2数据预处理

1.4.3数据分析及可视化

 

1.1任务说明

  • 任务主题:论文数量统计,即统计2019年全年计算机各方向论文数量
  • 赛题的理解、使⽤ Pandas 读取数据并进⾏统计;
  • 学习 Pandas 的基础操作;

1.2数据集介绍

  • 数据集来源:: https://www.kaggle.com/Cornell-University/arxiv

  • 数据集的格式如下:

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

1.3  arxiv论文类别介绍

我们从arxiv官⽹,查询到论⽂的类别名称以及其解释如下。 链接:https://arxiv.org/help/api/user-manual 的 5.3 ⼩节的 Subject Classifications 的部分,或 https://arxiv.org/category_taxonomy

1.4具体代码实现

1.4.1导入package并读取原始数据

#导入所需要的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 #画图⼯具
#读入数据
data = []#初始化
#使用with语句优势:1.⾃动关闭⽂件句柄;2.⾃动显示(处理)⽂件读取数据异常
 with open("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 #显示数据⼤⼩
data.head(5)#显示数据的前五行

data.shape运行结果为(170618, 14),data.head由于是在spyder里面运行的,结果不太美观,下次task打算试一试jupyter notebook

 1.4.2数据预处理

1.粗略统计论文信息。结果表明:共有1338381个数据,有61371个⼦类(因为有论⽂的类别是多个,例如⼀篇paper的 类别是CS.AI & CS.MM和⼀篇paper的类别是CS.AI & CS.OS属于不同的⼦类别,这⾥仅仅是粗略统计),其中最多的种类是astro-ph,即Astrophysics(天体物理学),共出现了86914次。 由于部分论⽂的类别不⽌⼀种,所以下⾯我们判断在本数据集中共出现了多少种独⽴的数据集

2.论文种类统计

#所有的种类(独立的)
unique_categories = set([i for l in [x.split(' ') for x in data["categories"]]
for i in l])
len(unique_categories)
unique_categories
Out[6]: 
{'acc-phys',
 'adap-org',
 'alg-geom',
 'astro-ph',
 'astro-ph.CO',
 'astro-ph.EP',
 'astro-ph.GA',
 'astro-ph.HE',
 'astro-ph.IM',
 'astro-ph.SR',
 'chao-dyn',
 'chem-ph',
 'cmp-lg',
 'comp-gas',
 'cond-mat',
 'cond-mat.dis-nn',
 'cond-mat.mes-hall',
 'cond-mat.mtrl-sci',
 'cond-mat.other',
 'cond-mat.quant-gas',
 'cond-mat.soft',
 'cond-mat.stat-mech',
 'cond-mat.str-el',
 'cond-mat.supr-con',
 'cs.AI',
 'cs.AR',
 'cs.CC',
 'cs.CE',
 'cs.CG',
 'cs.CL',
 'cs.CR',
 'cs.CV',
 'cs.CY',
 'cs.DB',
 'cs.DC',
 'cs.DL',
 'cs.DM',
 'cs.DS',
 'cs.ET',
 'cs.FL',
 'cs.GL',
 'cs.GR',
 'cs.GT',
 'cs.HC',
 'cs.IR',
 'cs.IT',
 'cs.LG',
 'cs.LO',
 'cs.MA',
 'cs.MM',
 'cs.MS',
 'cs.NA',
 'cs.NE',
 'cs.NI',
 'cs.OH',
 'cs.OS',
 'cs.PF',
 'cs.PL',
 'cs.RO',
 'cs.SC',
 'cs.SD',
 'cs.SE',
 'cs.SI',
 'cs.SY',
 'dg-ga',
 'econ.EM',
 'econ.GN',
 'econ.TH',
 'eess.AS',
 'eess.IV',
 'eess.SP',
 'eess.SY',
 'funct-an',
 'gr-qc',
 'hep-ex',
 'hep-lat',
 'hep-ph',
 'hep-th',
 'math-ph',
 'math.AC',
 'math.AG',
 'math.AP',
 'math.AT',
 'math.CA',
 'math.CO',
 'math.CT',
 'math.CV',
 'math.DG',
 'math.DS',
 'math.FA',
 'math.GM',
 'math.GN',
 'math.GR',
 'math.GT',
 'math.HO',
 'math.IT',
 'math.KT',
 'math.LO',
 'math.MG',
 'math.MP',
 'math.NA',
 'math.NT',
 'math.OA',
 'math.OC',
 'math.PR',
 'math.QA',
 'math.RA',
 'math.RT',
 'math.SG',
 'math.SP',
 'math.ST',
 'mtrl-th',
 'nlin.AO',
 'nlin.CD',
 'nlin.CG',
 'nlin.PS',
 'nlin.SI',
 'nucl-ex',
 'nucl-th',
 'patt-sol',
 'physics.acc-ph',
 'physics.ao-ph',
 'physics.app-ph',
 'physics.atm-clus',
 'physics.atom-ph',
 'physics.bio-ph',
 'physics.chem-ph',
 'physics.class-ph',
 'physics.comp-ph',
 'physics.data-an',
 'physics.ed-ph',
 'physics.flu-dyn',
 'physics.gen-ph',
 'physics.geo-ph',
 'physics.hist-ph',
 'physics.ins-det',
 'physics.med-ph',
 'physics.optics',
 'physics.plasm-ph',
 'physics.pop-ph',
 'physics.soc-ph',
 'physics.space-ph',
 'q-alg',
 'q-bio',
 'q-bio.BM',
 'q-bio.CB',
 'q-bio.GN',
 'q-bio.MN',
 'q-bio.NC',
 'q-bio.OT',
 'q-bio.PE',
 'q-bio.QM',
 'q-bio.SC',
 'q-bio.TO',
 'q-fin.CP',
 'q-fin.EC',
 'q-fin.GN',
 'q-fin.MF',
 'q-fin.PM',
 'q-fin.PR',
 'q-fin.RM',
 'q-fin.ST',
 'q-fin.TR',
 'quant-ph',
 'solv-int',
 'stat.AP',
 'stat.CO',
 'stat.ME',
 'stat.ML',
 'stat.OT',
 'stat.TH',
 'supr-con'}

这⾥使⽤了 split 函数将多类别使⽤ “ ”(空格)分开,组成list,并使⽤ for 循环将独⽴出现的类别找出 来,并使⽤ set 函数将重复项去除得到最终所有的独⽴paper种类。

我们的任务要求对于2019年以后的paper进⾏分析,所以⾸先对于时间特征进⾏预处理,从⽽得到2019 年以后的所有种类的论⽂:(我这里下载的数据是截至2019年的,因此只有这一年的数据)

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 #查看结果

得出2019一年有170618篇相关文献。

下⾯我们挑选出计算机领域内的所有⽂章

import pandas as pd
import requests
import re
from bs4 import BeautifulSoup

headers ={
    'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100'
}
url = 'https://arxiv.org/category_taxonomy'
r = requests.get(url,headers = headers).text
# print(r.text)
soup = BeautifulSoup(r,'lxml',from_encoding='utf-8') #爬取数据,这⾥使⽤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格式的数据
# pd.set_option('display.max_columns', None)
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"])
print(df_taxonomy)

在这里显示的列和行是不完整的,参考了一位同学的解决办法,以后可能用得上,码一下。

解决方法:
#显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', None)
#设置value的显示长度为100,默认为50
pd.set_option('max_colwidth',100)

1.4.3数据分析及可视化

_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

 使⽤merge函数,以两个dataframe共同的属性 “categories” 进⾏合并,并以 “group_name” 作为 类别进⾏统计,统计结果放⼊ “id” 列中并排序。结果如下:

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()

下面统计在计算机各个⼦领域2019年的paper数量:我们可以从结果看出,Computer Vision and Pattern Recognition(计算机视觉与模式识别)类是2019年CS中paper数量最多的子类,遥遥领先于其他的CS⼦类;另外,Computation and Language(计算与语⾔)、Cryptography and Security(密码学与安全)以及Robotics(机器⼈学)的2019年paper数量均超过1000或接近1000,这与我们的认知是⼀致的。

Out[17]: 
year                                             2019
category_name                                        
Artificial Intelligence                           558
Computation and Language                         2153
Computational Complexity                          131
Computational Engineering, Finance, and Science   108
Computational Geometry                            199
Computer Science and Game Theory                  281
Computer Vision and Pattern Recognition          5559
Computers and Society                             346
Cryptography and Security                        1067
Data Structures and Algorithms                    711
Databases                                         282
Digital Libraries                                 125
Discrete Mathematics                               84
Distributed, Parallel, and Cluster Computing      715
Emerging Technologies                             101
Formal Languages and Automata Theory              152
General Literature                                  5
Graphics                                          116
Hardware Architecture                              95
Human-Computer Interaction                        420
Information Retrieval                             245
Logic in Computer Science                         470
Machine Learning                                  177
Mathematical Software                              27
Multiagent Systems                                 85
Multimedia                                         76
Networking and Internet Architecture              864
Neural and Evolutionary Computing                 235
Numerical Analysis                                 40
Operating Systems                                  36
Other Computer Science                             67
Performance                                        45
Programming Languages                             268
Robotics                                          917
Social and Information Networks                   202
Software Engineering                              659
Sound                                               7
Symbolic Computation                               44
Systems and Control                               415

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值