使用gensim中的lda模型训练主题分布--print_topics使用

转载:https://blog.csdn.net/accumulate_zhang/article/details/62453672

一直在寻找各种大神的LDA算法,不过调试一直没有成功,最后还是选择使用gensim的LDA工具来训练自己的文本数据吧。

  1. #coding=utf-8
  2. import codecs
  3. from gensim import corpora
  4. from gensim.models import LdaModel
  5. from gensim.corpora import Dictionary
  6. fr=open( 'cleanChiSegments.txt', 'r')
  7. train=[]
  8. for line in fr.readlines():
  9. line=line.split( ' ')
  10. train.append(line)
  11. print len(train)
  12. print ' '.join(train[ 2])
  13. dictionary = corpora.Dictionary(train)
  14. corpus = [ dictionary.doc2bow(text) for text in train ]
  15. lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics= 100)
  16. topic_list=lda.print_topics( 20)
  17. print type(lda.print_topics( 20))
  18. print len(lda.print_topics( 20))
  19. for topic in topic_list:
  20. print topic
  21. print "第一主题"
  22. print lda.print_topic( 1)
  23. print '给定一个新文档,输出其主题分布'
  24. #test_doc = list(new_doc) #新文档进行分词
  25. test_doc=train[ 2] #查看训练集中第三个样本的主题分布
  26. doc_bow = dictionary.doc2bow(test_doc) #文档转换成bow
  27. doc_lda = lda[doc_bow] #得到新文档的主题分布
  28. #输出新文档的主题分布
  29. print doc_lda
  30. for topic in doc_lda:
  31. print "%s\t%f\n"%(lda.print_topic(topic[ 0]), topic[ 1])



----------------------------------------------------------下面输出上面工具代码的运行结果---------------------------------------------------

下面输出的是前20个topic-word分布


对训练集中第三个样本测试,基于训练集得到的主题模型,输出其主题分布。

即表示新文本的doc-topic分布,以及每个主题下的topic-word分布

扫描二维码关注公众号,回复: 1950909 查看本文章


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/accumulate_zhang/article/details/62453672
个人分类:  机器学习 word2vec

转载:https://blog.csdn.net/accumulate_zhang/article/details/62453672

一直在寻找各种大神的LDA算法,不过调试一直没有成功,最后还是选择使用gensim的LDA工具来训练自己的文本数据吧。

  1. #coding=utf-8
  2. import codecs
  3. from gensim import corpora
  4. from gensim.models import LdaModel
  5. from gensim.corpora import Dictionary
  6. fr=open( 'cleanChiSegments.txt', 'r')
  7. train=[]
  8. for line in fr.readlines():
  9. line=line.split( ' ')
  10. train.append(line)
  11. print len(train)
  12. print ' '.join(train[ 2])
  13. dictionary = corpora.Dictionary(train)
  14. corpus = [ dictionary.doc2bow(text) for text in train ]
  15. lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics= 100)
  16. topic_list=lda.print_topics( 20)
  17. print type(lda.print_topics( 20))
  18. print len(lda.print_topics( 20))
  19. for topic in topic_list:
  20. print topic
  21. print "第一主题"
  22. print lda.print_topic( 1)
  23. print '给定一个新文档,输出其主题分布'
  24. #test_doc = list(new_doc) #新文档进行分词
  25. test_doc=train[ 2] #查看训练集中第三个样本的主题分布
  26. doc_bow = dictionary.doc2bow(test_doc) #文档转换成bow
  27. doc_lda = lda[doc_bow] #得到新文档的主题分布
  28. #输出新文档的主题分布
  29. print doc_lda
  30. for topic in doc_lda:
  31. print "%s\t%f\n"%(lda.print_topic(topic[ 0]), topic[ 1])



----------------------------------------------------------下面输出上面工具代码的运行结果---------------------------------------------------

下面输出的是前20个topic-word分布


对训练集中第三个样本测试,基于训练集得到的主题模型,输出其主题分布。

即表示新文本的doc-topic分布,以及每个主题下的topic-word分布



猜你喜欢

转载自blog.csdn.net/m0_37870649/article/details/80897406