如何使用 doc2vec 为安然数据集分配标签

How to use doc2vec to assign labels to enron dataset

我正在使用安然电子邮件数据集。我要给这些class贴上5个标签,分别是:欣赏、升级、sending_document、request_for_document、meeting_invites。现在,我使用 doc2vec 为其分配标签:

emails_df['tokenized_sents'] = emails_df.iloc[0:1000].apply(lambda row: nltk.word_tokenize(row['content']), axis=1)

common_texts = [
                ['We' ,'were', 'impressed', 'with' ,'the' ,'work', 'produced' ,'by' ,'you' ,'and' ,'you' ,'showed' ,'leadership', 'qualities' ,'that' 'the' ,'rest' ,'of' ,'the', 'team' ,'could' ,'look', 'up' ,'to'],

                ['Finish' ,'the' ,'financial' ,'analysis', 'report', 'that' ,'was' ,'started' ,'last' ,'week'],

                ['Please', 'find', 'attached'],

                ['Looking', 'forward', 'to' ,'hearing' ,'from', 'you'],

                ['The' , 'meeting', 'will', 'take', 'place', 'on', 'Wednesday'],

                ['forwarded', 'to', 'xx']



    ]
documents = [TaggedDocument(doc, [i]) for i, doc in enumerate(common_texts)]
labels = []
#print (documents)

model = Doc2Vec(documents, size=5, window=3, min_count=1, workers=4)
#Persist a model to disk:

from gensim.test.utils import get_tmpfile
fname = get_tmpfile("my_doc2vec_model")

#print (fname)
#output: C:\Users\userABC\AppData\Local\Temp\my_doc2vec_model

#load model from saved file
model.save(fname)
model = Doc2Vec.load(fname)  
# you can continue training with the loaded model!
#If you’re finished training a model (=no more updates, only querying, reduce memory usage), you can do:

model.delete_temporary_training_data(keep_doctags_vectors=True, keep_inference=True)

#Infer vector for a new document:
#Here our text paragraph just 2 words
c=0
for i in emails_df['tokenized_sents']: 
    vector = model.infer_vector(i)
    c=c+1
    import operator
    index, value = max(enumerate(vector), key=operator.itemgetter(1))
    labels.append(index)

在这里,emails_df 只是我读取 emails.csv 的数据帧。我不需要完美的贴标机,但我需要有价值的东西。今后应该往哪个方向去改进一下呢? (考虑到这是我第一次了解doc2vec)

编辑:说明: 我创建了 common_texts 作为特征向量,其中包含属于每个 class 的句子。然后我应用 doc2vec 然后使用它的 infer_vector 函数来生成相似性

Doc2Vec 需要大量数据来训练有用的 "dense embedding" 文本向量。它不太可能只用少量的训练文本就给出好的结果,就像你的 6 short common_texts – 即使你将 vector0size 减少到 5 维。

(发表的Doc2Vec工作经常使用数万到数百万的训练文档,来训练100-1000维的文档向量。)

但更进一步,这些向量 而不是 将它们各自的维度作为可解释的类别。相反,它们是 "dense" 向量,其中没有先验地为各个轴分配意义。相反,所有训练文档都 "packed" 到一个共享的 space 中,其中它们的相对距离或相对方向可能表示关系强度。

因此,您根据文档向量的最大正值维度为每个文档选择标签的代码是对 Doc2Vec 样式向量的荒谬误用。

这将有助于更清楚地说明您的实际目标:您想要分配什么样的标签,为什么?

特别是,更适合:

  • 所有电子邮件文本

  • 上训练Doc2Vec模型
  • 如果您知道某些电子邮件的标签,但想找出其他电子邮件的标签,则使用文档向量作为单独 "classification" 的输入步。

  • 如果您没有已知标签,但想发现电子邮件中可能存在何种自然分组(如 Doc2Vec 所建模),那么您可以使用doc-vectors 作为单独 "clustering" 步骤的输入——然后进一步 examine/analyze 生成的集群,以查看它们是否适合您的需求或揭示您的项目感兴趣的模式。

(有许多使用 Python 机器学习工具对来自 Enron 数据集的电子邮件进行分类的在线教程示例。我建议成功完成其中一个或多个 - 即使他们不使用Doc2Vec – 了解一般的分类器训练,然后是分类器测试,最后是分类器应用过程。只有这样,才能将 Doc2Vec 视为 'features' 的额外来源,以添加到分类工作中。 )