學術前沿趨勢分析1:論文數據統計

一 、數據集與開發環境

下載鏈接::https://www.kaggle.com/Cornell-University/arxiv
開發環境:Jupyter lab 2.1.5

二、 數據預處理

import seaborn as sns
from bs4 import BeautifulSoup
import re
import requests
import json
import pandas as pd
import matplotlib.pyplot as plt

data=[]
with open(r"D:\Datawhale\arxiv-metadata-oai-snapshot.json",'r') as f: 
    for idx, line in enumerate(f):   #enumerate() 對於跟蹤某些值在列表中出現的位置是很有用的。 所以,如果你想將一個文件中出現的單詞映射到它出現的行號上去,可以很容易的利用 enumerate() 來完成                                                                                                                                                                                                                                                                                                                                                                                                                                        
        data.append(json.loads(line))
        #json.load()是用來讀取文件的,json.loads()是用來讀取字符串
data=pd.DataFrame(data)
data.head()        

在這裏插入圖片描述

data["categories"]
unique_categories=set(i for l in [x.split(' ') for x in data["categories"]] for i in l)
len(unique_categories) 
data["year"]=pd.to_datetime(data["update_date"]).dt.year
data["year"]
del data["update_date"]
data=data[data['year']>=2019]
data.reset_index(drop=True,inplace=True)
#drop=True: 把原來的索引index列去掉,丟掉。
#drop=False:保留原來的索引(以前的可能是亂的)
#inplace=True:不創建新的對象,直接對原始對象進行修改
#inplace=False:對數據進行修改,創建並返回新的對象承載其修改結果。
data
#爬取所有的類別
website_url=requests.get("https://arxiv.org/category_taxonomy").text

soup=BeautifulSoup(website_url,'lxml')

root=soup.find('div',{
   
   'id':'category_taxonomy_list'})

tags=root.find_all(['h2','h3','h4','p'],recursive=True)

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

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章