主题分布的不同维度

时间:2016-12-15 15:42:54

标签: r lda topic-modeling

我想将所有文档划分为10个主题,除了主题的分布维度和协方差矩阵外,它与收敛结果相吻合。
为什么主题分布是9维向量而不是10,它们的协方差矩阵是9 * 9矩阵而不是10 * 10?

我使用library(topicmodels)和函数CTM()来实现中文主题模型。

我的代码如下:

library(rJava);
library(Rwordseg); 
library(NLP);
library(tm); 
library(tmcn)
library(tm)
library(Rwordseg)
library(topicmodels)

installDict("C:\\Users\\Jeffy\\OneDrive\\Workplace\\R\\Law.scel","Law");
installDict("C:\\Users\\Jeffy\\OneDrive\\Workplace\\R\\NationalInstitution.scel","NationalInstitution");
installDict("C:\\Users\\Jeffy\\OneDrive\\Workplace\\R\\Place.scel","Place");
installDict("C:\\Users\\Jeffy\\OneDrive\\Workplace\\R\\Psychology.scel","Psychology");
installDict("C:\\Users\\Jeffy\\OneDrive\\Workplace\\R\\Politics.scel","Politics");
listDict();

#read file
d.vec <- segmentCN("samgovWithoutID.csv", returnType = "tm")
samgov.segment <- read.table("samgovWithoutID.segment.csv", header = TRUE, fill = TRUE, stringsAsFactors = F, sep = ",",fileEncoding='utf-8')
fix(samgov.segment)

# create DTM(document term matrix)
d.corpus <- Corpus(VectorSource(samgov.segment$content))
inspect(d.corpus[1:10])
d.corpus <- tm_map(d.corpus, removeWords, stopwordsCN())
ctrl <- list(removePunctuation = TRUE, removeNumbers= TRUE, wordLengths = c(1, Inf), stopwords = stopwordsCN(), wordLengths = c(2, Inf))
d.dtm <- DocumentTermMatrix(d.corpus, control = ctrl)
inspect(d.dtm[1:10, 110:112])

# impletment topic models
ctm10<-CTM(d.dtm,k=10, control=list(seed=2014012692))
Terms10 <- terms(ctm10, 10)
Terms10[,1:10]

ctm20<-CTM(d.dtm,k=20, control=list(seed=2014012692))
Terms20 <- terms(ctm20, 20)
Terms20[,1:20]

R Studio中的结果(参见突出显示的部分):

enter image description here

帮助文档:

enter image description here

1 个答案:

答案 0 :(得分:1)

超过10个值的概率分布有9个自由参数:一旦我告诉你前9个概率,最后一个值的概率必须是1减去这些概率的总和。

10维逻辑正态分布相当于从高斯分布中对10维向量进行采样,然后进行压缩&#34;该向量通过取幂并将其归一化为1.0。有无数个10维向量将对同一个10维概率分布进行取幂和归一化 - 你只需要为每个值添加一个任意常数c。这是因为高斯的平均值有10个自由参数,比受约束的分布多一个。

有几种方法可以使高斯&#34;可识别&#34;。一种是将平均向量的一个元素固定为0.0。这就是为什么你看到一个9维均值和协方差矩阵的原因:第10个值总是0,没有变化。

相关问题