gensim实现LDA(Latent Dirichlet Allocation)算法提取主题词(topic)

 Latent Dirichlet Allocation(LDA) 隐含分布作为目前最受欢迎的主题模型算法被广泛使用。LDA能够将文本集合转化为不同概率的主题集合。需要注意的是LDA是利用统计手段对主题词汇进行到的处理,是一种词袋(bag-of-words)方法。如:
 输入:

第一段:“Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. It is altogether fitting and proper that we should do this.”
第二段:‘Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.’
第三段:"We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that nation might live. "

 输出:

(0, u'0.032*"conceive" + 0.032*"dedicate" + 0.032*"nation" + 0.032*"life"')
(1, u'0.059*"conceive" + 0.059*"score" + 0.059*"seven" + 0.059*"proposition"')
(2, u'0.103*"nation" + 0.071*"dedicate" + 0.071*"great" + 0.071*"field"')
(3, u'0.032*"conceive" + 0.032*"nation" + 0.032*"dedicate" + 0.032*"rest"')
(4, u'0.032*"conceive" + 0.032*"nation" + 0.032*"dedicate" + 0.032*"battle"')

 本文将简单介绍如何使用Python 的nltk、spacy、gensim包,实现包括预处理流程在内的LDA算法。

1. 预处理:

1.1 分词处理

#第一次使用需要首先下载en包:
#python -m spacy download en
import spacy
spacy.load('en_core_web_sm')
from spacy.lang.en import English
parser = English()
#对文章内容进行清洗并将单词统一降为小写
def tokenize(text):
    lda_tokens = []
    tokens = parser(text)
    for token in tokens:
        if token.orth_.isspace():
            continue
        elif token.like_url
  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
LDALatent Dirichlet Allocation)是一种文本主题模型,它可以将文本表示为多个主题的混合,并且每个主题又表示为多个单词的分布。LDA算法可以用于文本分类、信息检索、推荐系统等领域。 下面介绍基于gensim模块实现LDA算法的步骤: 1. 准备数据 我们需要将文本数据转换为词袋模型(bag-of-words),即将每个文档表示为一个向量,向量的每个元素表示一个单词在文档出现的次数。可以使用gensim提供的corpora.Dictionary类来构建词袋模型。 ```python from gensim import corpora # 构建词袋模型 texts = [['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']] dictionary = corpora.Dictionary(texts) corpus = [dictionary.doc2bow(text) for text in texts] ``` 2. 训练模型 使用gensim提供的models.ldamodel.LdaModel类来训练LDA模型。需要指定主题数目和迭代次数等参数。 ```python from gensim import models # 训练LDA模型 lda_model = models.LdaModel(corpus=corpus, id2word=dictionary, num_topics=3, iterations=100) ``` 3. 查看结果 可以使用lda_model.print_topics()方法查看每个主题的单词分布。 ```python # 查看每个主题的单词分布 for topic in lda_model.print_topics(): print(topic) ``` 4. 推断文档主题 可以使用lda_model.get_document_topics()方法推断文档的主题分布。 ```python # 推断文档主题 doc = corpus[0] doc_topics = lda_model.get_document_topics(doc) print(doc_topics) ``` 至此,基于gensim模块的LDA算法实现就完成了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值