概率语言模型及其变形系列(5)-LDA Gibbs Sampling 的JAVA实现

本系列博文介绍常见概率语言模型及其变形模型,主要总结PLSA、LDA及LDA的变形模型及参数Inference方法。初步计划内容如下

第一篇:PLSA及EM算法

第二篇:LDA及Gibbs Samping

第三篇:LDA变形模型-Twitter LDA,TimeUserLDA,ATM,Labeled-LDA,MaxEnt-LDA等

第四篇:基于变形LDA的paper分类总结(bibliography)

第五篇:LDA Gibbs Sampling 的JAVA实现


第五篇 LDA Gibbs Sampling的JAVA 实现

在本系列博文的前两篇,我们系统介绍了PLSA, LDA以及它们的参数Inference 方法,重点分析了模型表示和公式推导部分。曾有位学者说,“做研究要顶天立地”,意思是说做研究空有模型和理论还不够,我们还得有扎实的程序code和真实数据的实验结果来作为支撑。本文就重点分析 LDA Gibbs Sampling的JAVA 实现,并给出apply到newsgroup18828新闻文档集上得出的Topic建模结果。

本项目Github地址 https://github.com/yangliuy/LDAGibbsSampling


1、文档集预处理

要用LDA对文本进行topic建模,首先要对文本进行预处理,包括token,去停用词,stem,去noise词,去掉低频词等等。当语料库比较大时,我们也可以不进行stem。然后将文本转换成term的index表示形式,因为后面实现LDA的过程中经常需要在term和index之间进行映射。Documents类的实现如下,里面定义了Document内部类,用于描述文本集合中的文档。

package liuyang.nlp.lda.main;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import liuyang.nlp.lda.com.FileUtil;
import liuyang.nlp.lda.com.Stopwords;

/**Class for corpus which consists of M documents
 * @author yangliu
 * @blog http://blog.csdn.net/yangliuy
 * @mail [email protected]
 */

public class Documents {
	
	ArrayList<Document> docs; 
	Map<String, Integer> termToIndexMap;
	ArrayList<String> indexToTermMap;
	Map<String,Integer> termCountMap;
	
	public Documents(){
		docs = new ArrayList<Document>();
		termToIndexMap = new HashMap<String, Integer>();
		indexToTermMap = new ArrayList<String>();
		termCountMap = new HashMap<String, Integer>();
	}
	
	public void readDocs(String docsPath){
		for(File docFile : new File(docsPath).listFiles()){
			Document doc = new Document(docFile.getAbsolutePath(), termToIndexMap, indexToTermMap, termCountMap);
			docs.add(doc);
		}
	}
	
	public static class Document {	
		private String docName;
		int[] docWords;
		
		public Document(String docName, Map<String, Integer> termToIndexMap, ArrayList<String> indexToTermMap, Map<String, Integer> termCountMap){
			this.d
  • 23
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 104
    评论
LDA(Latent Dirichlet Allocation)模型是一种无监督机器学习模型,用于对文本数据进行主题建模。其基本思想是假设每篇文档都由若干个主题组成,每个主题又由若干个单词组成。 以下是LDA模型实现步骤: 1. 预处理文本数据,包括分词、去除停用词等操作。 2. 建立文档-词矩阵,每行代表一个文档,每列代表一个单词,矩阵中的元素表示该单词在该文档中出现的次数。 3. 设置LDA模型的参数,包括主题数、迭代次数、超参数等。 4. 定义LDA模型并训练模型。训练过程中,模型会随机给每个单词分配一个主题,并更新每个单词所属主题的概率分布,直到模型收敛。 5. 输出每个主题的关键词,以及每篇文档所包含的主题及其概率分布。 Python中可以使用gensim库实现LDA模型。以下是一个简单的示例代码: ```python import gensim from gensim import corpora # 读取文本数据 documents = ["I love machine learning", "I hate studying", "Machine learning is interesting"] # 分词、去除停用词等预处理操作 # 建立文档-词矩阵 dictionary = corpora.Dictionary(documents) corpus = [dictionary.doc2bow(doc) for doc in documents] # 设置LDA模型参数 num_topics = 2 iterations = 50 passes = 10 # 定义LDA模型并训练模型 lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics, iterations=iterations, passes=passes) # 输出每个主题的关键词 print(lda_model.print_topics(num_topics=num_topics, num_words=3)) # 输出每篇文档所包含的主题及其概率分布 for doc in corpus: print(lda_model[doc]) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 104
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值