基于gensim的lda实践

基于gensim的lda实践

from gensim.test.utils import common_texts
from gensim.corpora.dictionary import Dictionary
from gensim.models.ldamodel import LdaModel

# Create a corpus from a list of texts
texts = [['褒贬','春秋','看重','德望'],
        ['慈禧','政治','果断','胆识','眼光','改革'],
        ['历史','兴替'],
        ['食堂','饭菜','不好吃','闹肚子'],
        ['就餐','挑剔','鲜','美味']]
dct = Dictionary(texts)
corpus = [dct.doc2bow(text) for text in texts]

lda = LdaModel(corpus, num_topics=2, id2word=dct)
# Print the most contributing words for 2 topics
lda.print_topics(num_topics=2, num_words=6)

output:
[(0,
‘0.072*“食堂” + 0.072*“饭菜” + 0.072*“挑剔” + 0.072*“不好吃” + 0.071*“美味” + 0.071*“鲜”’),
(1,
‘0.071*“褒贬” + 0.071*“改革” + 0.071*“慈禧” + 0.071*“德望” + 0.070*“政治” + 0.070*“果断”’)]

# Print topic dist. of every doc
for c in corpus:
    print(lda[c])

output:
[(0, 0.10735256), (1, 0.89264745)]
[(0, 0.07645551), (1, 0.92354447)]
[(0, 0.81719965), (1, 0.18280035)]
[(0, 0.89298487), (1, 0.10701518)]
[(0, 0.8923324), (1, 0.1076676)]

简析:1,2,3文档说的是历史相关;4,5文档说的是食物相关,除了第3个文档,其他主题分布是大致正确的。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值