主题模型Gensim入门系列之一:核心概念

本系列主要介绍 Gensim的基本概念,以及通过Gensim实现词向量训练、主题模型训练、文本相似度计算等常见任务。本系列不定时更新。

系列目录:

(1)主题模型Gensim入门系列之一:核心概念

(2)主题模型Gensim入门系列之二:语料和向量空间

(3)主题模型Gensim入门系列之三:主题和变换

(4)主题模型Gensim入门系列之四:文本相似度查询

————————————————————————————

0、 简介

Gensim是一个开源的python工具包,主要实现自然语言的词向量模型和主题模型,可以实现词向量转换、文本相似性计算和文本分类等应用,其官网的简介为:topic modelling for humans。

在该工具包中,实现了word2vec,fastext词向量模型,LSA和LDA主题模型等,用户可以调用简单的接口训练自己的embedding 向量和主题模型。

 

Gensim的官网为:https://radimrehurek.com/gensim/index.html

Gensim的github地址为:https://github.com/RaRe-Technologies/gensim

Gensim文档地址:https://radimrehurek.com/gensim/auto_examples/index.html

 

Gensim的安装方法为:pip install gensim

                                     conda install gensim

 

Gensim 有4个核心概念,分别为 Document、Corpus、Vector、Model,因为是核心概念,这里沿用英文的解释,并尝试翻译:

1、Document:some text.       文档:一些文本。

2、Corpus:a collections of documents    语料:文档的集合

3、Vector:a mathematically convenient representation of a document.  向量:文档的数值表达

4、Model:an algorithm for transforming vectors from one representation to another.  模型:将向量从一种表达转换为另一种表达的算法

以下对这四个概念分别介绍。

 

1、文档

简单的说,文档就是一段文本。比如一条短信、一条微博、一个自然段、一篇文章,甚至是一本书。可以看到,文档的范围是非常广泛的,这取决于你的研究对象,比如,你是研究一条短信的分类和一本书的分类,显然文档的对象大不相同。

 举例而言:

document = "Human machine interface for lab abc computer applications"

 

2、语料

语料是文档的集合,即多个文档。在Gensim中,语料主要作为两个角色出现:

    首先,在模型的训练阶段,模型通过输入的语料学习到隐含的主题,得到有效的模型参数。

    其次,在预测阶段,语料需要通过训练好的模型进行预测和组织,比如通过相似性分析将未知的文档归类。

值得注意的一点是,gensim主要提供无监督学习模型,也就是说,输入的语料不需要打标等任何人工的干预。

举例而言:

text_corpus = [
    "Human machine interface for lab abc computer applications",
    "A survey of user opinion of computer system response time",
    "The EPS user interface management system",
    "System and human system engineering testing of EPS",
    "Relation of user perceived response time to error measurement",
    "The generation of random binary unordered trees",
    "The intersection graph of paths in trees",
    "Graph minors IV Widths of trees and well quasi ordering",
    "Graph minors A survey",
]

以上仅为一个非常小型的语料示例,实际中的语料可能是规模非常大,通常会存储到文件中。

在得到一个语料之后,通常要进行一些预处理操作,最基本的操作为分词去除停用词。对于英文,gensim提供了gensim.utils.simple_preprocess()接口实现这个功能,而中文则要借助 jieba、Hanlp等分词工具实现这些功能。作为示例,我们把上面的语料作如下处理:

import pprint # 为了保证打印的格式比较好看
 
 
# 创建停止词列表
stoplist = set('for a of the and to in'.split(' '))
 
# 将所有文档进行大小写转换和根据空格分词
texts = [[word for word in document.lower().split() if word not in stoplist]
         for document in text_corpus]
 
# 统计词频
from collections import defaultdict
frequency = defaultdict(int)
for text in texts:
    for token in text:
        frequency[token] += 1
 
# 文档只保存词频大于1的词
processed_corpus = [[token for token in text if frequency[token] > 1] for text in texts]
pprint.pprint(processed_corpus)
 
 
# 输出如下
 
"""
[['human', 'interface', 'computer'],
 ['survey', 'user', 'computer', 'system', 'response', 'time'],
 ['eps', 'user', 'interface', 'system'],
 ['system', 'human', 'system', 'eps'],
 ['user', 'response', 'time'],
 ['trees'],
 ['graph', 'trees'],
 ['graph', 'minors', 'trees'],
 ['graph', 'minors', 'survey']]
"""

获得预处理的语料之后(processed_corpus),我们根据语料建立一个字典,字典对语料中的每一个单词赋予1个整数索引。建立字典采用gensim.corpora.Dictionary 类:

from gensim import corpora
 
dictionary = corpora.Dictionary(processed_corpus)
print(dictionary)
 
 
#输出如下
"""
Dictionary(12 unique tokens: ['computer', 'human', 'interface', 'response', 'survey']...)
"""

根据上述处理后的语料,获得了一个包含12个单词的字典,可以通过以下方法获取所有词的索引:

pprint.pprint(dictionary.token2id)
 
 
"""
{'computer': 0,
 'eps': 8,
 'graph': 10,
 'human': 1,
 'interface': 2,
 'minors': 11,
 'response': 3,
 'survey': 4,
 'system': 5,
 'time': 6,
 'trees': 9,
 'user': 7}
"""

 

3、向量

为了将文档输入模型进行训练,需要将文档转换为数值向量。

一种转换的方法是,统计每个词出现的次数,比如对于第一个文档: ['human', 'interface', 'computer'],三个单词在文档中各出现1次,假设三个单词在上述的字典中的编号分别为 1,2,0。则可以将该文档转换如下:[(1,1),(2,1),(0,1)],这种向量表达称为词袋表达

实现转换的方法如下:

bow_corpus = [dictionary.doc2bow(text) for text in processed_corpus]
pprint.pprint(bow_corpus)
 
 
#输出
"""
[[(0, 1), (1, 1), (2, 1)],
 [(0, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)],
 [(2, 1), (5, 1), (7, 1), (8, 1)],
 [(1, 1), (5, 2), (8, 1)],
 [(3, 1), (6, 1), (7, 1)],
 [(9, 1)],
 [(9, 1), (10, 1)],
 [(9, 1), (10, 1), (11, 1)],
 [(4, 1), (10, 1), (11, 1)]]
 
"""

值得注意的一点是,以上示例由于规模较小,所以是直接加载到内存上统一处理的,在面对大规模语料的时候,gensim提供了迭代器的入口,可以实现迭代处理,从而减少内存的占用。

 

4、模型

上面将原始的语料转换成了向量表达,接下来就可以通过模型将初始的向量表达转换为另外一种表达。首先模型需要通过原始语料的向量进行训练。

以 tf-idf 模型为例,tf-idf 可以将原来的词袋表达转换为另外一种向量表达,代码如下:

from gensim import models
 
# 利用语料训练tf-idf模型
tfidf = models.TfidfModel(bow_corpus)
 
# 测试”system“和”minors“的转换
words = ["system","minors"]
print(tfidf[dictionary.doc2bow(words)])
 
 
#输出
"""
[(5, 0.5898341626740045), (11, 0.8075244024440723)]
"""

其中,元组中的第二个元素表示输入语料(["system","minors"])中单词与在原始语料中的稀有性(区分性),即原始语料中出现的次数越少,则这个数值越大。从输出的结果可以看出,minors的权重比较大,说明minors在原始语料中出现的次数较少,它可以作为输入语料区分于原始语料的一个特征。这在找文本关键词或者主题词的时候是非常有用的。

训练后的模型还可以保存成模型文件,以便后续的使用。关于保存方法将在后面的部分加以介绍。

在训练完模型之后,你还可以在该模型的基础上做很多事情。比如查询文本之间的相似度,其中的一个方法如下:

from gensim import similarities
 
# 针对语料建立相似度查询模型
index = similarities.SparseMatrixSimilarity(tfidf[bow_corpus], num_features=12)
 
 
query_document = [system,engineering]
query_bow = dictionary.doc2bow(query_document)
sims = index[tfidf[query_bow]]
print(list(enumerate(sims)))
 
 
#输出-得到查询文档[system,engineering]和语料所有文档的相似度
[(0, 0.0), (1, 0.32448703), (2, 0.41707572), (3, 0.7184812), (4, 0.0), (5, 0.0), (6, 0.0), (7, 0.0), (8, 0.0)]
# 通过排序就可以找出语料中,和输入文档最相似的文档

 

 

翻译和编辑自文档:Gensim-Core Concepts

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值