Python_LDA实现方法详解


Posted in Python onOctober 25, 2017

LDA(Latent Dirichlet allocation)模型是一种常用而用途广泛地概率主题模型。其实现一般通过Variational inference和Gibbs Samping实现。作者在提出LDA模型时给出了其变分推理的C源码(后续贴出C++改编的类),这里贴出基于Python的第三方模块改写的LDA类及实现。

#coding:utf-8
import numpy as np
import lda
import lda.datasets
import jieba
import codecs
class LDA_v20161130():
  def __init__(self, topics=2):
    self.n_topic = topics
    self.corpus = None
    self.vocab = None
    self.ppCountMatrix = None
    self.stop_words = [u',', u'。', u'、', u'(', u')', u'·', u'!', u' ', u':', u'“', u'”', u'\n']
    self.model = None
  def loadCorpusFromFile(self, fn):
    # 中文分词
    f = open(fn, 'r')
    text = f.readlines()
    text = r' '.join(text)
    seg_generator = jieba.cut(text)
    seg_list = [i for i in seg_generator if i not in self.stop_words]
    seg_list = r' '.join(seg_list)
    # 切割统计所有出现的词纳入词典
    seglist = seg_list.split(" ")
    self.vocab = []
    for word in seglist:
      if (word != u' ' and word not in self.vocab):
        self.vocab.append(word)
    CountMatrix = []
    f.seek(0, 0)
    # 统计每个文档中出现的词频
    for line in f:
      # 置零
      count = np.zeros(len(self.vocab),dtype=np.int)
      text = line.strip()
      # 但还是要先分词
      seg_generator = jieba.cut(text)
      seg_list = [i for i in seg_generator if i not in self.stop_words]
      seg_list = r' '.join(seg_list)
      seglist = seg_list.split(" ")
      # 查询词典中的词出现的词频
      for word in seglist:
        if word in self.vocab:
          count[self.vocab.index(word)] += 1
      CountMatrix.append(count)
    f.close()
    #self.ppCountMatrix = (len(CountMatrix), len(self.vocab))
    self.ppCountMatrix = np.array(CountMatrix)
    print "load corpus from %s success!"%fn
  def setStopWords(self, word_list):
    self.stop_words = word_list
  def fitModel(self, n_iter = 1500, _alpha = 0.1, _eta = 0.01):
    self.model = lda.LDA(n_topics=self.n_topic, n_iter=n_iter, alpha=_alpha, eta= _eta, random_state= 1)
    self.model.fit(self.ppCountMatrix)
  def printTopic_Word(self, n_top_word = 8):
    for i, topic_dist in enumerate(self.model.topic_word_):
      topic_words = np.array(self.vocab)[np.argsort(topic_dist)][:-(n_top_word + 1):-1]
      print "Topic:",i,"\t",
      for word in topic_words:
        print word,
      print
  def printDoc_Topic(self):
    for i in range(len(self.ppCountMatrix)):
      print ("Doc %d:((top topic:%s) topic distribution:%s)"%(i, self.model.doc_topic_[i].argmax(),self.model.doc_topic_[i]))
  def printVocabulary(self):
    print "vocabulary:"
    for word in self.vocab:
      print word,
    print
  def saveVocabulary(self, fn):
    f = codecs.open(fn, 'w', 'utf-8')
    for word in self.vocab:
      f.write("%s\n"%word)
    f.close()
  def saveTopic_Words(self, fn, n_top_word = -1):
    if n_top_word==-1:
      n_top_word = len(self.vocab)
    f = codecs.open(fn, 'w', 'utf-8')
    for i, topic_dist in enumerate(self.model.topic_word_):
      topic_words = np.array(self.vocab)[np.argsort(topic_dist)][:-(n_top_word + 1):-1]
      f.write( "Topic:%d\t"%i)
      for word in topic_words:
        f.write("%s "%word)
      f.write("\n")
    f.close()
  def saveDoc_Topic(self, fn):
    f = codecs.open(fn, 'w', 'utf-8')
    for i in range(len(self.ppCountMatrix)):
      f.write("Doc %d:((top topic:%s) topic distribution:%s)\n" % (i, self.model.doc_topic_[i].argmax(), self.model.doc_topic_[i]))
    f.close()

算法实现demo:

例如,抓取BBC川普当选的新闻作为语料,输入以下代码:

if __name__=="__main__":
  _lda = LDA_v20161130(topics=20)
  stop = [u'!', u'@', u'#', u',',u'.',u'/',u';',u' ',u'[',u']',u'$',u'%',u'^',u'&',u'*',u'(',u')',
      u'"',u':',u'<',u'>',u'?',u'{',u'}',u'=',u'+',u'_',u'-',u'''''']
  _lda.setStopWords(stop)
  _lda.loadCorpusFromFile(u'C:\\Users\Administrator\Desktop\\BBC.txt')
  _lda.fitModel(n_iter=1500)
  _lda.printTopic_Word(n_top_word=10)
  _lda.printDoc_Topic()
  _lda.saveVocabulary(u'C:\\Users\Administrator\Desktop\\vocab.txt')
  _lda.saveTopic_Words(u'C:\\Users\Administrator\Desktop\\topic_word.txt')
  _lda.saveDoc_Topic(u'C:\\Users\Administrator\Desktop\\doc_topic.txt')

因为语料全部为英文,因此这里的stop_words全部设置为英文符号,主题设置20个,迭代1500次。结果显示,文档148篇,词典1347词,总词数4174,在i3的电脑上运行17s。
Topic_words部分输出如下:

Topic: 0
to will and of he be trumps the what policy
Topic: 1 he would in said not no with mr this but
Topic: 2 for or can some whether have change health obamacare insurance
Topic: 3 the to that president as of us also first all
Topic: 4 trump to when with now were republican mr office presidential
Topic: 5 the his trump from uk who president to american house
Topic: 6 a to that was it by issue vote while marriage
Topic: 7 the to of an are they which by could from
Topic: 8 of the states one votes planned won two new clinton
Topic: 9 in us a use for obama law entry new interview
Topic: 10 and on immigration has that there website vetting action given

Doc_Topic部分输出如下:

Doc 0:((top topic:4) topic distribution:[ 0.02972973 0.0027027 0.0027027 0.16486486 0.32702703 0.19189189
0.0027027 0.0027027 0.02972973 0.0027027 0.02972973 0.0027027
0.0027027 0.0027027 0.02972973 0.0027027 0.02972973 0.0027027
0.13783784 0.0027027 ])
Doc 1:((top topic:18) topic distribution:[ 0.21 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.11 0.01 0.01 0.01
0.01 0.01 0.01 0.01 0.01 0.01 0.31 0.21])
Doc 2:((top topic:18) topic distribution:[ 0.02075472 0.00188679 0.03962264 0.00188679 0.00188679 0.00188679
0.00188679 0.15283019 0.00188679 0.02075472 0.00188679 0.24716981
0.00188679 0.07735849 0.00188679 0.00188679 0.00188679 0.00188679
0.41698113 0.00188679])

当然,对于英文语料,需要排除大部分的虚词以及常用无意义词,例如it, this, there, that...在实际操作中,需要合理地设置参数。

换中文语料尝试,采用习大大就卡斯特罗逝世发表的吊唁文章和朴槿惠辞职的新闻。

Topic: 0
的 同志 和 人民 卡斯特罗 菲德尔 古巴 他 了 我
Topic: 1 在 朴槿惠 向 表示 总统 对 将 的 月 国民
Doc 0:((top topic:0) topic distribution:[ 0.91714123 0.08285877])
Doc 1:((top topic:1) topic distribution:[ 0.09200666 0.90799334])

还是存在一些虚词,例如“的”,“和”,“了”,“对”等词的干扰,但是大致来说,两则新闻的主题分布很明显,效果还不赖。

总结

以上就是本文关于Python_LDA实现方法详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:python+mongodb数据抓取详细介绍、Python探索之创建二叉树、Python探索之修改Python搜索路径等,有什么问题可以随时留言,欢迎大家一起交流讨论。感谢朋友们对本站的支持!

Python 相关文章推荐
使用setup.py安装python包和卸载python包的方法
Nov 27 Python
给你选择Python语言实现机器学习算法的三大理由
Nov 15 Python
Django实现全文检索的方法(支持中文)
May 14 Python
Pandas 数据处理,数据清洗详解
Jul 10 Python
Python中fnmatch模块的使用详情
Nov 30 Python
Django 多环境配置详解
May 14 Python
解决python xx.py文件点击完之后一闪而过的问题
Jun 24 Python
Python搭建代理IP池实现获取IP的方法
Oct 27 Python
Python中文分词库jieba,pkusegwg性能准确度比较
Feb 11 Python
Python制作数据预测集成工具(值得收藏)
Aug 21 Python
matplotlib 三维图表绘制方法简介
Sep 20 Python
pandas取dataframe特定行列的实现方法
May 24 Python
python+mongodb数据抓取详细介绍
Oct 25 #Python
python装饰器实例大详解
Oct 25 #Python
Python3 模块、包调用&amp;路径详解
Oct 25 #Python
Python探索之创建二叉树
Oct 25 #Python
Python探索之修改Python搜索路径
Oct 25 #Python
python中 logging的使用详解
Oct 25 #Python
python下载文件记录黑名单的实现代码
Oct 24 #Python
You might like
ThinkPHP自动验证失败的解决方法
2011/06/09 PHP
ThinkPHP实现多数据库连接的解决方法
2014/07/01 PHP
CodeIgniter配置之routes.php用法实例分析
2016/01/19 PHP
Windows Server 2008 R2和2012中PHP连接MySQL过慢的解决方法
2016/07/02 PHP
laravel中的错误与日志用法详解
2016/07/26 PHP
php PDO属性设置与操作方法分析
2018/12/27 PHP
Locate a File Using a File Open Dialog Box
2007/06/18 Javascript
分享27款非常棒的jQuery 表单插件
2011/03/28 Javascript
actionscript与javascript的区别
2011/05/25 Javascript
含有CKEditor的表单如何提交
2014/01/09 Javascript
用js通过url传参把数据从一个页面传到另一个页面
2014/09/01 Javascript
javascript记录文本框内文字个数检测文字个数变化
2014/10/14 Javascript
JavaScript中pop()方法的使用教程
2015/06/09 Javascript
JS+CSS实现简易实用的滑动门菜单效果
2015/09/18 Javascript
JS实现的仿淘宝交易倒计时效果
2015/11/27 Javascript
简单的jQuery banner图片轮播实例代码
2016/03/04 Javascript
用js控件div的滚动条,让它在内容更新时自动滚到底部的实现方法
2016/10/27 Javascript
Javascript 使用ajax与C#获取文件大小实例详解
2017/01/13 Javascript
Bootstrap面板学习使用
2017/02/09 Javascript
js从输入框读取内容,比较两个数字的大小方法
2017/03/13 Javascript
让Vue也可以使用Redux的方法
2018/05/23 Javascript
layui 根据后台数据动态创建下拉框并同时默认选中的实例
2019/09/02 Javascript
vue 开发企业微信整合案例分析
2019/12/02 Javascript
vue-resourc发起异步请求的方法
2020/02/11 Javascript
微信小程序 button样式设置为图片的方法
2020/06/19 Javascript
[01:07]2015国际邀请赛 中国区预选赛精彩回顾
2015/06/15 DOTA
python的dict,set,list,tuple应用详解
2014/07/24 Python
Python使用Dijkstra算法实现求解图中最短路径距离问题详解
2018/05/16 Python
python输出第n个默尼森数的实现示例
2020/03/08 Python
python 解决pycharm运行py文件只有unittest选项的问题
2020/09/01 Python
Canvas 帧动画吃苹果小游戏
2020/08/05 HTML / CSS
美国女士泳装店:Swimsuits For All
2017/03/02 全球购物
销售活动策划方案
2014/08/26 职场文书
怀孕辞职信怎么写
2015/02/28 职场文书
音乐之声观后感
2015/06/04 职场文书
一文搞懂python异常处理、模块与包
2021/06/26 Python