2016-12-15 99 views
-2

我想將所有文檔劃分爲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工作室(見突出顯示的部分):

enter image description here

幫助文檔:

enter image description here

+3

請提供[可重現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – figurine

+0

Thx爲您的評論! – Jeffy

回答

1

概率分配超過10 VA lue有9個免費參數:一旦我告訴你前9的概率,最後一個值的概率必須是1減去那些概率的總和。

10維邏輯正態分佈相當於從高斯分佈中採樣10維矢量,然後通過對矢量求冪並將其歸一化爲1.0來「擠壓」該矢量。有無限數量的10維矢量將以相同的10維概率分佈指數化和歸一化 - 您只需爲每個值添加一個任意常數c。這是因爲高斯的均值具有10個自由參數,比受約束的分佈多一個。

有幾種方法可以使高斯「可識別」。一個是修復平均向量的元素之一爲0.0。這就是爲什麼你會看到一個9維平均值和協方差矩陣:第10個值始終爲0,沒有方差。