python应用:主题分类(gensim lda)
安装第三方包:gensim
首先,执行去停词操作(去除与主题无关的词)
#-*-coding:utf8-*- import jieba def stopwordslist(filepath):
stopwords = [line.strip() for line in open(filepath, 'r').readlines()]
return stopwords def seg_sentence(sentence):
sentence_seged = jieba.cut(sentence.strip())
stopwords = stopwordslist('stopWords/stopwords.txt')
outstr = ''
for word in sentence_seged:
word = word.lower()
if word not in stopwords:
if word != '\t':
outstr += word
outstr += " "
return outstr inputs = open('input/copurs.txt', 'r') outputs = open('input/copurs_out.txt', 'w')
for line in inputs:
line_seg = seg_sentence(line)
outputs.write(line_seg + '\n')
outputs.close()
inputs.close()
然后,执行主题分类操作
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/copurs_out.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] #########Run the LDA model for XX topics ###############################
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=50,passes=2000)
doc_topic = [a for a in lda[corpus]] ####### write the topics in file topics_result.txt ####################
topics_r = lda.print_topics(num_topics = 50, num_words = 10)
topic_name = codecs.open('output/topics_result.txt','w')
for v in topics_r:
topic_name.write(str(v)+'\n') ###################### write the class results to file #########################
###################### each document belongs to which topic ###################### fp2 = codecs.open('output/documents_result.txt','w')
for t in doc_topic:
c = []
c.append([a[1] for a in 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
################################ OVER ############################################
注意:上述主题分类,仅使用lda模型(根据频数计算)
也可混合使用tf-idf模型XX-topic下代码改为如下即可:
方式一
#########Run the LDA model for XX topics ###############################
lda = LdaModel(corpus=corpus_tfidf, id2word=dictionary, num_topics=50,passes=2000)
doc_topic = [a for a in lda[corpus_tfidf]]
或
方式二
#########Run the LDA model for XX topics ###############################
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=50,passes=2000)
doc_topic = [a for a in lda[corpus_tfidf]]
常用方式为方式一,作者暂时为弄清楚这两种方式的区别,后期将会继续完善
python应用:主题分类(gensim lda)的更多相关文章
- 主题模型(LDA)(一)--通俗理解与简单应用
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_39422642/article/de ...
- 文本主题模型之LDA(一) LDA基础
文本主题模型之LDA(一) LDA基础 文本主题模型之LDA(二) LDA求解之Gibbs采样算法 文本主题模型之LDA(三) LDA求解之变分推断EM算法(TODO) 在前面我们讲到了基于矩阵分解的 ...
- 文本主题模型之LDA(二) LDA求解之Gibbs采样算法
文本主题模型之LDA(一) LDA基础 文本主题模型之LDA(二) LDA求解之Gibbs采样算法 文本主题模型之LDA(三) LDA求解之变分推断EM算法(TODO) 本文是LDA主题模型的第二篇, ...
- 文本主题模型之LDA(三) LDA求解之变分推断EM算法
文本主题模型之LDA(一) LDA基础 文本主题模型之LDA(二) LDA求解之Gibbs采样算法 文本主题模型之LDA(三) LDA求解之变分推断EM算法 本文是LDA主题模型的第三篇,读这一篇之前 ...
- python的数据结构分类,以及数字的处理函数,类型判断
python的数据结构分类: 数值型 int:python3中都是长整形,没有大小限制,受限内存区域的大小 float:只有双精度型 complex:实数和虚数部分都是浮点型,1+1.2J bool: ...
- Gensim LDA主题模型实验
本文利用gensim进行LDA主题模型实验,第一部分是基于前文的wiki语料,第二部分是基于Sogou新闻语料. 1. 基于wiki语料的LDA实验 上一文得到了wiki纯文本已分词语料 wiki.z ...
- gensim LDA模型提取每篇文档所属主题(概率最大主题所在)
gensim的LDA算法中很容易提取到每篇文章的主题分布矩阵,但是一般地还需要进一步获取每篇文章归属到哪个主题概率最大的数据,这个在检索gensim文档和网络有关文章后,发现竟然没有. 简单写了一下. ...
- LDA模型应用实践-希拉里邮件主题分类
#coding=utf8 import numpy as np import pandas as pd import re from gensim import corpora, models, si ...
- 转:Python 文本挖掘:使用gensim进行文本相似度计算
Python使用gensim进行文本相似度计算 转于:http://rzcoding.blog.163.com/blog/static/2222810172013101895642665/ 在文本处理 ...
随机推荐
- requireJS的优化工具 ---- r.js
requireJS是javascript的模块加载器,是基于AMD规范实现的. r.js是其提供的对模块进行打包和构建的一个工具 下载 r.js 创建r.js 的配置文件 build.js build ...
- matlab练习程序(演化策略ES)
还是这本书上的内容,不过我看演化计算这一章是倒着看的,这里练习的算法正好和书中介绍的顺序是相反的. 演化策略是最古老的的演化算法之一,和上一篇DE算法类似,都是基于种群的随机演化产生最优解的算法. 算 ...
- installed_oracle_can't_use
Preface 1.my server is windowsxp 2.database is the oralce 10g step A.CHECK SERVER 1.win + r cmd sqlp ...
- NodeJs安装less(npm方式)
上一次讲了如何在浏览器端解析less文件,这次是在cmd中使用npm中的less模块来解析 详解如下 首下我们去下载一个https://nodejs.org/en/, 一路next之后,因为文件不 ...
- TOEFL考试(一年半的复仇,裸考)
8/11/2018 had a TOEFL test without preparation. Reading (worry about too much, not familiar with the ...
- python入门20 导入模块(引包)
1 引包: import module 或 import module.module1 或 from module import module1,module2...等 2 import xx ...
- POJ-2828 Buy Tickets---线段树+逆序
题目链接: https://cn.vjudge.net/problem/POJ-2828 题目大意: 插队的问题,每个案例给出n,代表有n个插队的,每个给出p,v,意思是代号为v的人插在了第p个人的后 ...
- UVA515 King
嘟嘟嘟 题目翻译:有n个数,m个限制条件.每一个限制条件形如:1.x y gt c:表示ax + ax+1 + … +ay > c.2.x y It c:表示ax + ax+1 + …… +ay ...
- vue常用事件
一.事件监听 1. banner_edit.$watch('bannerForm.type', function () { //执行其他代码 console.log(666); this.banner ...
- git bush 代码提交
# git add . # git commit -m"init project" # git push