2016-12-08 1 views
2

LDAvis 패키지와 함께 mallet 주제 모델을 사용하려고합니다. topic.model 개체의 To do so you must extract a number of parameters : phi, theta, vocab, doc.lengthterm.frequency입니다.말렛 LDA 개체에서 phi, theta, doc.length, vocab, term.frequency를 가져 옵니까?

mallet 에는 이러한 매개 변수에 대한 언급이 없습니다. mallet.import()MalletLDA()을 사용하여 데이터에서 생성 된 topic.model 개체에서 어떻게 그들을 추출 할 수 있습니까?

는 지금까지, 나는 주제 모델에 맞게 망치를 사용했습니다 :

id_numbers <- as.integer(c(1, 2, 3)) 

comments <- c("words to be used for text mining", "that may or may not be interesting", "but could serve as a good example") 

df <- data.frame(id_numbers, comments, stringsAsFactors = F) 

# Set up topic model 
library(mallet) 

stoplist <- c("to", "be", "or") 
write.csv(stoplist, file = "example_stoplist.csv") 

mallet.instances <- mallet.import(
    as.character(df$id_numbers), 
    as.character(df$comments), 
    "example_stoplist.csv", 
    FALSE, 
    token.regexp="[\\p{L}']+") 

topic.model <- MalletLDA(num.topics=10) 
topic.model$loadDocuments(mallet.instances) 
vocabulary <- topic.model$getVocabulary() 
word.freqs <- mallet.word.freqs(topic.model) 
topic.model$setAlphaOptimization(40, 80) # tweaking optimization interval and burn-in iterations) 
topic.model$train(400) 

topic.words.m <- mallet.topic.words(topic.model, smoothed=TRUE, 
           normalized=TRUE) 
dim(topic.words.m) 

vocabulary <- topic.model$getVocabulary() 
colnames(topic.words.m) <- vocabulary 

doc.topics.m <- mallet.doc.topics(topic.model, smoothed=T, 
           normalized=T) 


doc.topics.df <- as.data.frame(doc.topics.m) 
doc.topics.df <- cbind(id_numbers, doc.topics.df) 

doc.topic.means.df <- aggregate(doc.topics.df[, 2:ncol(doc.topics.df)], 
           list(doc.topics.df[,1]), 
           mean) 

이 중 내가 지금 LDAvis에 대한 JSON을 생성해야합니다.

# LDAvis 
library(LDAvis) 
phi <- t(mallet.topic.words(topic.model, smoothed = TRUE, normalized = TRUE)) 
phi.count <- mallet.topic.words(topic.model, smoothed = TRUE, normalized = FALSE) 

topic.words <- mallet.topic.words(topic.model, smoothed=TRUE, normalized=TRUE) 
topic.counts <- rowSums(topic.words) 

topic.proportions <- topic.counts/sum(topic.counts) 

vocab <- topic.model$getVocabulary() 

doc.tokens <- data.frame(id=c(1:nrow(doc.topics.m)), tokens=0) 
for(i in vocab){ 
    # Find word if word in text 
    matched <- grepl(i, df$comments) 
    doc.tokens[matched,2] =doc.tokens[matched,2] + 1 
} 

createJSON(phi = phi, 
      theta = doc.topics.m, 
      doc.length = doc.tokens, 
      vocab = vocab, 
      term.frequency = apply(phi.count, 1, sum)) 

그러나, 이것은 나에게 다음과 같은 오류 메시지를 제공합니다 :

Error in createJSON(phi = phi, theta = doc.topics.m, doc.length = doc.tokens, : 
    Number of rows of phi does not match 
     number of columns of theta; both should be equal to the number of topics 
     in the model. 

그래서 나는 피 잘못된 방법으로 세타 행렬을 생성하는 것 같다 나는 다음 시도했다.

+2

예제 입력 데이터로 [reproducible example] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)을 제공하십시오. 당신이하려고하는 것을보고 가능한 해결책을 테스트 할 수 있습니다. – MrFlick

+0

말렛 오브젝트에서'str'은 무엇을 생성합니까? – emilliman5

+0

@ emilliman5 :'str (topic.model)'은 2 개의 슬롯이있는'formal class 'jobjRef'[package "rJava"]를 제공합니다. @ jobj : .. @jclass : chr "cc/mallet/topics/RTopicModel "' – histelheim

답변

2

phi을 작성한 행에서 행렬 전치 함수 t()을 제거해보십시오.

RMallet은 LDAvis에서 예상하는 형식으로 이러한 행렬을 반환합니다. 항목은 문서 주제 (theta)의 열과 주제 단어 행 (phi)입니다. 때로는 행이나 열이 항상 주제를 의미 할 수 있도록 하나를 뒤집는 것이 좋지만 여기서는 그렇지 않습니다.