【学术前沿分析】4 论文种类分类

任务说明

  • 论文分类(数据建模任务),利用已有数据建模,对新论文进行类别分类;
  • 使用论文标题完成类别分类;
  • 学会文本分类的基本方法、TF-IDF等;

数据处理步骤

在原始arxiv论文中论文都有对应的类别,而论文类别是作者填写的。在本次任务中我们可以借助论文的标题和摘要完成:

  • 对论文标题和摘要进行处理;
  • 对论文类别进行处理;
  • 构建文本分类模型;

文本分类思路

  • 思路1:TF-IDF+机器学习分类器

直接使用TF-IDF对文本提取特征,使用分类器进行分类,分类器的选择上可以使用SVM、LR、XGboost等

  • 思路2:FastText

FastText是入门款的词向量,利用Facebook提供的FastText工具,可以快速构建分类器

  • 思路3:WordVec+深度学习分类器

WordVec是进阶款的词向量,并通过构建深度学习分类完成分类。深度学习分类的网络结构可以选择TextCNN、TextRnn或者BiLSTM。

  • 思路4:Bert词向量

Bert是高配款的词向量,具有强大的建模学习能力。

(涉及深度学习还得后面补,日渐沦为没有感情的搬运工。。。。)

具体代码实现以及讲解

首先完成字段读取:

# 导入所需的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 #画图工具
def readArxivFile(path, columns=['id', 'submitter', 'authors', 'title', 'comments', 'journal-ref', 'doi',
       'report-no', 'categories', 'license', 'abstract', 'versions',
       'update_date', 'authors_parsed'], count=None):
    '''
    定义读取文件的函数
        path: 文件路径
        columns: 需要选择的列
        count: 读取行数
    '''
    
    data  = []
    with open(path, 'r') as f: 
        for idx, line in enumerate(f): 
            if idx == count:
                break
                
            d = json.loads(line)
            d = {
    
    col : d[col] for col in columns}
            data.append(d)

    data = pd.DataFrame(data)
    return data

data = readArxivFile('arxiv-metadata-oai-snapshot.json', 
                     ['id', 'title', 'categories', 'abstract'],
                    200000)

data
id title categories abstract
0 0704.0001 Calculation of prompt diphoton production cros... hep-ph A fully differential calculation in perturba...
1 0704.0002 Sparsity-certifying Graph Decompositions math.CO cs.CG We describe a new algorithm, the $(k,\ell)$-...
2 0704.0003 The evolution of the Earth-Moon system based o... physics.gen-ph The evolution of Earth-Moon system is descri...
3 0704.0004 A determinant of Stirling cycle numbers counts... math.CO We show that a determinant of Stirling cycle...
4 0704.0005 From dyadic $\Lambda_{\alpha}$ to $\Lambda_{\a... math.CA math.FA In this paper we show how to compute the $\L...
... ... ... ... ...
199995 1007.0757 Detectability of large-scale power suppression... astro-ph.CO gr-qc hep-th Suppression in primordial power on the Unive...
199996 1007.0758 Systematic Improvement of Parton Showers with ... hep-ph hep-ex nucl-th We carry out a systematic classification and...
199997 1007.0759 Relaxation dynamics of stochastic long-range i... cond-mat.stat-mech Long-range interacting systems, while relaxi...
199998 1007.0760 Identification of a connection from Cauchy dat... math.DG math.AP We consider a connection $\nabla^X$ on a com...
199999 1007.0761 Adhesive penetration in Beech wood Part II: Pe... cond-mat.mtrl-sci We propose an analytical model to predict th...

200000 rows × 4 columns

为了方便数据的处理,我们可以将标题和摘要拼接一起完成分类。

data['text'] = data['title'] + data['abstract']

data['text'] = data['text'].apply(lambda x: x.replace('\n',' '))
data['text'] = data['text'].apply(lambda x: x.lower())
data = data.drop(['abstract', 'title'], axis=1)
data.head()
id categories text
0 0704.0001 hep-ph calculation of prompt diphoton production cros...
1 0704.0002 math.CO cs.CG sparsity-certifying graph decompositions we d...
2 0704.0003 physics.gen-ph the evolution of the earth-moon system based o...
3 0704.0004 math.CO a determinant of stirling cycle numbers counts...
4 0704.0005 math.CA math.FA from dyadic $\lambda_{\alpha}$ to $\lambda_{\a...

由于原始论文有可能有多个类别,所以也需要处理:

# 多个类别,包含子分类
data['categories'] = data['categories'].apply(lambda x : x.split(' '))

# 单个类别,不包含子分类
data['categories_big'] = data['categories'].apply(lambda x : [xx.split('.')[0] for xx in x])
data.head()

然后将类别进行编码,这里类别是多个,所以需要多编码:

from sklearn.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer()
data_label = mlb.fit_transform(data['categories_big'].iloc[:])

思路1

思路1使用TFIDF提取特征,限制最多4000个单词:

from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(max_features=4000)
data_tfidf = vectorizer.fit_transform(data['text'].iloc[:])

由于这里是多标签分类,可以使用sklearn的多标签分类进行封装:

# 划分训练集和验证集
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(data_tfidf, data_label,
                                                 test_size = 0.2,random_state = 1)

# 构建多标签分类模型
from sklearn.multioutput import MultiOutputClassifier
from sklearn.naive_bayes import MultinomialNB
clf = MultiOutputClassifier(MultinomialNB()).fit(x_train, y_train)
from sklearn.metrics import classification_report
print(classification_report(y_test, clf.predict(x_test)))

              precision    recall  f1-score   support

           0       0.95      0.85      0.89      7925
           1       0.85      0.79      0.82      7339
           2       0.77      0.72      0.74      2944
           3       0.00      0.00      0.00         4
           4       0.72      0.48      0.58      2123
           5       0.51      0.66      0.58       987
           6       0.86      0.38      0.52       544
           7       0.71      0.69      0.70      3649
           8       0.76      0.61      0.68      3388
           9       0.85      0.88      0.87     10745
          10       0.46      0.13      0.20      1757
          11       0.79      0.04      0.07       729
          12       0.45      0.35      0.39       507
          13       0.54      0.36      0.43      1083
          14       0.69      0.14      0.24      3441
          15       0.84      0.20      0.33       655
          16       0.93      0.16      0.27       268
          17       0.87      0.43      0.58      2484
          18       0.82      0.38      0.52       692

   micro avg       0.81      0.65      0.72     51264
   macro avg       0.70      0.43      0.50     51264
weighted avg       0.80      0.65      0.69     51264
 samples avg       0.72      0.72      0.70     51264

思路2

思路2使用深度学习模型,单词进行词嵌入然后训练。将数据集处理进行编码,并进行截断:

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(data['text'].iloc[:100000], 
                                                    data_label[:100000],
                                                 test_size = 0.95,random_state = 1)

# parameter
max_features= 500
max_len= 150
embed_size=100
batch_size = 128
epochs = 5

from keras.preprocessing.text import Tokenizer
from keras.preprocessing import sequence

tokens = Tokenizer(num_words = max_features)
tokens.fit_on_texts(list(data['text'].iloc[:100000]))

y_train = data_label[:100000]
x_sub_train = tokens.texts_to_sequences(data['text'].iloc[:100000])
x_sub_train = sequence.pad_sequences(x_sub_train, maxlen=max_len)

定义模型并完成训练:

# LSTM model
# Keras Layers:
from keras.layers import Dense,Input,LSTM,Bidirectional,Activation,Conv1D,GRU
from keras.layers import Dropout,Embedding,GlobalMaxPooling1D, MaxPooling1D, Add, Flatten
from keras.layers import GlobalAveragePooling1D, GlobalMaxPooling1D, concatenate, SpatialDropout1D# Keras Callback Functions:
from keras.callbacks import Callback
from keras.callbacks import EarlyStopping,ModelCheckpoint
from keras import initializers, regularizers, constraints, optimizers, layers, callbacks
from keras.models import Model
from keras.optimizers import Adam

sequence_input = Input(shape=(max_len, ))
x = Embedding(max_features, embed_size, trainable=True)(sequence_input)
x = SpatialDropout1D(0.2)(x)
x = Bidirectional(GRU(128, return_sequences=True,dropout=0.1,recurrent_dropout=0.1))(x)
x = Conv1D(64, kernel_size = 3, padding = "valid", kernel_initializer = "glorot_uniform")(x)
avg_pool = GlobalAveragePooling1D()(x)
max_pool = GlobalMaxPooling1D()(x)
x = concatenate([avg_pool, max_pool]) 
preds = Dense(19, activation="sigmoid")(x)

model = Model(sequence_input, preds)
model.compile(loss='binary_crossentropy',optimizer=Adam(lr=1e-3),metrics=['accuracy'])
model.fit(x_sub_train, y_train, 
          batch_size=batch_size, 
          validation_split=0.2,
          epochs=epochs)

没有gpu跑起来很慢。。。

感谢DataWhale

猜你喜欢

转载自blog.csdn.net/weixin_41545602/article/details/113010549
今日推荐