import codecs  #主题模型
from gensim import corpora
from gensim.models import LdaModel
from gensim import models
from gensim.corpora import Dictionary
te = []
fp = codecs.open('input.txt','r')
for line in fp:
line = line.split(',')
te.append([ w for w in line ])
print ('输入文本数量:',len(te))
dictionary = corpora.Dictionary(te)
corpus = [ dictionary.doc2bow(text) for text in te ]
tfidf = models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]
print(list(corpus_tfidf))#输出词的tfidf
print(list(corpus))#输出文本向量空间
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=20,passes=100)
doc_topic = [a for a in lda[corpus]]
topics_r = lda.print_topics(num_topics = 20, num_words =20)
topic_name = codecs.open('topics_result3.txt','w')
for v in topics_r:
topic_name.write(str(v)+'\n')
fp2 = codecs.open('documents_result.txt','w')
for t in doc_topic:
c = []
c.append([a[1] for a in t])
print(t)
m = max(c[0]) for i in range(0, len(t)):
if m in t[i]:
#print(t[i])
fp2.write(str(t[i][0]) + ' ' + str(t[i][1]) + '\n')#输出模型类和概览
break

python3 LDA主题模型以及TFIDF实现的更多相关文章

  1. Gensim LDA主题模型实验

    本文利用gensim进行LDA主题模型实验,第一部分是基于前文的wiki语料,第二部分是基于Sogou新闻语料. 1. 基于wiki语料的LDA实验 上一文得到了wiki纯文本已分词语料 wiki.z ...

  2. 用scikit-learn学习LDA主题模型

    在LDA模型原理篇我们总结了LDA主题模型的原理,这里我们就从应用的角度来使用scikit-learn来学习LDA主题模型.除了scikit-learn,  还有spark MLlib和gensim库 ...

  3. 机器学习入门-文本特征-使用LDA主题模型构造标签 1.LatentDirichletAllocation(LDA用于构建主题模型) 2.LDA.components(输出各个词向量的权重值)

    函数说明 1.LDA(n_topics, max_iters, random_state)  用于构建LDA主题模型,将文本分成不同的主题 参数说明:n_topics 表示分为多少个主题, max_i ...

  4. [综] Latent Dirichlet Allocation(LDA)主题模型算法

    多项分布 http://szjc.math168.com/book/ebookdetail.aspx?cateid=1&&sectionid=983 二项分布和多项分布 http:// ...

  5. R语言︱LDA主题模型——最优主题数选取(topicmodels)+LDAvis可视化(lda+LDAvis)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 笔者寄语:在自己学LDA主题模型时候,发现该模 ...

  6. Spark:聚类算法之LDA主题模型算法

    http://blog.csdn.net/pipisorry/article/details/52912179 Spark上实现LDA原理 LDA主题模型算法 [主题模型TopicModel:隐含狄利 ...

  7. R语言︱LDA主题模型——最优主题...

    R语言︱LDA主题模型——最优主题...:https://blog.csdn.net/sinat_26917383/article/details/51547298#comments

  8. 自然语言处理之LDA主题模型

    1.LDA概述 在机器学习领域,LDA是两个常用模型的简称:线性判别分析(Linear Discriminant Analysis)和 隐含狄利克雷分布(Latent Dirichlet Alloca ...

  9. 理解 LDA 主题模型

    前言 gamma函数 0 整体把握LDA 1 gamma函数 beta分布 1 beta分布 2 Beta-Binomial 共轭 3 共轭先验分布 4 从beta分布推广到Dirichlet 分布 ...

随机推荐

  1. 2015年上海现场赛重现 (A几何, K暴力搜索)

    A: 题目链接 :https://vjudge.net/contest/250823#problem/A 参考 : https://www.cnblogs.com/helenawang/p/54654 ...

  2. 安利一款强大的学习软件XMind(顺便放上这几天制作的JavaSE的思维导图day1-day4)

    最近在学习Java,并且在使用一款非常酷炫无敌吊炸天的软件,思维导图制作神器-XMind,然后就像分享给大家,至于XMind是什么大家自行百度,在这里我就不赘述了 我这里说下我认为的好的实用的常用快捷 ...

  3. 网络-02-端口号-linux端口详解大全

    端口详解 1 tcpmux TCP Port Service Multiplexer 传输控制协议端口服务多路开关选择器  2 compressnet Management Utility compr ...

  4. Qt QML 2D shader

    --------------------------------------------------- Qt quick 2d shader effect ---------------------- ...

  5. Web前端方向课程要点:CSS3渐变制作过程

    CSS3 渐变 CSS3 渐变(gradient)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必须使用图像来实现这些效果,现在通过使用 CSS3 的渐变(gradients)即可实现. ...

  6. Oarcle之用户管理 与 DCL

    用户管理 1.创建一个账户 create user zhangsan identified by123456: 2.修改账户的密码 alter user zhangsan identified by ...

  7. js识别ESC并关闭开窗

    $(document).keyup(function(event) { switch (event.keyCode) { //ESC默认code为27 case 27: var index = par ...

  8. etcd v3 备份恢复

    备份数据: # ETCDCTL_API=3 etcdctl --endpoints localhost:2379 snapshot save snapshot.db 恢复数据: # ETCDCTL_A ...

  9. springboot整合redis(简单整理)

    Redis安装与开启 我这里是在windows上练习,所以这里的安装是指在windows上的安装,操作非常简单,点击https://github.com/MicrosoftArchive/redis/ ...

  10. 移动端js调试工具:eruda

    通常写前端页面都在Chrome浏览器的开发模式下进行调试,但是写放在移动端的H5页面时,有时候会遇到在Chrome上调试没有问题,但是在手机的浏览器上有问题的情况:或者有些功能只能在特定的容器中才能其 ...