LDA模型学习(代码)

本文介绍如何将LDA模型应用于文本聚类,过程中涉及到的概率论和数学知识,以及通过Mallet库尝试实现,但因示例不足而转向其他源代码。最终采用文本分词和词频统计构建document矩阵进行操作。
摘要由CSDN通过智能技术生成

 为了把LDA算法用于文本聚类,我真的是绞尽脑汁。除了去看让我头大的概率论、随机过程、高数这些基础的数学知识,还到网上找已经实现的源代码。

     最先让我看到署光的是Mallet,我研究了大概一个星期,最后决定放弃了。因为Mallet作者提供的例子实在太少了。

      回到了网上找到的这样一段源代码:

 
 
 
  1. /*  
  2.  * (C) Copyright 2005, Gregor Heinrich (gregor :: arbylon : net) (This file is  
  3.  * part of the org.knowceans experimental software packages.)  
  4.  */ 
  5. /*  
  6.  * LdaGibbsSampler is free software; you can redistribute it and/or modify it  
  7.  * under the terms of the GNU General Public License as published by the Free  
  8.  * Software Foundation; either version 2 of the License, or (at your option) any  
  9.  * later version.  
  10.  */ 
  11. /*  
  12.  * LdaGibbsSampler is distributed in the hope that it will be useful, but  
  13.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or  
  14.  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more  
  15.  * details.  
  16.  */ 
  17. /*  
  18.  * You should have received a copy of the GNU General Public License along with  
  19.  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple  
  20.  * Place, Suite 330, Boston, MA 02111-1307 USA  
  21.  */ 
  22.  
  23. /*  
  24.  * Created on Mar 6, 2005  
  25.  */ 
  26. package com.xh.lda;  
  27.  
  28. import java.text.DecimalFormat;  
  29. import java.text.NumberFormat;  
  30.  
  31. /**  
  32.  * Gibbs sampler for estimating the best assignments of topics for words and  
  33.  * documents in a corpus. The algorithm is introduced in Tom Griffiths' paper  
  34.  * "Gibbs sampling in the generative model of Latent Dirichlet Allocation"  
  35.  * (2002).  
  36.  *   
  37.  * @author heinrich  
  38.  */ 
  39. public class LdaGibbsSampler {  
  40.  
  41.     /**  
  42.      * document data (term lists)  
  43.      */ 
  44.     int[][] documents;  
  45.  
  46.     /**  
  47.      * vocabulary size  
  48.      */ 
  49.     int V;  
  50.  
  51.     /**  
  52.      * number of topics  
  53.      */ 
  54.     int K;  
  55.  
  56.     /**  
  57.      * Dirichlet parameter (document--topic associations)  
  58.      */ 
  59.     double alpha;  
  60.  
  61.     /**  
  62.      * Dirichlet parameter (topic--term associations)  
  63.      */ 
  64.     double beta;  
  65.  
  66.     /**  
  67.      * topic assignments for each word.  
  68.      */ 
  69.     int z[][];  
  70.  
  71.     /**  
  72.      * cwt[i][j] number of instances of word i (term?) assigned to topic j.  
  73.      */ 
  74.     int[][] nw;  
  75.  
  76.     /**  
  77.      * na[i][j] number of words in document i assigned to topic j.  
  78.      */ 
  79.     int[][] nd;  
  80.  
  81.     /**  
  82.      * nwsum[j] total number of words assigned to topic j.  
  83.      */ 
  84.     int[] nwsum;  
  85.  
  86.     /**  
  87.      * nasum[i] total number of words in document i.  
  88.      */ 
  89.     int[] ndsum;  
  90.  
  91.     /**  
  92.      * cumulative statistics of theta  
  93.      */ 
  94.     double[][] thetasum;  
  95.  
  96.     /**  
  97.      * cumulative statistics of phi  
  98.      */ 
  99.     double[][] phisum;  
  100.  
  101.     /**  
  102.      * size of statistics  
  103.      */ 
  104.     int numstats;  
  105.  
  106.     /**  
  107.      * sampling lag (?)  
  108.      */ 
  109.     private static int THIN_INTERVAL = 20;  
  110.  
  111.     /**  
  112.      * burn-in period  
  113.      */ 
  114.     private static 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值