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/ 在文本处理 ...
随机推荐
- mysql 省市数据
CREATE TABLE `province` ( `id` ) DEFAULT NULL, `name` ) DEFAULT NULL ) ENGINE=INNODB DEFAULT CHARSET ...
- php连接MySQL分析
Mysql:在PHP脚本中操作MySQL数据库的的几个步骤如下: 1.连接MySQL数据库服务器,并判断是否连接正确 2.选择数据库,并设置字符集(可选) 3.执行SQL命令 4.处理结果集 5.关闭 ...
- JSP中include动作与指令
include指令 JSP中有三大指令:page,include,taglib,之前已经说过了page的用法.这里介绍下include. 使用语法如下: <%@ include file=&qu ...
- 使flex-direction: column的子元素height: 100%生效的办法
在flex-direction: column子元素里直接使用height:100%,height并不会被设置成100% <!DOCTYPE html> <html lang=&qu ...
- MySQL:数据库入门篇4
1. 视图 创建视图 create view 视图名字 as 查询sql语句; drop view 视图名字; alter view 视图名字 as 查询sql语句; 2. 触发器 1. 插入事件触发 ...
- Python:IPC-Pipe与IPC-Manger
1,IPC-PIPE: 管道 pipe from multiprocessing import Process from multiprocessing import Pipe p1,p2 = Pip ...
- PHP设计模式——适配器模式
<?php /** * 适配器模式 * 适配器模式是将某个对象的接口适配为另一个对象所期望的接口 * * 在需要转化一个对象的接口用于另一个对象时,最好实现适配器模式对象 */ class We ...
- Java中获取classpath路径下的资源文件
ClassLoader 提供了两个方法用于从装载的类路径中取得资源: public URL getResource (String name); public InputStream getRes ...
- div可编辑框,去除粘贴文字样式😄
上个月做了个聊天的需求(网页版的).说到聊天都想到输入框,说到输入框都会想到input,但是input标签是不支持插入图片的(包括areatext标签).查阅了一些资料就看到div标签有一个属性con ...
- darknet53 yolo 下的识别训练
[目录] 一. 安装Darknet(仅CPU下) 2 1.1在CPU下安装Darknet方式 2 1.2在GPU下安装Darknet方式 4 二. YOLO.V3训练官网数据集(VOC数据集/COCO ...